Skip to content

Commit d2a0ae2

Browse files
committed
Remove the Swift-3-era pointer casting diagnostic.
Remove this additional note when attempting to incorrectly initialize UnsafePointer: Pointer conversion restricted: use '.assumingMemoryBound(to:)' or '.bindMemory(to:capacity:)' to view memory as a type. The note was added for Swift 3 migration. The idea was to get projects building as quickly as possible and effectively tag bad behavior so it could be corrected later. That worked, but now it's been working against us for the past couple of releases. Too much new code is using these two APIs and the vast majority of uses are not only incorrect, but much more simply expressed without them. Instead, programmers need to be made aware that the UnsafeRawBufferPointer API already does what they want... but we can work on that problem as separate task. Most uses of these APIs are something like: return rawptr.assumingMemoryBound(to: T.self).pointee or UnsafeRawPointer(ptr).bindMemory(to: UInt16.self, capacity: 2).pointee Instead of simply return rawptr.load(as: T.self) I've even seen programmers do this: extension UnsafeRawPointer { var uint8: UInt8 { let uint8Ptr = self.bindMemory( to: UInt8.self, capacity: 1 ) return uint8Ptr[0] } } ...instead of simply using either UnsafeRawPointer.load or UnsafeRawBufferPointer's subscript.
1 parent 3924d1a commit d2a0ae2

File tree

3 files changed

+6
-52
lines changed

3 files changed

+6
-52
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,6 @@ ERROR(cannot_call_with_params, none,
250250
"cannot invoke %select{|initializer for type }2'%0' with an argument list"
251251
" of type '%1'", (StringRef, StringRef, bool))
252252

253-
NOTE(pointer_init_to_type,none,
254-
"Pointer conversion restricted: use '.assumingMemoryBound(to:)' or '.bindMemory(to:capacity:)' to view memory as a type.", ())
255-
256253
ERROR(cannot_call_non_function_value,none,
257254
"cannot call value of non-function type %0", (Type))
258255

lib/Sema/CSDiag.cpp

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4146,44 +4146,6 @@ bool FailureDiagnosis::diagnoseArgumentGenericRequirements(
41464146
return result == RequirementCheckResult::Failure;
41474147
}
41484148

4149-
/// When initializing Unsafe[Mutable]Pointer<T> from Unsafe[Mutable]RawPointer,
4150-
/// issue a diagnostic that refers to the API for binding memory to a type.
4151-
static bool isCastToTypedPointer(ConstraintSystem &CS, const Expr *Fn,
4152-
const Expr *Arg) {
4153-
auto &Ctx = CS.DC->getASTContext();
4154-
auto *TypeExp = dyn_cast<TypeExpr>(Fn);
4155-
auto *ParenExp = dyn_cast<ParenExpr>(Arg);
4156-
if (!TypeExp || !ParenExp)
4157-
return false;
4158-
4159-
auto InitType = CS.getInstanceType(TypeExp);
4160-
auto ArgType = CS.getType(ParenExp->getSubExpr());
4161-
if (InitType.isNull() || ArgType.isNull())
4162-
return false;
4163-
4164-
// unwrap one level of Optional
4165-
if (auto ArgOptType = ArgType->getOptionalObjectType())
4166-
ArgType = ArgOptType;
4167-
4168-
auto *InitNom = InitType->getAnyNominal();
4169-
if (!InitNom)
4170-
return false;
4171-
4172-
if (InitNom != Ctx.getUnsafeMutablePointerDecl()
4173-
&& InitNom != Ctx.getUnsafePointerDecl()) {
4174-
return false;
4175-
}
4176-
auto *ArgNom = ArgType->getAnyNominal();
4177-
if (!ArgNom)
4178-
return false;
4179-
4180-
if (ArgNom != Ctx.getUnsafeMutableRawPointerDecl()
4181-
&& ArgNom != Ctx.getUnsafeRawPointerDecl()) {
4182-
return false;
4183-
}
4184-
return true;
4185-
}
4186-
41874149
static bool diagnoseClosureExplicitParameterMismatch(
41884150
ConstraintSystem &CS, SourceLoc loc,
41894151
ArrayRef<AnyFunctionType::Param> params,
@@ -5058,11 +5020,6 @@ bool FailureDiagnosis::visitApplyExpr(ApplyExpr *callExpr) {
50585020
overloadName, argString, isInitializer);
50595021
}
50605022

