Skip to content
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

explicit-outlives-requirements suggestion breaks code #134902

Open
matthiaskrgr opened this issue Dec 29, 2024 · 0 comments
Open

explicit-outlives-requirements suggestion breaks code #134902

matthiaskrgr opened this issue Dec 29, 2024 · 0 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@matthiaskrgr
Copy link
Member

I tried this code:

//@ run-pass
// Test that the lifetime of the enclosing `&` is used for the object
// lifetime bound.


#![allow(dead_code)]

use std::fmt::Display;

trait Test {
    fn foo(&self) { }
}

struct Ref<'a,T:'a+?Sized> {
    r: &'a T
}

struct Ref2<'a,'b,T:'a+'b+?Sized> {
    a: &'a T,
    b: &'b T
}

struct SomeStruct<'a> {
    t: Ref<'a, dyn Test>,
    u: Ref<'a, dyn Test+'a>,
}

fn a<'a>(t: Ref<'a, dyn Test>, mut ss: SomeStruct<'a>) {
    ss.t = t;
}

fn b<'a>(t: Ref<'a, dyn Test>, mut ss: SomeStruct<'a>) {
    ss.u = t;
}

fn c<'a>(t: Ref<'a, dyn Test+'a>, mut ss: SomeStruct<'a>) {
    ss.t = t;
}

fn d<'a>(t: Ref<'a, dyn Test+'a>, mut ss: SomeStruct<'a>) {
    ss.u = t;
}

fn e<'a>(_: Ref<'a, dyn Display+'static>) {}
fn g<'a, 'b>(_: Ref2<'a, 'b, dyn Display+'static>) {}


fn main() {
    // Inside a function body, we can just infer all
    // lifetimes, to allow Ref<'tmp, Display+'static>
    // and Ref2<'tmp, 'tmp, Display+'static>.
    let x = &0 as &(dyn Display+'static);
    let r: Ref<dyn Display> = Ref { r: x };
    let r2: Ref2<dyn Display> = Ref2 { a: x, b: x };
    e(r);
    g(r2);
}

rustc --edition=2024 --crate-type lib --force-warn explicit-outlives-requirements object-lifetime-default-from-ref-struct.rs
=>

warning: outlives requirements can be inferred
  --> object-lifetime-default-from-ref-struct.rs:14:17
   |
14 | struct Ref<'a,T:'a+?Sized> {
   |                 ^^^ help: remove this bound
   |
   = note: requested on the command line with `--force-warn explicit-outlives-requirements`

warning: outlives requirements can be inferred
  --> object-lifetime-default-from-ref-struct.rs:18:21
   |
18 | struct Ref2<'a,'b,T:'a+'b+?Sized> {
   |                     ^^^^^^ help: remove these bounds

warning: 2 warnings emitted

applying the suggestion causes to code to no longer build:

error: lifetime may not live long enough
  --> object-lifetime-default-from-ref-struct.rs:37:5
   |
36 | fn c<'a>(t: Ref<'a, dyn Test+'a>, mut ss: SomeStruct<'a>) {
   |      -- lifetime `'a` defined here
37 |     ss.t = t;
   |     ^^^^^^^^ assignment requires that `'a` must outlive `'static`

error: aborting due to 1 previous error

Meta

rustc --version --verbose:

rustc 1.85.0-nightly (8742e0556 2024-12-28)
binary: rustc
commit-hash: 8742e0556dee3c64f7144de2fb2e88936418865a
commit-date: 2024-12-28
host: x86_64-unknown-linux-gnu
release: 1.85.0-nightly
LLVM version: 19.1.6
@matthiaskrgr matthiaskrgr added A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Dec 29, 2024
@matthiaskrgr matthiaskrgr changed the title explicit-outlives-requirements breaks code explicit-outlives-requirements suggestion breaks code Dec 29, 2024
@jieyouxu jieyouxu added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Dec 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants