Skip to content

Commit 7c7ea1d

Browse files
committed
[SE-0458] Suppress "no unsafe operations" warnings outside of strict mode
This is a stop-gap solution to prevent spurious warnings when "unsafe" expressions and for..in loops are used without strict memory safety. The full answer is probably to determine where unsafe code is all the time, so that we can still (correctly) diagnose "no unsafe operations" even outside of strict memory safety mode.
1 parent 8e87541 commit 7c7ea1d

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lib/Sema/TypeCheckEffects.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4309,7 +4309,8 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>
43094309
Ctx.Diags.diagnose(S->getForLoc(), diag::for_unsafe_without_unsafe)
43104310
.fixItInsert(insertionLoc, "unsafe ");
43114311
}
4312-
} else if (S->getUnsafeLoc().isValid()) {
4312+
} else if (S->getUnsafeLoc().isValid() &&
4313+
Ctx.LangOpts.hasFeature(Feature::StrictMemorySafety)) {
43134314
// Extraneous "unsafe" on the sequence.
43144315
Ctx.Diags.diagnose(S->getUnsafeLoc(), diag::no_unsafe_in_unsafe_for)
43154316
.fixItRemove(S->getUnsafeLoc());
@@ -4343,6 +4344,9 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>
43434344
}
43444345

43454346
void diagnoseRedundantUnsafe(UnsafeExpr *E) const {
4347+
if (!Ctx.LangOpts.hasFeature(Feature::StrictMemorySafety))
4348+
return;
4349+
43464350
if (auto *SVE = SingleValueStmtExpr::tryDigOutSingleValueStmtExpr(E)) {
43474351
// For an if/switch expression, produce a tailored warning.
43484352
Ctx.Diags.diagnose(E->getUnsafeLoc(),
@@ -4351,6 +4355,7 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>
43514355
.highlight(E->getUnsafeLoc());
43524356
return;
43534357
}
4358+
43544359
Ctx.Diags.diagnose(E->getUnsafeLoc(), diag::no_unsafe_in_unsafe);
43554360
}
43564361

test/Unsafe/unsafe_nonstrict.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %target-typecheck-verify-swift -print-diagnostic-groups
2+
3+
@unsafe func unsafeFunc() { }
4+
5+
@unsafe
6+
struct UnsafeType { }
7+
8+
protocol P { }
9+
10+
struct X: @unsafe P { }
11+
12+
func acceptP<T: P>(_: T) { }
13+
14+
func testItAll(ut: UnsafeType, x: X, i: Int) {
15+
_ = unsafe ut
16+
unsafe acceptP(x)
17+
_ = unsafe i
18+
}

0 commit comments

Comments
 (0)