5061-
if (isCastToTypedPointer(CS, fnExpr, argExpr)) {
5062-
diagnose(fnExpr->getLoc(), diag::pointer_init_to_type)
5063-
.highlight(argExpr->getSourceRange());
5064-
}
5065-
50665023
// Did the user intend on invoking a different overload?
50675024
calleeInfo.suggestPotentialOverloads(fnExpr->getLoc());
50685025
return true;

test/stdlib/UnsafePointerDiagnostics.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ func unsafePointerConversionAvailability(
6161
_ = UnsafeMutablePointer<Int>(umpi)
6262
_ = UnsafeMutablePointer<Int>(oumpi)
6363

64-
_ = UnsafeMutablePointer<Void>(rp) // expected-warning 4 {{UnsafeMutablePointer<Void> has been replaced by UnsafeMutableRawPointer}} expected-error {{cannot invoke initializer for type 'UnsafeMutablePointer<Void>' with an argument list of type '(UnsafeRawPointer)'}} expected-note {{overloads for 'UnsafeMutablePointer<Void>' exist with these partially matching parameter lists: (RawPointer), (UnsafeMutablePointer<Pointee>), (UnsafeMutablePointer<Pointee>?)}} expected-note {{Pointer conversion restricted: use '.assumingMemoryBound(to:)' or '.bindMemory(to:capacity:)' to view memory as a type.}}
65-
_ = UnsafeMutablePointer<Void>(mrp) // expected-warning 4 {{UnsafeMutablePointer<Void> has been replaced by UnsafeMutableRawPointer}} expected-error {{cannot invoke initializer for type 'UnsafeMutablePointer<Void>' with an argument list of type '(UnsafeMutableRawPointer)'}} expected-note {{overloads for 'UnsafeMutablePointer<Void>' exist with these partially matching parameter lists: (RawPointer), (UnsafeMutablePointer<Pointee>), (UnsafeMutablePointer<Pointee>?)}} expected-note {{Pointer conversion restricted: use '.assumingMemoryBound(to:)' or '.bindMemory(to:capacity:)' to view memory as a type.}}
64+
_ = UnsafeMutablePointer<Void>(rp) // expected-warning 4 {{UnsafeMutablePointer<Void> has been replaced by UnsafeMutableRawPointer}} expected-error {{cannot invoke initializer for type 'UnsafeMutablePointer<Void>' with an argument list of type '(UnsafeRawPointer)'}} expected-note {{overloads for 'UnsafeMutablePointer<Void>' exist with these partially matching parameter lists: (RawPointer), (UnsafeMutablePointer<Pointee>), (UnsafeMutablePointer<Pointee>?)}}
65+
_ = UnsafeMutablePointer<Void>(mrp) // expected-warning 4 {{UnsafeMutablePointer<Void> has been replaced by UnsafeMutableRawPointer}} expected-error {{cannot invoke initializer for type 'UnsafeMutablePointer<Void>' with an argument list of type '(UnsafeMutableRawPointer)'}} expected-note {{overloads for 'UnsafeMutablePointer<Void>' exist with these partially matching parameter lists: (RawPointer), (UnsafeMutablePointer<Pointee>), (UnsafeMutablePointer<Pointee>?)}}
6666
_ = UnsafeMutablePointer<Void>(umpv) // expected-warning {{UnsafeMutablePointer<Void> has been replaced by UnsafeMutableRawPointer}}
6767
_ = UnsafeMutablePointer<Void>(umpi) // expected-warning {{UnsafeMutablePointer<Void> has been replaced by UnsafeMutableRawPointer}}
6868
_ = UnsafeMutablePointer<Void>(umps) // expected-warning {{UnsafeMutablePointer<Void> has been replaced by UnsafeMutableRawPointer}}
@@ -76,10 +76,10 @@ func unsafePointerConversionAvailability(
7676
_ = UnsafePointer<Void>(umps) // expected-warning {{UnsafePointer<Void> has been replaced by UnsafeRawPointer}}
7777
_ = UnsafePointer<Void>(ups) // expected-warning {{UnsafePointer<Void> has been replaced by UnsafeRawPointer}}
7878

79-
_ = UnsafeMutablePointer<Int>(rp) // expected-error {{cannot invoke initializer for type 'UnsafeMutablePointer<Int>' with an argument list of type '(UnsafeRawPointer)'}} expected-note {{overloads for 'UnsafeMutablePointer<Int>' exist with these partially matching parameter lists: (RawPointer), (UnsafeMutablePointer<Pointee>), (UnsafeMutablePointer<Pointee>?)}} expected-note {{Pointer conversion restricted: use '.assumingMemoryBound(to:)' or '.bindMemory(to:capacity:)' to view memory as a type.}}
80-
_ = UnsafeMutablePointer<Int>(mrp) // expected-error {{cannot invoke initializer for type 'UnsafeMutablePointer<Int>' with an argument list of type '(UnsafeMutableRawPointer)'}} expected-note {{Pointer conversion restricted: use '.assumingMemoryBound(to:)' or '.bindMemory(to:capacity:)' to view memory as a type.}} expected-note {{overloads for 'UnsafeMutablePointer<Int>' exist with these partially matching parameter lists: (RawPointer), (UnsafeMutablePointer<Pointee>), (UnsafeMutablePointer<Pointee>?)}}
81-
_ = UnsafeMutablePointer<Int>(orp) // expected-error {{cannot invoke initializer for type 'UnsafeMutablePointer<Int>' with an argument list of type '(UnsafeRawPointer?)'}} expected-note {{overloads for 'UnsafeMutablePointer<Int>' exist with these partially matching parameter lists: (RawPointer), (UnsafeMutablePointer<Pointee>), (UnsafeMutablePointer<Pointee>?)}} expected-note {{Pointer conversion restricted: use '.assumingMemoryBound(to:)' or '.bindMemory(to:capacity:)' to view memory as a type.}}
82-
_ = UnsafeMutablePointer<Int>(omrp) // expected-error {{cannot invoke initializer for type 'UnsafeMutablePointer<Int>' with an argument list of type '(UnsafeMutableRawPointer?)'}} expected-note {{overloads for 'UnsafeMutablePointer<Int>' exist with these partially matching parameter lists: (RawPointer), (UnsafeMutablePointer<Pointee>), (UnsafeMutablePointer<Pointee>?)}} expected-note {{Pointer conversion restricted: use '.assumingMemoryBound(to:)' or '.bindMemory(to:capacity:)' to view memory as a type.}}
79+
_ = UnsafeMutablePointer<Int>(rp) // expected-error {{cannot invoke initializer for type 'UnsafeMutablePointer<Int>' with an argument list of type '(UnsafeRawPointer)'}} expected-note {{overloads for 'UnsafeMutablePointer<Int>' exist with these partially matching parameter lists: (RawPointer), (UnsafeMutablePointer<Pointee>), (UnsafeMutablePointer<Pointee>?)}}
80+
_ = UnsafeMutablePointer<Int>(mrp) // expected-error {{cannot invoke initializer for type 'UnsafeMutablePointer<Int>' with an argument list of type '(UnsafeMutableRawPointer)'}} expected-note {{overloads for 'UnsafeMutablePointer<Int>' exist with these partially matching parameter lists: (RawPointer), (UnsafeMutablePointer<Pointee>), (UnsafeMutablePointer<Pointee>?)}}
81+
_ = UnsafeMutablePointer<Int>(orp) // expected-error {{cannot invoke initializer for type 'UnsafeMutablePointer<Int>' with an argument list of type '(UnsafeRawPointer?)'}} expected-note {{overloads for 'UnsafeMutablePointer<Int>' exist with these partially matching parameter lists: (RawPointer), (UnsafeMutablePointer<Pointee>), (UnsafeMutablePointer<Pointee>?)}}
82+
_ = UnsafeMutablePointer<Int>(omrp) // expected-error {{cannot invoke initializer for type 'UnsafeMutablePointer<Int>' with an argument list of type '(UnsafeMutableRawPointer?)'}} expected-note {{overloads for 'UnsafeMutablePointer<Int>' exist with these partially matching parameter lists: (RawPointer), (UnsafeMutablePointer<Pointee>), (UnsafeMutablePointer<Pointee>?)}}
8383

8484
_ = UnsafePointer<Int>(rp) // expected-error {{cannot convert value of type 'UnsafeRawPointer' to expected argument type 'RawPointer'}}
8585
_ = UnsafePointer<Int>(mrp) // expected-error {{cannot convert value of type 'UnsafeMutableRawPointer' to expected argument type 'RawPointer'}}

0 commit comments

Comments
 (0)