Skip to content

librustc: Implement arbitrary lifetimes in trait objects. #16068

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

Conversation

pcwalton
Copy link
Contributor

librustc: Implement arbitrary lifetimes in trait objects. …

All trait objects must be annotated with a lifetime. This means that
code like this breaks:

fn f(x: Box<Trait>) { ... }

fn g<'a>(x: &'a Trait) { ... }

Change this code to:

fn f(x: Box<Trait+'static>) { ... }

fn g<'a>(x: &'a Trait<'a>) { ... }

This will be eventually addressed by some additions that @nikomatsakis
wants. However, the fundamental thrust of this change is necessary to
achieve memory safety. Further additions to improve ergonomics will
follow.

Closes #5723.

r? @nikomatsakis

@pnkfelix
Copy link
Member

(please tease apart the functional changes to rustc from the fallout from said changes. If you want to delegate that task to me, I am happy to do it.)

@alexcrichton
Copy link
Member

Does this require that all trait objects in structures and impl headers have an explicitly marked lifetime? This looks like it unfortunately makes working with trait objects much more verbose.

@pcwalton
Copy link
Contributor Author

Yes. However, it is needed for safety.

@pcwalton
Copy link
Contributor Author

Niko has plans to fix some of the verbosity, but I don't know specifically what they are.

I think we should merge this, because verbosity is no excuse for memory unsafety in safe code.

All trait objects must be annotated with a lifetime. This means that
code like this breaks:

    fn f(x: Box<Trait>) { ... }

    fn g<'a>(x: &'a Trait) { ... }

Change this code to:

    fn f(x: Box<Trait+'static>) { ... }

    fn g<'a>(x: &'a Trait<'a>) { ... }

This will be eventually addressed by some additions that @nikomatsakis
wants. However, the fundamental thrust of this change is necessary to
achieve memory safety. Further additions to improve ergonomics will
follow.

Closes rust-lang#5723.
@pcwalton pcwalton changed the title librustc: WIP to implement arbitrary lifetimes in trait objects. librustc: Implement arbitrary lifetimes in trait objects. Jul 29, 2014
@nikomatsakis
Copy link
Contributor

I will review but it is difficult this week due to TC39.

@pcwalton
Copy link
Contributor Author

@nikomatsakis I'm happy to punt to @pnkfelix if you'd like.

unsafe {
// FIXME(pcwalton): This is totally bogus with the
// lifetimes. I have no idea how this even worked to begin
// with.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Certainly it should not have type-checked before, but it is not bogus. In my branch the result of this function is Box<MacResult+'cx> where 'cx is the lifetime of the cx variable.

@pcwalton
Copy link
Contributor Author

Obsoleted by #16483.

@pcwalton pcwalton closed this Aug 13, 2014
@pnkfelix
Copy link
Member

(I assume the link above was supposed to be to #16453 )

lnicola pushed a commit to lnicola/rust that referenced this pull request Jan 3, 2024
…e-conflicts, r=Veykril

fix: pick up new names when the name conflicts in 'introduce_named_generic'

Improve generation of names for generic parameters in `introduce_named_generics`.

fix rust-lang#15731.

### Changes

- Modified `for_generic_parameter` function in `suggest_name.rs` to handle conflicts with existing generic parameters and generate unique names accordingly.
- Update `introduce_named_generic` function and pass existing params to `for_generic_parameter`, enabling the detection and handling of name collisions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lifetimes can escape in traits / objects
4 participants