-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Open
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.Category: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.Diagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
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
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.Category: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.Diagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.