Skip to content

[Strict memory safety] Adjust "unsafe" location for string interpolations #81910

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 1 commit into from
Jun 3, 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
30 changes: 22 additions & 8 deletions lib/Sema/TypeCheckEffects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3645,9 +3645,9 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>
}

/// Find the top location where we should put the await
static Expr *walkToAnchor(Expr *e, llvm::DenseMap<Expr *, Expr *> &parentMap,
bool isInterpolatedString,
bool stopAtAutoClosure) {
Expr *walkToAnchor(Expr *e, llvm::DenseMap<Expr *, Expr *> &parentMap,
InterpolatedStringLiteralExpr *interpolatedString,
bool stopAtAutoClosure, EffectKind effect) {
llvm::SmallPtrSet<Expr *, 4> visited;
Expr *parent = e;
Expr *lastParent = e;
Expand All @@ -3662,8 +3662,20 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>
if (parent && !isAnchorTooEarly(parent)) {
return parent;
}
if (isInterpolatedString) {
if (interpolatedString) {
assert(parent == nullptr && "Expected to be at top of expression");

// If the last parent we found is a call to appendInterpolation, adjust
// the anchor location to the interpolated string itself.
if (effect == EffectKind::Unsafe) {
if (auto callExpr = dyn_cast<CallExpr>(lastParent)) {
if (auto calleeDecl = callExpr->getCalledValue()) {
if (calleeDecl->getName().matchesRef(Ctx.Id_appendInterpolation))
return interpolatedString;
}
}
}

if (ArgumentList *args = lastParent->getArgs()) {
if (Expr *unaryArg = args->getUnlabeledUnaryExpr())
return unaryArg;
Expand Down Expand Up @@ -4322,8 +4334,9 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>
Flags.has(ContextFlags::InAsyncLet))) {
Expr *expr = E.dyn_cast<Expr*>();
Expr *anchor = walkToAnchor(expr, parentMap,
CurContext.isWithinInterpolatedString(),
/*stopAtAutoClosure=*/true);
CurContext.getInterpolatedString(),
/*stopAtAutoClosure=*/true,
EffectKind::Async);
if (Flags.has(ContextFlags::StmtExprCoversAwait))
classification.setDowngradeToWarning(true);
if (uncoveredAsync.find(anchor) == uncoveredAsync.end())
Expand All @@ -4348,8 +4361,9 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>
if (!Flags.has(ContextFlags::IsUnsafeCovered)) {
Expr *expr = E.dyn_cast<Expr*>();
Expr *anchor = walkToAnchor(expr, parentMap,
CurContext.isWithinInterpolatedString(),
/*stopAtAutoClosure=*/false);
CurContext.getInterpolatedString(),
/*stopAtAutoClosure=*/false,
EffectKind::Unsafe);

// We don't diagnose uncovered unsafe uses within the next/nextElement
// call, because they're handled already by the for-in loop checking.
Expand Down
6 changes: 6 additions & 0 deletions test/Unsafe/safe.swift
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,9 @@ protocol CustomAssociated: Associated { }
extension SomeClass: CustomAssociated {
typealias Associated = SomeClassWrapper // expected-note{{unsafe type 'SomeClass.Associated' (aka 'SomeClassWrapper') cannot satisfy safe associated type 'Associated'}}
}

func testInterpolation(ptr: UnsafePointer<Int>) {
_ = "Hello \(unsafe ptr)" // expected-warning{{expression uses unsafe constructs but is not marked with 'unsafe'}}{{7-7=unsafe }}
// expected-note@-1{{reference to unsafe type 'UnsafePointer<Int>'}}
// expected-note@-2{{argument #0 in call to instance method 'appendInterpolation' has unsafe type 'UnsafePointer<Int>'}}
}