Skip to content

[6.2] Fix diagnostics for missing or invalid @_lifetime annotations on inout params #82511

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 5 commits into from
Jun 27, 2025

Conversation

atrick
Copy link
Contributor

@atrick atrick commented Jun 25, 2025

  • Fix LifetimeDependence diagnostic formatting
    Remove incorrectly nested single quotes from the suggested fix.

    (cherry picked from commit 465d6a8)

  • Lifetime diagnostics: clarify @_lifetime usage for inout parameters
    This comes up often when passing a MutableSpan as an 'inout' argument. The
    vague diagnostic was causing developers to attempt incorrect @_lifetime
    annotations. Be clear about why the annotation is needed and which annotation
    should be used.

    (cherry picked from commit df0b81c)

  • Fix misleading Lifetime diagnostics for inout parameters
    Correctly diagnose this as:
    "invalid use of inout dependence on the same inout parameter

    @_lifetime(a: &a)
    func f_inout_useless(a: inout MutableRawSpan) {}
    

    Correctly diagnose this as:
    "lifetime-dependent parameter must be 'inout'":

    @_lifetime(a: borrow a)
    func f_inout_useless(a: borrowing MutableRawSpan) {}
    

    (cherry picked from commit 05fa82b)

  • Fix a compiler crash with '@'_lifetime(inout x), add diagnostic
    This is a common mistake made more common be suggestions of existing diagnostic
    that tell users not to use a 'copy' dependency.

    Report a diagnostic error rather than crashing the compiler. Fix the diagnostic
    output to make sense relative to the source location.

    Fixes rdar://154136015 ([nonescapable] compiler assertion with @_lifetime(x: inout x))

    (cherry picked from commit 080b682)

  • Diagnostic note for invalid @_lifetime annotations on inout params
    Users commonly try to write a lifetime dependency on an 'inout' parameters as:

    @_lifetime(a: &a)
    func f_inout_useless(a: inout MutableRawSpan) {}
    

    This is useless. Guide them toward what they really wanted:

    @_lifetime(a: copy a)
    

    Fixes rdar://151618856 (@Lifetime(..) gives inconsistent error messages)

    (cherry picked from commit 87f2510)

--- CCC ---

Explanation: Fix diagnostics for missing or invalid @_lifetime annotations on inout params

Scope: This primarily affects adopters of the supported experimental Lifetimes feature but also improves diagnostic for regular non-experimental uses of MutableSpan.

Radar/SR Issue:
rdar://151618856 (@Lifetime(...) gives inconsistent error messages)
rdar://154136015 ([nonescapable] compiler assertion with @_lifetime(x: inout x))

main PR: #82505

Risk: Low. This eliminates a compiler crash and improves the quality of the diagnostic messages.

Testing: Added source-level unit tests.

Reviewer: Meghana Gupta

@atrick atrick requested a review from a team as a code owner June 25, 2025 23:42
@atrick atrick added 🍒 release cherry pick Flag: Release branch cherry picks swift 6.2 labels Jun 25, 2025
@atrick atrick requested review from meg-gupta and tbkka June 25, 2025 23:43
@atrick
Copy link
Contributor Author

atrick commented Jun 25, 2025

@swift-ci test

@atrick atrick enabled auto-merge June 26, 2025 01:58
@atrick
Copy link
Contributor Author

atrick commented Jun 26, 2025

@swift-ci test macOS

@atrick
Copy link
Contributor Author

atrick commented Jun 26, 2025

[2025-06-26T02:12:02.037Z] Cloning into 'llvm-project'...
[2025-06-26T02:12:02.037Z] error: inflate: data stream error (incorrect data check)
[2025-06-26T02:12:02.037Z] fatal: serious inflate inconsistency
[2025-06-26T02:12:02.037Z] fatal: fetch-pack: invalid index-pack output

@atrick
Copy link
Contributor Author

atrick commented Jun 26, 2025

@swift-ci test macOS

1 similar comment
@atrick
Copy link
Contributor Author

atrick commented Jun 26, 2025

@swift-ci test macOS

@atrick
Copy link
Contributor Author

atrick commented Jun 26, 2025

CI still fails for unknown reasons after four tries.

[2025-06-26T17:47:18.508Z]  > git checkout -f 9c7c3870d6154699a6616fb807850ad00564bf07 # timeout=30
[2025-06-26T17:47:22.706Z] Commit message: "Merge 384334eff8e98c4f43bb5f311cebbfee2f8015e2 into e180d8bb389aeace23be2e33bf9eb9eb5ea33a4a"
[2025-06-26T17:47:22.706Z] Using 'Changelog to branch' strategy.
[2025-06-26T17:47:22.678Z] fatal: bad revision '^origin/release/6.2'
[2025-06-26T17:47:22.743Z] ERROR: Unable to retrieve changeset
[2025-06-26T17:47:22.743Z] hudson.plugins.git.GitException: Error: git whatchanged --no-abbrev -M "--format=commit %H%ntree %T%nparent %P%nauthor %aN <%aE> %ai%ncommitter %cN <%cE> %ci%n%n%w(0,4,4)%B" -n 1024 9c7c3870d6154699a6616fb807850ad00564bf07 ^origin/release/6.2 in /Users/ec2-user/jenkins/workspace/swift-PR-macos/branch-release/6.2/swift

I'll rebase and start from scratch to see if that helps.

atrick added 5 commits June 26, 2025 12:58
Remove incorrectly nested single quotes from the suggested fix.

(cherry picked from commit 465d6a8)
This comes up often when passing a MutableSpan as an 'inout' argument.  The
vague diagnostic was causing developers to attempt incorrect @_lifetime
annotations. Be clear about why the annotation is needed and which annotation
should be used.

(cherry picked from commit df0b81c)
Correctly diagnose this as:
"invalid use of inout dependence on the same inout parameter

    @_lifetime(a: &a)
    func f_inout_useless(a: inout MutableRawSpan) {}

Correctly diagnose this as:
"lifetime-dependent parameter must be 'inout'":

    @_lifetime(a: borrow a)
    func f_inout_useless(a: borrowing MutableRawSpan) {}

(cherry picked from commit 05fa82b)
This is a common mistake made more common be suggestions of existing diagnostic
that tell users not to use a 'copy' dependency.

Report a diagnostic error rather than crashing the compiler. Fix the diagnostic
output to make sense relative to the source location.

Fixes rdar://154136015 ([nonescapable] compiler assertion with @_lifetime(x: inout x))

(cherry picked from commit 080b682)
Users commonly try to write a lifetime dependency on an 'inout' parameters as:

    @_lifetime(a: &a)
    func f_inout_useless(a: inout MutableRawSpan) {}

This is useless. Guide them toward what they really wanted:

    @_lifetime(a: copy a)

Fixes rdar://151618856 (@Lifetime(..) gives inconsistent error messages)

(cherry picked from commit 87f2510)
@atrick atrick force-pushed the 62-inout-syntax branch from 384334e to 0b0dbd9 Compare June 26, 2025 19:59
@atrick
Copy link
Contributor Author

atrick commented Jun 26, 2025

@swift-ci test

@atrick atrick disabled auto-merge June 26, 2025 22:05
@atrick atrick merged commit 92fd571 into swiftlang:release/6.2 Jun 27, 2025
5 checks passed
@atrick atrick deleted the 62-inout-syntax branch June 27, 2025 06:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🍒 release cherry pick Flag: Release branch cherry picks swift 6.2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants