Skip to content

Diagnostic for changed lifetime in trait object parameters points to tracking issue for different change #151101

@traviscross

Description

@traviscross

Tracking:

Given this code:

trait AnyL<'x> {}

fn f<'long: 'short, 'short>(
    x: *const dyn AnyL<'long>,
) -> *const dyn AnyL<'short> {
    x as *const dyn AnyL<'short>
}

Our diagnostic, after #136776, reports:

error: lifetime may not live long enough
 --> src/main.rs:7:5
  |
4 | fn f<'long: 'short, 'short>(
  |      -----          ------ lifetime `'short` defined here
  |      |
  |      lifetime `'long` defined here
...
7 |     x as *const dyn AnyL<'short>
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cast requires that `'short` must outlive `'long`
  |
  = help: consider adding the following bound: `'short: 'long`
note: raw pointer casts of trait objects cannot extend lifetimes
 --> src/main.rs:7:5
  |
7 |     x as *const dyn AnyL<'short>
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  = note: this was previously accepted by the compiler but was changed recently
  = help: see <https://github.com/rust-lang/rust/issues/141402> for more information

It says this was previously accepted, but as far as I know, it never was. I tested it in Rust 1.0, Rust 1.85, and in various other version. It was rejected in the versions I tested.

Related:

cc @BoxyUwU @Darksonn @ehuss

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-dyn-traitArea: trait objects, vtable layoutA-lifetimesArea: Lifetimes / regionsA-type-systemArea: Type systemD-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.T-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.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions