Skip to content

Commit 1b94c3b

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.
1 parent 02e1d11 commit 1b94c3b

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
@@ -8149,7 +8149,7 @@ Expr *ExprRewriter::convertLiteralInPlace(
81498149
Diag<> brokenProtocolDiag, Diag<> brokenBuiltinProtocolDiag) {
81508150
// If coercing a literal to an unresolved type, we don't try to look up the
81518151
// witness members, just do it.
8152-
if (type->is<UnresolvedType>()) {
8152+
if (type->is<UnresolvedType>() || type->is<ErrorType>()) {
81538153
cs.setType(literal, type);
81548154
return literal;
81558155
}

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) {
@@ -1708,8 +1707,7 @@ class ApplyClassifier {
17081707
!fnType->isAsync() &&
17091708
!E->isImplicitlyAsync() &&
17101709
!hasAnyConformances &&
1711-
(fnRef.getExplicitSafety() == ExplicitSafety::Safe ||
1712-
!ctx.LangOpts.hasFeature(Feature::StrictMemorySafety))) {
1710+
fnRef.getExplicitSafety() == ExplicitSafety::Safe) {
17131711
return Classification();
17141712
}
17151713

@@ -1928,7 +1926,6 @@ class ApplyClassifier {
19281926
// If the safety of the callee is unspecified, check the safety of the
19291927
// arguments specifically.
19301928
if (hasUnspecifiedSafety &&
1931-
ctx.LangOpts.hasFeature(Feature::StrictMemorySafety) &&
19321929
!(assumedSafeArguments && assumedSafeArguments->contains(E))) {
19331930
classifyApplyEffect(EffectKind::Unsafe);
19341931
}
@@ -4554,8 +4551,7 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>
45544551
Ctx.Diags.diagnose(S->getForLoc(), diag::for_unsafe_without_unsafe)
45554552
.fixItInsert(insertionLoc, "unsafe ");
45564553
}
4557-
} else if (S->getUnsafeLoc().isValid() &&
4558-
Ctx.LangOpts.hasFeature(Feature::StrictMemorySafety)) {
4554+
} else if (S->getUnsafeLoc().isValid()) {
45594555
// Extraneous "unsafe" on the sequence.
45604556
Ctx.Diags.diagnose(S->getUnsafeLoc(), diag::no_unsafe_in_unsafe_for)
45614557
.fixItRemove(S->getUnsafeLoc());
@@ -4600,9 +4596,6 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>
46004596
}
46014597

46024598
void diagnoseRedundantUnsafe(UnsafeExpr *E) const {
4603-
if (!Ctx.LangOpts.hasFeature(Feature::StrictMemorySafety))
4604-
return;
4605-
46064599
// Silence this warning in the expansion of the _SwiftifyImport macro.
46074600
// This is a hack because it's tricky to determine when to insert "unsafe".
46084601
unsigned bufferID =

lib/Sema/TypeCheckUnsafe.cpp

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

341-
ASTContext &ctx = conformance.getProtocol()->getASTContext();
342-
if (!ctx.LangOpts.hasFeature(Feature::StrictMemorySafety))
343-
return false;
344-
345341
if (!conformance.hasEffect(EffectKind::Unsafe))
346342
continue;
347343

@@ -410,9 +406,6 @@ bool swift::isUnsafeInConformance(const ValueDecl *requirement,
410406

411407
void swift::diagnoseUnsafeType(ASTContext &ctx, SourceLoc loc, Type type,
412408
llvm::function_ref<void(Type)> diagnose) {
413-
if (!ctx.LangOpts.hasFeature(Feature::StrictMemorySafety))
414-
return;
415-
416409
if (!type->isUnsafe())
417410
return;
418411

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)