Skip to content

Commit 60024c3

Browse files
committed
Always emit "unsafe does not cover any unsafe constructs" warning
Check for unsafe constructs in all modes, so that we can emit the "unsafe does not cover any unsafe constructs" warning consistently. One does not need to write "unsafe" outside of strict memory safety mode, but if you do... it needs to cover unsafe behavior. (cherry picked from commit 1b94c3b)
1 parent 589fd33 commit 60024c3

File tree

7 files changed

+9
-23
lines changed

7 files changed

+9
-23
lines changed

lib/Sema/CSApply.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8172,7 +8172,7 @@ Expr *ExprRewriter::convertLiteralInPlace(
81728172
Diag<> brokenProtocolDiag, Diag<> brokenBuiltinProtocolDiag) {
81738173
// If coercing a literal to an unresolved type, we don't try to look up the
81748174
// witness members, just do it.
8175-
if (type->is<UnresolvedType>()) {
8175+
if (type->is<UnresolvedType>() || type->is<ErrorType>()) {
81768176
cs.setType(literal, type);
81778177
return literal;
81788178
}

lib/Sema/TypeCheckEffects.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,8 +1187,7 @@ class Classification {
11871187
Classification result;
11881188
bool considerAsync = !onlyEffect || *onlyEffect == EffectKind::Async;
11891189
bool considerThrows = !onlyEffect || *onlyEffect == EffectKind::Throws;
1190-
bool considerUnsafe = (!onlyEffect || *onlyEffect == EffectKind::Unsafe) &&
1191-
ctx.LangOpts.hasFeature(Feature::StrictMemorySafety);
1190+
bool considerUnsafe = (!onlyEffect || *onlyEffect == EffectKind::Unsafe);
11921191

11931192
// If we're tracking "unsafe" effects, compute them here.
11941193
if (considerUnsafe) {
@@ -1710,8 +1709,7 @@ class ApplyClassifier {
17101709
!fnType->isAsync() &&
17111710
!E->isImplicitlyAsync() &&
17121711
!hasAnyConformances &&
1713-
(fnRef.getExplicitSafety() == ExplicitSafety::Safe ||
1714-
!ctx.LangOpts.hasFeature(Feature::StrictMemorySafety))) {
1712+
fnRef.getExplicitSafety() == ExplicitSafety::Safe) {
17151713
return Classification();
17161714
}
17171715

@@ -1932,7 +1930,6 @@ class ApplyClassifier {
19321930
// If the safety of the callee is unspecified, check the safety of the
19331931
// arguments specifically.
19341932
if (hasUnspecifiedSafety &&
1935-
ctx.LangOpts.hasFeature(Feature::StrictMemorySafety) &&
19361933
!(assumedSafeArguments && assumedSafeArguments->contains(E))) {
19371934
classifyApplyEffect(EffectKind::Unsafe);
19381935
}
@@ -4559,8 +4556,7 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>
45594556
Ctx.Diags.diagnose(S->getForLoc(), diag::for_unsafe_without_unsafe)
45604557
.fixItInsert(insertionLoc, "unsafe ");
45614558
}
4562-
} else if (S->getUnsafeLoc().isValid() &&
4563-
Ctx.LangOpts.hasFeature(Feature::StrictMemorySafety)) {
4559+
} else if (S->getUnsafeLoc().isValid()) {
45644560
// Extraneous "unsafe" on the sequence.
45654561
Ctx.Diags.diagnose(S->getUnsafeLoc(), diag::no_unsafe_in_unsafe_for)
45664562
.fixItRemove(S->getUnsafeLoc());
@@ -4605,9 +4601,6 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>
46054601
}
46064602

46074603
void diagnoseRedundantUnsafe(UnsafeExpr *E) const {
4608-
if (!Ctx.LangOpts.hasFeature(Feature::StrictMemorySafety))
4609-
return;
4610-
46114604
// Silence this warning in the expansion of the _SwiftifyImport macro.
46124605
// This is a hack because it's tricky to determine when to insert "unsafe".
46134606
unsigned bufferID =

lib/Sema/TypeCheckUnsafe.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -341,10 +341,6 @@ bool swift::enumerateUnsafeUses(ArrayRef<ProtocolConformanceRef> conformances,
341341
if (conformance.isInvalid())
342342
continue;
343343

344-
ASTContext &ctx = conformance.getProtocol()->getASTContext();
345-
if (!ctx.LangOpts.hasFeature(Feature::StrictMemorySafety))
346-
return false;
347-
348344
if (!conformance.hasEffect(EffectKind::Unsafe))
349345
continue;
350346

@@ -413,9 +409,6 @@ bool swift::isUnsafeInConformance(const ValueDecl *requirement,
413409

414410
void swift::diagnoseUnsafeType(ASTContext &ctx, SourceLoc loc, Type type,
415411
llvm::function_ref<void(Type)> diagnose) {
416-
if (!ctx.LangOpts.hasFeature(Feature::StrictMemorySafety))
417-
return;
418-
419412
if (!type->isUnsafe())
420413
return;
421414

test/Constraints/issue-66553.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
func baz(y: [Int], z: Int) -> Int {
66
switch z {
7-
case y[let z]: // expected-error {{'let' binding pattern cannot appear in an expression}}
7+
case y[let z]: // expected-error 2{{'let' binding pattern cannot appear in an expression}}
88
z
99
default:
1010
z

test/Parse/matching_patterns.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ func testNonBinding5(_ x: Int, y: [Int]) {
406406
func testNonBinding6(y: [Int], z: Int) -> Int {
407407
switch 0 {
408408
// We treat 'z' here as a binding, which is invalid.
409-
case let y[z]: // expected-error {{pattern variable binding cannot appear in an expression}}
409+
case let y[z]: // expected-error 2{{pattern variable binding cannot appear in an expression}}
410410
z
411411
case y[z]: // This is fine
412412
0

test/Unsafe/unsafe_nonstrict.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ func acceptP<T: P>(_: T) { }
1414
func testItAll(ut: UnsafeType, x: X, i: Int) {
1515
_ = unsafe ut
1616
unsafe acceptP(x)
17-
_ = unsafe i
17+
_ = unsafe i // expected-warning{{no unsafe operations occur within 'unsafe' expression}}
1818
}

test/stmt/typed_throws.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,11 +361,11 @@ func testSequenceExpr() async throws(Never) {
361361

362362
_ = unsafe await try! getIntAsync() + getIntAsync()
363363
// expected-warning@-1 {{'try' must precede 'await'}}
364-
364+
// expected-warning@-2{{no unsafe operations occur within 'unsafe' expression}}
365365
_ = try unsafe await try! getIntAsync() + getIntAsync()
366366
// expected-warning@-1 {{'try' must precede 'await'}}
367367
// expected-warning@-2 {{no calls to throwing functions occur within 'try' expression}}
368-
368+
// expected-warning@-3{{no unsafe operations occur within 'unsafe' expression}}
369369
try _ = (try! getInt()) + getInt()
370370
// expected-error@-1:29 {{thrown expression type 'any Error' cannot be converted to error type 'Never'}}
371371

0 commit comments

Comments
 (0)