Skip to content

Commit ae77f6a

Browse files
authored
Merge pull request #4443 from atrick/rawptr-optional-cast
[diagnostics] Emit a a note for optional conversion of raw pointers. …
2 parents 6934265 + 9895af0 commit ae77f6a

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

lib/Sema/CSDiag.cpp

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

5026+
// unwrap one level of Optional
5027+
if (auto ArgOptType = ArgType->getOptionalObjectType())
5028+
ArgType = ArgOptType;
5029+
50265030
auto *InitNom = InitType->getAnyNominal();
50275031
if (!InitNom)
50285032
return false;

test/1_stdlib/UnsafePointerDiagnostics.swift

Lines changed: 41 additions & 1 deletion
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,6 +47,14 @@ 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

3459
_ = 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 {{Pointer conversion restricted: use '.assumingMemoryBound(to:)' or '.bindMemory(to:capacity:)' to view memory as a type.}} expected-note {{}}
3560
_ = UnsafeMutablePointer<Void>(mrp) // 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 '(UnsafeMutableRawPointer)'}} expected-note {{Pointer conversion restricted: use '.assumingMemoryBound(to:)' or '.bindMemory(to:capacity:)' to view memory as a type.}} expected-note {{}}
@@ -40,7 +65,6 @@ func unsafePointerConversionAvailability(
4065
_ = UnsafeMutablePointer<Void>(umps) // expected-warning {{UnsafeMutablePointer<Void> has been replaced by UnsafeMutableRawPointer}}
4166
_ = UnsafeMutablePointer<Void>(ups) // expected-error {{'init' has been renamed to 'init(mutating:)'}} expected-warning {{UnsafeMutablePointer<Void> has been replaced by UnsafeMutableRawPointer}}
4267

43-
4468
_ = UnsafePointer<Void>(rp) // expected-error {{cannot invoke initializer for type 'UnsafePointer<Void>' 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 {{}} expected-warning 3 {{UnsafePointer<Void> has been replaced by UnsafeRawPointer}}
4569
_ = UnsafePointer<Void>(mrp) // expected-error {{cannot invoke initializer for type 'UnsafePointer<Void>' 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 {{}} expected-warning 3 {{UnsafePointer<Void> has been replaced by UnsafeRawPointer}}
4670
_ = UnsafePointer<Void>(umpv) // expected-warning {{UnsafePointer<Void> has been replaced by UnsafeRawPointer}}
@@ -58,6 +82,14 @@ func unsafePointerConversionAvailability(
5882
_ = UnsafeMutablePointer<Int>(upi) // expected-error {{'init' has been renamed to 'init(mutating:)'}}
5983
_ = UnsafeMutablePointer<Int>(umps) // expected-error {{'init' is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type.}}
6084
_ = 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.}}
6193

6294
_ = 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 {{}}
6395
_ = 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 {{}}
@@ -67,4 +99,12 @@ func unsafePointerConversionAvailability(
6799
_ = UnsafePointer<Int>(upi)
68100
_ = UnsafePointer<Int>(umps) // expected-error {{'init' is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type.}}
69101
_ = 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.}}
70110
}

0 commit comments

Comments
 (0)