Skip to content

[Strict memory safety] Lift "unsafe" in pattern match expressions #81675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/Sema/TypeCheckPattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -768,13 +768,27 @@ ExprPatternMatchRequest::evaluate(Evaluator &evaluator,
DeclNameLoc(EP->getLoc()));
matchOp->setImplicit();

auto subExpr = EP->getSubExpr();

// Pull off the outer "unsafe" expression.
UnsafeExpr *unsafeExpr = dyn_cast<UnsafeExpr>(subExpr);
if (unsafeExpr) {
subExpr = unsafeExpr->getSubExpr();
}

Comment on lines +771 to +778
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can UnsafeExpr be wrapped in an IdentityExpr?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm. It's possible that we could have a try unsafe or similar here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, that is broken, but I'm going to file a follow-up bug for myself because the try unsafe case is annoyingly hard to fix without breaking internal invariants.

// Note we use getEndLoc here to have the BinaryExpr source range be the same
// as the expr pattern source range.
auto *matchVarRef =
new (ctx) DeclRefExpr(matchVar, DeclNameLoc(EP->getEndLoc()),
/*Implicit=*/true);
auto *matchCall = BinaryExpr::create(ctx, EP->getSubExpr(), matchOp,
Expr *matchCall = BinaryExpr::create(ctx, subExpr, matchOp,
matchVarRef, /*implicit*/ true);

// If there was an "unsafe", put it outside of the match call.
if (unsafeExpr) {
matchCall = UnsafeExpr::createImplicit(ctx, unsafeExpr->getLoc(), matchCall);
}

return {matchVar, matchCall};
}

Expand Down
6 changes: 3 additions & 3 deletions lib/Sema/TypeCheckUnsafe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,20 +165,20 @@ void swift::diagnoseUnsafeUse(const UnsafeUse &use) {
ctx.Diags.diagnose(
loc,
diag::note_unsafe_call_decl_argument_indexed,
calleeDecl, argumentIndex, paramType)
calleeDecl, argumentIndex, argument->getType())
.highlight(argument->getSourceRange());
} else {
ctx.Diags.diagnose(
loc,
diag::note_unsafe_call_decl_argument_named,
calleeDecl, argumentName, paramType)
calleeDecl, argumentName, argument->getType())
.highlight(argument->getSourceRange());
}
} else {
ctx.Diags.diagnose(
loc,
diag::note_unsafe_call_argument_indexed,
argumentIndex, paramType)
argumentIndex, argument->getType())
.highlight(argument->getSourceRange());
}

Expand Down
32 changes: 32 additions & 0 deletions test/Unsafe/safe.swift
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,35 @@ extension Slice {
}
}
}

@unsafe enum SomeEnum {
case first
case second
}

@unsafe var someEnumValue: SomeEnum = unsafe .first

func testSwitch(se: SomeEnum) {
switch unsafe se {
case unsafe someEnumValue: break
default: break
}

switch unsafe se {
case someEnumValue: break
// expected-warning@-1{{expression uses unsafe constructs but is not marked with 'unsafe'}}{{8-8=unsafe }}
// expected-note@-2{{argument #0 in call to operator function '~=' has unsafe type 'SomeEnum'}}
// expected-note@-3{{argument #1 in call to operator function '~=' has unsafe type 'SomeEnum'}}
// expected-note@-4{{reference to unsafe type 'SomeEnum'}}
// expected-note@-5{{reference to unsafe var 'someEnumValue'}}
// expected-note@-6{{reference to let '$match' involves unsafe type 'SomeEnum'}}
default: break
}

// expected-note@+2{{reference to parameter 'se' involves unsafe type 'SomeEnum'}}
// expected-warning@+1{{expression uses unsafe constructs but is not marked with 'unsafe'}}{{10-10=unsafe }}
switch se {
case unsafe someEnumValue: break
default: break
}
}
2 changes: 1 addition & 1 deletion test/Unsafe/unsafe.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ extension ConformsToMultiP: MultiP {
// expected-note@-1{{unsafe type 'UnsafeSuper' cannot satisfy safe associated type 'Ptr'}}
@unsafe func f() -> UnsafeSuper {
.init() // expected-warning{{expression uses unsafe constructs but is not marked with 'unsafe'}}
// expected-note@-1{{argument 'self' in call to initializer 'init' has unsafe type 'UnsafeSuper'}}
// expected-note@-1{{argument 'self' in call to initializer 'init' has unsafe type 'UnsafeSuper.Type'}}
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/Unsafe/unsafe_stdlib.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func test(
) {
var array = [1, 2, 3]
// expected-warning@+2{{expression uses unsafe constructs but is not marked with 'unsafe'}}{{3-3=unsafe }}
// expected-note@+1{{argument #0 in call to instance method 'withUnsafeBufferPointer' has unsafe type '(UnsafeBufferPointer<Element>) throws(E) -> R'}}
// expected-note@+1{{argument #0 in call to instance method 'withUnsafeBufferPointer' has unsafe type '(UnsafeBufferPointer<Int>) -> ()'}}
array.withUnsafeBufferPointer{ buffer in
// expected-warning@+1{{expression uses unsafe constructs but is not marked with 'unsafe'}}{{5-5=unsafe }}
print(buffer) // expected-note{{reference to parameter 'buffer' involves unsafe type 'UnsafeBufferPointer<Int>'}}
Expand Down