-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Allow passing MutableSpan 'inout' without an experimental feature. #81656
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 |
This is also failing on main: |
@swift-ci test |
@slavapestov @DougGregor in this commit I have suppressed the extra "through reference here" diagnostic by ignoring LifetimeDependenceRequest. But I noticed that we have redundant "through reference here" notes in other cases. Does this suppression make sense or should I allow the redundant note? |
@slavapestov @DougGregor is it acceptable to change the order of declarations in test/decl/protocols/protocols.swift? On this test, we now say protocol 'CircleMiddle' refines itself
Current main:
With this PR:
|
@meg-gupta did you run into similar problems before you guarded all the lifetime dependence type checking by the feature flag? |
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, thank you!
@swift-ci test |
This adds a new lifetime inference rule, loosening the requirement for @Lifetime annotations even when the experimental LifetimeDependence mode is enabled. Additionally, it enables this new inference rule even when the experimental mode is disabled. All other inference rules continue to require the experimental feature. The rule is: If a function or method has a single inout non-Escapable parameter other than 'self' and has no other non-Escapable parameters including 'self', then infer a single @Lifetime(copy) dependency on the inout parameter from its own incoming value. This supports the common case in which the user of a non-Escapable type, such as MutableSpan, wants to modify the span's contents without modifying the span value itself. It should be possible to use MutableSpan this way without requiring any knowledge of lifetime annotations. The tradeoff is that it makes authoring non-Escapable types less safe. For example, a MutableSpan method could update the underlying unsafe pointer and forget to declare a dependence on the incoming pointer. Disallowing other non-Escapable parameters rules out the easy mistake of programmers attempting to trivially reassign the inout parameter. There's is no way to rule out the possibility that they derive another non-Escapable value from an Escapable parameteter. So users can still write the following: func reassign(s: inout MutableSpan<Int>, a: [Int]) { s = a.mutableSpan } The 'reassign' declaration will type check, but it's implementation will diagnose a lifetime error on 's'. Fixes rdar://150557314 ([nonescapable] Declaration of inout MutableSpan parameter requires LifetimeDependence experimental feature)
When reporting the declarations that lead to a cycle, we end up printing an extra "through reference here" on the function declaration: class C2: C1, P { | |- note: through reference here | `- note: through reference here 15 | // expected-note@-1 2{{through reference here}} 16 | override func run(a: A) {} | | | `- note: while resolving type 'A' | | `- note: through reference here | |- error: circular reference | |- note: through reference here | `- note: through reference here
Evaluating LifetimeDependence changes the order that the declarations are diagnosed. Fix this test output by flipping the order of two declarations. The new order actually makes more sense to me.
22d477d
to
c04e555
Compare
@swift-ci test |
Unrelated linux failure
|
@swift-ci smoke test linux |
…infer" This reverts commit e240dd5.
This adds a new lifetime inference rule, loosening the requirement for @Lifetime
annotations even when the experimental LifetimeDependence mode is
enabled. Additionally, it enables this new inference rule even when the
experimental mode is disabled. All other inference rules continue to require the
experimental feature. The rule is:
If a function or method has a single inout non-Escapable parameter other than
'self' and has no other non-Escapable parameters including 'self', then infer a
single @Lifetime(copy) dependency on the inout parameter from its own incoming
value.
This supports the common case in which the user of a non-Escapable type,
such as MutableSpan, wants to modify the span's contents without modifying
the span value itself. It should be possible to use MutableSpan this way
without requiring any knowledge of lifetime annotations. The tradeoff is
that it makes authoring non-Escapable types less safe. For example, a
MutableSpan method could update the underlying unsafe pointer and forget to
declare a dependence on the incoming pointer.
Disallowing other non-Escapable parameters rules out the easy mistake of
programmers attempting to trivially reassign the inout parameter. There's
is no way to rule out the possibility that they derive another
non-Escapable value from an Escapable parameteter. So users can still write
the following:
The 'reassign' declaration will type check, but it's implementation will
diagnose a lifetime error on 's'.
Fixes rdar://150557314 ([nonescapable] Declaration of inout MutableSpan
parameter requires LifetimeDependence experimental feature)