Skip to content

&'a T → &'static T in safe code #118876

Closed

Description

@Jules-Bertholet

(Originally found here)

I tried this code:

fn extend<T>(input: &T) -> &'static T {
    struct Bounded<'a, 'b: 'static, T>(&'a T, [&'b (); 0]);
    let n: Box<dyn FnOnce(&T) -> Bounded<'static, '_, T>> = Box::new(|x| Bounded(x, []));
    n(input).0
}

fn extend_mut<'a, T>(input: &'a mut T) -> &'static mut T {
    struct Bounded<'a, 'b: 'static, T>(&'a mut T, [&'b (); 0]);
    let mut n: Box<dyn FnMut(&mut T) -> Bounded<'static, '_, T>> = Box::new(|x| Bounded(x, []));
    n(input).0
}

I expected to see this happen: lifetime error, 'a is not 'static

Instead, this happened: compiles

Meta

Regression in nightly-2022-08-10

found 7 bors merge commits in the specified range
commit[0] 2022-08-08: Auto merge of #98863 - compiler-errors:projection-msg, r=estebank
commit[1] 2022-08-09: Auto merge of #100205 - cjgillot:noice-doc, r=camelid
commit[2] 2022-08-09: Auto merge of #100304 - matthiaskrgr:rollup-gs56vlw, r=matthiaskrgr
commit[3] 2022-08-09: Auto merge of #100089 - JakobDegen:no-invalidate-visitor, r=tmiasko
commit[4] 2022-08-09: Auto merge of #100318 - Dylan-DPC:rollup-18tzp6q, r=Dylan-DPC
commit[5] 2022-08-09: Auto merge of #99217 - lcnr:implied-bounds-pre-norm, r=lcnr
commit[6] 2022-08-09: Auto merge of #100150 - notriddle:notriddle/implementors-js, r=GuillaumeGomez

@rustbot label T-types T-compiler regression-from-stable-to-stable I-unsound A-lifetimes

Activity

  1. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    A-lifetimesArea: Lifetimes / regions
    I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness
    regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.
    T-typesRelevant to the types team, which will review and decide on the PR/issue.
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Dec 12, 2023
  2. Jules-Bertholet commented on Dec 12, 2023

    @Jules-Bertholet
    ContributorAuthor

    Hmm… maybe this is just a duplicate of #114936?

  3. added
    NLL-soundWorking towards the "invalid code does not compile" goal
    A-NLLArea: Non-lexical lifetimes (NLL)
    and removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Dec 12, 2023
  4. compiler-errors commented on Dec 12, 2023

    @compiler-errors
    Contributor

    I went ahead and implemented the "hacky" fix in #114936 (comment): #118882

  5. aliemjay commented on Dec 12, 2023

    @aliemjay
    Contributor

    Looks like an NLL regression. The following invalid example compiles in v1.63.0, where NLL is enabled by default:

    fn extend(s: &str, f: fn(&str) -> &'static &'_ ()) {
        f(s);
    }
  6. added a commit that references this issue on Dec 13, 2023
    8bdbfd4
  7. apiraino commented on Dec 18, 2023

    @apiraino
    Contributor

    WG-prioritization assigning priority (Zulip discussion).

    @rustbot label -I-prioritize +P-high

  8. added
    P-highHigh priority
    and removed
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Dec 18, 2023
  9. added 2 commits that reference this issue on Jan 22, 2024
    49d2f53
    8f44cb0
  10. added 2 commits that reference this issue on Feb 13, 2024
    8d0e6f0
    db9591c
  11. added a commit that references this issue on Feb 14, 2024
    68a543d
  12. moved this to Completed in T-types unsound issueson Jun 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-NLLArea: Non-lexical lifetimes (NLL)A-lifetimesArea: Lifetimes / regionsC-bugCategory: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessNLL-soundWorking towards the "invalid code does not compile" goalP-highHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions