Skip to content

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

Closed
@Jules-Bertholet

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

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

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions