Skip to content

Commit 580372b

Browse files
authored
[diagnostics] Emit a a note for optional conversion of raw pointers. (#4442)
In CSDiag.cpp I previously added a helper, isCastToTypedPointer(), to emit an diagnostics explaining how to migrate UnsafeRawPointer conversion. This commit broadens that diagnostic to handle Optional<UnsafeRawPointer>. rdar:27894255 [3.0 migration] Emit an additional note for conversion
1 parent 3f2655e commit 580372b

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5043,6 +5043,10 @@ static bool isCastToTypedPointer(ASTContext &Ctx, const Expr *Fn,
50435043
if (InitType.isNull() || ArgType.isNull())
50445044
return false;
50455045

5046+
// unwrap one level of Optional
5047+
if (auto ArgOptType = ArgType->getOptionalObjectType())
5048+
ArgType = ArgOptType;
5049+
50465050
auto *InitNom = InitType->getAnyNominal();
50475051
if (!InitNom)
50485052
return false;

test/1_stdlib/UnsafePointerDiagnostics.swift

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ func unsafePointerConversionAvailability(
1212
umps: UnsafeMutablePointer<String>,
1313
ups: UnsafePointer<String>) {
1414

15+
let omrp: UnsafeMutableRawPointer? = mrp
16+
let orp: UnsafeRawPointer? = rp
17+
let oumpv: UnsafeMutablePointer<Void> = umpv // expected-warning {{UnsafeMutablePointer<Void> has been replaced by UnsafeMutableRawPointer}}
18+
let oupv: UnsafePointer<Void>? = upv // expected-warning {{UnsafePointer<Void> has been replaced by UnsafeRawPointer}}
19+
let oumpi: UnsafeMutablePointer<Int>? = umpi
20+
let oupi: UnsafePointer<Int>? = upi
21+
let oumps: UnsafeMutablePointer<String>? = umps
22+
let oups: UnsafePointer<String>? = ups
23+
1524
_ = UnsafeMutableRawPointer(mrp)
1625
_ = UnsafeMutableRawPointer(rp) // expected-error {{'init' has been renamed to 'init(mutating:)'}}
1726
_ = UnsafeMutableRawPointer(umpv)
@@ -20,6 +29,14 @@ func unsafePointerConversionAvailability(
2029
_ = UnsafeMutableRawPointer(upi) // expected-error {{'init' has been renamed to 'init(mutating:)'}}
2130
_ = UnsafeMutableRawPointer(umps)
2231
_ = UnsafeMutableRawPointer(ups) // expected-error {{'init' has been renamed to 'init(mutating:)'}}
32+
_ = UnsafeMutableRawPointer(omrp)
33+
_ = UnsafeMutableRawPointer(orp) // expected-error {{'init' has been renamed to 'init(mutating:)'}}
34+
_ = UnsafeMutableRawPointer(oumpv)
35+
_ = UnsafeMutableRawPointer(oupv) // expected-error {{'init' has been renamed to 'init(mutating:)'}}
36+
_ = UnsafeMutableRawPointer(oumpi)
37+
_ = UnsafeMutableRawPointer(oupi) // expected-error {{'init' has been renamed to 'init(mutating:)'}}
38+
_ = UnsafeMutableRawPointer(oumps)
39+
_ = UnsafeMutableRawPointer(oups) // expected-error {{'init' has been renamed to 'init(mutating:)'}}
2340

2441
// These all correctly pass with no error.
2542
_ = UnsafeRawPointer(mrp)
@@ -30,8 +47,15 @@ func unsafePointerConversionAvailability(
3047
_ = UnsafeRawPointer(upi)
3148
_ = UnsafeRawPointer(umps)
3249
_ = UnsafeRawPointer(ups)
50+
_ = UnsafeRawPointer(omrp)
51+
_ = UnsafeRawPointer(orp)
52+
_ = UnsafeRawPointer(oumpv)
53+
_ = UnsafeRawPointer(oupv)
54+
_ = UnsafeRawPointer(oumpi)
55+
_ = UnsafeRawPointer(oupi)
56+
_ = UnsafeRawPointer(oumps)
57+
_ = UnsafeRawPointer(oups)
3358

34-
// UnsafeMutablePointer<Void> to UnsafeMutableRawPointer(umpv)
3559
_ = UnsafeMutablePointer<Void>(rp) // expected-warning 3 {{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), (OpaquePointer), (OpaquePointer?), (UnsafeMutablePointer<Pointee>), (UnsafeMutablePointer<Pointee>?)}} expected-note{{Pointer conversion restricted: use '.assumingMemoryBound(to:)' or '.bindMemory(to:capacity:)' to view memory as a type}}
3660
_ = UnsafeMutablePointer<Void>(mrp) // expected-error {{cannot invoke initializer for type 'UnsafeMutablePointer<Void>' with an argument list of type '(UnsafeMutableRawPointer)'}} expected-note {{}} expected-warning 3 {{UnsafeMutablePointer<Void> has been replaced by UnsafeMutableRawPointer}} expected-note{{overloads for 'UnsafeMutablePointer<Void>' exist with these partially matching parameter lists: (RawPointer), (OpaquePointer), (OpaquePointer?), (UnsafeMutablePointer<Pointee>), (UnsafeMutablePointer<Pointee>?)}}
3761
_ = UnsafeMutablePointer<Void>(umpv) // expected-warning {{UnsafeMutablePointer<Void> has been replaced by UnsafeMutableRawPointer}}
@@ -41,7 +65,6 @@ func unsafePointerConversionAvailability(
4165
_ = UnsafeMutablePointer<Void>(umps) // expected-warning {{UnsafeMutablePointer<Void> has been replaced by UnsafeMutableRawPointer}}
4266
_ = UnsafeMutablePointer<Void>(ups) // expected-error {{'init' has been renamed to 'init(mutating:)'}} expected-warning {{UnsafeMutablePointer<Void> has been replaced by UnsafeMutableRawPointer}}
4367

44-
// UnsafePointer<Void> to UnsafeRawPointer(umpv)
4568
_ = UnsafePointer<Void>(rp) // expected-error {{cannot invoke initializer for type 'UnsafePointer<Void>' with an argument list of type '(UnsafeRawPointer)'}} expected-note {{}} expected-warning 3 {{UnsafePointer<Void> has been replaced by UnsafeRawPointer}} expected-note{{overloads for 'UnsafePointer<Void>' exist with these partially matching parameter lists: (RawPointer), (OpaquePointer), (OpaquePointer?), (UnsafePointer<Pointee>), (UnsafePointer<Pointee>?), (UnsafeMutablePointer<Pointee>), (UnsafeMutablePointer<Pointee>?)}}
4669
_ = UnsafePointer<Void>(mrp) // expected-error {{cannot invoke initializer for type 'UnsafePointer<Void>' with an argument list of type '(UnsafeMutableRawPointer)'}} expected-note {{}} expected-warning 3 {{UnsafePointer<Void> has been replaced by UnsafeRawPointer}} expected-note{{overloads for 'UnsafePointer<Void>' exist with these partially matching parameter lists: (RawPointer), (OpaquePointer), (OpaquePointer?), (UnsafePointer<Pointee>), (UnsafePointer<Pointee>?), (UnsafeMutablePointer<Pointee>), (UnsafeMutablePointer<Pointee>?)}}
4770
_ = UnsafePointer<Void>(umpv) // expected-warning {{UnsafePointer<Void> has been replaced by UnsafeRawPointer}}
@@ -59,6 +82,14 @@ func unsafePointerConversionAvailability(
5982
_ = UnsafeMutablePointer<Int>(upi) // expected-error {{'init' has been renamed to 'init(mutating:)'}}
6083
_ = UnsafeMutablePointer<Int>(umps) // expected-error {{'init' is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type.}}
6184
_ = UnsafeMutablePointer<Int>(ups) // expected-error {{'init' is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type.}}
85+
_ = UnsafeMutablePointer<Int>(orp) // expected-error {{cannot invoke initializer for type 'UnsafeMutablePointer<Int>' with an argument list of type '(UnsafeRawPointer?)'}} expected-note {{Pointer conversion restricted: use '.assumingMemoryBound(to:)' or '.bindMemory(to:capacity:)' to view memory as a type.}} expected-note {{}}
86+
_ = UnsafeMutablePointer<Int>(omrp) // 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 {{}}
87+
_ = UnsafeMutablePointer<Int>(oumpv) // expected-error {{'init' is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type.}}
88+
_ = UnsafeMutablePointer<Int>(oupv) // expected-error {{'init' is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type.}}
89+
_ = UnsafeMutablePointer<Int>(oumpi)
90+
_ = UnsafeMutablePointer<Int>(oupi) // expected-error {{'init' has been renamed to 'init(mutating:)'}}
91+
_ = UnsafeMutablePointer<Int>(oumps) // expected-error {{'init' is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type.}}
92+
_ = UnsafeMutablePointer<Int>(oups) // expected-error {{'init' is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type.}}
6293

6394
_ = UnsafePointer<Int>(rp) // expected-error {{cannot invoke initializer for type 'UnsafePointer<Int>' with an argument list of type '(UnsafeRawPointer)'}} expected-note {{Pointer conversion restricted: use '.assumingMemoryBound(to:)' or '.bindMemory(to:capacity:)' to view memory as a type.}} expected-note {{}}
6495
_ = UnsafePointer<Int>(mrp) // expected-error {{cannot invoke initializer for type 'UnsafePointer<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 {{}}
@@ -68,4 +99,12 @@ func unsafePointerConversionAvailability(
6899
_ = UnsafePointer<Int>(upi)
69100
_ = UnsafePointer<Int>(umps) // expected-error {{'init' is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type.}}
70101
_ = UnsafePointer<Int>(ups) // expected-error {{'init' is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type.}}
102+
_ = UnsafePointer<Int>(orp) // expected-error {{cannot invoke initializer for type 'UnsafePointer<Int>' with an argument list of type '(UnsafeRawPointer?)'}} expected-note {{Pointer conversion restricted: use '.assumingMemoryBound(to:)' or '.bindMemory(to:capacity:)' to view memory as a type.}} expected-note {{}}
103+
_ = UnsafePointer<Int>(omrp) // expected-error {{cannot invoke initializer for type 'UnsafePointer<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 {{}}
104+
_ = UnsafePointer<Int>(oumpv) // expected-error {{'init' is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type.}}
105+
_ = UnsafePointer<Int>(oupv) // expected-error {{'init' is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type.}}
106+
_ = UnsafePointer<Int>(oumpi)
107+
_ = UnsafePointer<Int>(oupi)
108+
_ = UnsafePointer<Int>(oumps) // expected-error {{'init' is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type.}}
109+
_ = UnsafePointer<Int>(oups) // expected-error {{'init' is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type.}}
71110
}

0 commit comments

Comments
 (0)