-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Disable surprising lifetime inference of implicit initializers #82472
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
Conversation
@swift-ci test |
Linux PR testing on main has been blocked all day
|
@swift-ci smoke test linux |
1 similar comment
@swift-ci smoke test linux |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
Can you add a swift test for CountedSpan
as well with your comments from the PR?
I added a motivational example with explanation. |
@swift-ci smoke test |
1 similar comment
@swift-ci smoke test |
@swift-ci smoke test windows |
@swift-ci smoke test macOS |
This PR used to pass testing but now keeps failing with
|
@swift-ci smoke test |
@swift-ci smoke test macOS |
1 similar comment
@swift-ci smoke test macOS |
Non-escapable struct definitions often have inicidental integer fields that are unrelated to lifetime. Without an explicit initializer, the compiler would infer these fields to be borrowed by the implicit intializer. struct CountedSpan: ~Escapable { let span: Span<Int> let i: Int /* infer: @Lifetime(copy span, borrow i) init(...) */ } This was done because - we always want to infer lifetimes of synthesized code if possible - inferring a borrow dependence is always conservative But this was the wrong decision because it inevitabely results in lifetime diagnostic errors elsewhere in the code that can't be tracked down at the use site: let span = CountedSpan(span: span, i: 3) // ERROR: span depends on the lifetime of this value Instead, force the author of the data type to specify whether the type actually depends on trivial fields or not. Such as: struct CountedSpan: ~Escapable { let span: Span<Int> let i: Int @Lifetime(copy span) init(...) { ... } } This fix enables stricter diagnostics, so we need it in 6.2. Fixes rdar://152130977 ([nonescapable] confusing diagnostic message when a synthesized initializer generates dependence on an Int parameter)
@swift-ci smoke test |
@swift-ci smoke test windows |
1 similar comment
@swift-ci smoke test windows |
Non-escapable struct definitions often have inicidental integer fields that are
unrelated to lifetime. Without an explicit initializer, the compiler would infer
these fields to be borrowed by the implicit intializer.
This was done because
But this was the wrong decision because it inevitabely results in lifetime
diagnostic errors elsewhere in the code that can't be tracked down at the use
site:
Instead, force the author of the data type to specify whether the type actually
depends on trivial fields or not. Such as:
This fix enables stricter diagnostics, so we need it in 6.2.
Fixes rdar://152130977 ([nonescapable] confusing diagnostic message when a
synthesized initializer generates dependence on an Int parameter)