Skip to content

[6.2] [Strict memory safety] Show issues with unsafe constructs in the for..in loop #81885

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
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
4 changes: 4 additions & 0 deletions lib/Sema/TypeCheckEffects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4555,6 +4555,10 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>
auto insertionLoc = S->getPattern()->getStartLoc();
Ctx.Diags.diagnose(S->getForLoc(), diag::for_unsafe_without_unsafe)
.fixItInsert(insertionLoc, "unsafe ");

for (const auto &unsafeUse : classification.getUnsafeUses()) {
diagnoseUnsafeUse(unsafeUse);
}
}
} else if (S->getUnsafeLoc().isValid()) {
// Extraneous "unsafe" on the sequence.
Expand Down
3 changes: 3 additions & 0 deletions test/Unsafe/safe.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,12 @@ struct UnsafeAsSequence: @unsafe Sequence, @unsafe IteratorProtocol {
func testUnsafeAsSequenceForEach() {
let uas = UnsafeAsSequence()

// expected-note@+2{{reference to unsafe instance method 'next()'}}
// expected-warning@+1{{expression uses unsafe constructs but is not marked with 'unsafe'}}{{12-12=unsafe }}
for _ in uas { } // expected-note{{conformance}}
// expected-warning@-1{{for-in loop uses unsafe constructs but is not marked with 'unsafe'}}{{documentation-file=strict-memory-safety}}{{7-7=unsafe }}

// expected-note@+1{{reference to unsafe instance method 'next()'}}
for _ in unsafe uas { } // expected-warning{{for-in loop uses unsafe constructs but is not marked with 'unsafe'}}{{documentation-file=strict-memory-safety}}{{7-7=unsafe }}

for unsafe _ in unsafe uas { } // okay
Expand All @@ -118,6 +120,7 @@ struct SequenceWithUnsafeIterator: Sequence {
func testUnsafeIteratorForEach() {
let swui = SequenceWithUnsafeIterator()

// expected-note@+1{{reference to unsafe instance method 'next()'}}
for _ in swui { } // expected-warning{{for-in loop uses unsafe constructs but is not marked with 'unsafe'}}{{7-7=unsafe }}
for unsafe _ in swui { } // okay, it's only the iterator that's unsafe
}
Expand Down
3 changes: 3 additions & 0 deletions test/Unsafe/unsafe-suppression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,12 @@ struct UnsafeSequence: @unsafe IteratorProtocol, @unsafe Sequence {
}

func forEachLoop(us: UnsafeSequence) {
// expected-note@+1{{reference to unsafe instance method 'next()'}}
for _ in us { } // expected-warning{{expression uses unsafe constructs but is not marked with 'unsafe'}}{{documentation-file=strict-memory-safety}}{{12-12=unsafe }}
// expected-note@-1{{'@unsafe' conformance of 'UnsafeSequence' to protocol 'Sequence' involves unsafe code}}
// expected-warning@-2{{for-in loop uses unsafe constructs but is not marked with 'unsafe'}}{{documentation-file=strict-memory-safety}}

// expected-note@+1{{reference to unsafe instance method 'next()'}}
for _ in unsafe us { }
// expected-warning@-1{{for-in loop uses unsafe constructs but is not marked with 'unsafe'}}{{documentation-file=strict-memory-safety}}
}