-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Consider re-tuning the lifetime elision rules for trait objects #91302
Comments
If this proposition allows to do pub type SectionDataType = dyn SectionData + 'static; and use that type everywhere instead of typing the full type always, I'm in favor of that as it's indeed not always clear that it defaults to |
@scottmcm said:
Historical note: It used to work the way you're suggesting under RFC 0599, but was deliberately changed to Context: for the rest of this comment I'm suggesting changes to the actual behavior and not what the reference says. Some I'd definitely like to see happen and others I'm just throwing out there. Always infer the elided lifetime in function bodiesIn these examples, the elided trait object lifetime is inferred and independent. // Type with no bounds, trait with no bounds
let _: Box<dyn Trait> = bx;
// Type with one bound, trait with no bounds, parameter is elided
let _: &dyn Trait = rf;
// Type with ambiguous bounds, trait with no bounds
let _: Ambiguous<'a, 'b, dyn Trait> = ambig; But in these examples, it is not. // Type with one bound, trait with no bounds, parameter is explicit
// (Elided trait object lifetime is `'a` and cannot be inferred to be `'static` for example)
let _: &'a dyn Trait = rf;
// Trait with bounds (type bounds don't matter)
// (Elided trait object lifetime is `'a` and cannot be inferred to be `'static` for example)
let _: Box<dyn Single<'a>> = bx;
// (Elided trait object lifetime is ambiguous and this throws an error)
let _: Box<dyn Double<'a, 'b>> = bx; I believe it could be inferred in these cases too, which should only allow more code to compile, and that inference would infer the correct lifetimes where needed. I.e. this change would not be major-breaking if it breaks anything. I'd like to see this change. Make
|
The lifetime elision rules for functions are willing to fail sometimes, and that's good because
-> &i32
defaulting to-> &'static i32
would not be what people generally want, and having the error say "hey, what lifetime did you mean?" is way better than getting borrowck errors about "that's not'static
".It might be worth taking inspiration from that to improve the elision rules for
dyn Trait
, as lints for now and possibly as hard changes in a future edition.For example,
impl dyn Trait
is currentlyimpl dyn Trait + 'static
, but it's not clear that's good. It might be better to require that the user writeimpl dyn Trait + '_
orimpl dyn Trait + 'static
to say which they want. Inspired by this thread: https://users.rust-lang.org/t/why-do-associated-functions-in-impl-dyn-trait-require-static-lifetime/67548/2?u=scottmcmSimilarly, the
+ 'static
default applies even inside a struct with a lifetime. For example, #91292 hadwhere the implicit
+ 'static
is more restrictive than needed because it's behind the&'a
-- but being in theBox
hides that from elision.I don't have any concrete proposal here right now, but I figured I'd open this as a place to track it.
Other examples (feel free to edit this post to add more down here):
The text was updated successfully, but these errors were encountered: