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

#[may_dangle], a refined dropck escape hatch (tracking issue for RFC 1327) #34761

Open
7 tasks done
pnkfelix opened this issue Jul 11, 2016 · 36 comments · Fixed by #62568
Open
7 tasks done

#[may_dangle], a refined dropck escape hatch (tracking issue for RFC 1327) #34761

pnkfelix opened this issue Jul 11, 2016 · 36 comments · Fixed by #62568
Assignees
Labels
B-RFC-approved Blocker: Approved by a merged RFC but not yet implemented. B-unstable Blocker: Implemented in the nightly compiler and unstable. C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC F-dropck_eyepatch `#![feature(dropck_eyepatch)]` S-tracking-perma-unstable Status: The feature will stay unstable indefinitely. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@pnkfelix
Copy link
Member

pnkfelix commented Jul 11, 2016

Tracking issue for rust-lang/rfcs#1327

For docs explaining the feature, also see the forge.

@apasel422 apasel422 added the B-RFC-approved Blocker: Approved by a merged RFC but not yet implemented. label Jul 11, 2016
Manishearth added a commit to Manishearth/rust that referenced this issue Sep 30, 2016
Manishearth added a commit to Manishearth/rust that referenced this issue Oct 1, 2016
@pnkfelix pnkfelix self-assigned this Oct 3, 2016
@pnkfelix
Copy link
Member Author

see also #28498

eddyb added a commit to eddyb/rust that referenced this issue Oct 19, 2016
…sakis

`#[may_dangle]` attribute

`#[may_dangle]` attribute

Second step of rust-lang#34761. Last big hurdle before we can work in earnest towards Allocator integration (rust-lang#32838)

Note: I am not clear if this is *also* a syntax-breaking change that needs to be part of a breaking-batch.
eddyb added a commit to eddyb/rust that referenced this issue Oct 19, 2016
…sakis

`#[may_dangle]` attribute

`#[may_dangle]` attribute

Second step of rust-lang#34761. Last big hurdle before we can work in earnest towards Allocator integration (rust-lang#32838)

Note: I am not clear if this is *also* a syntax-breaking change that needs to be part of a breaking-batch.
@pnkfelix
Copy link
Member Author

Some people (myself included) have noted that may_dangle is not an ideal name. (Some have gone so far as to call it "terrible".)

Perhaps we could return to calling it an eyepatch:

3vvuqta

GuillaumeGomez pushed a commit to GuillaumeGomez/rust that referenced this issue Jan 4, 2017
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Jan 4, 2017
Replace uses of `#[unsafe_destructor_blind_to_params]` with `#[may_dangle]`

CC rust-lang#34761

r? @pnkfelix
steveklabnik added a commit to steveklabnik/rust that referenced this issue Jan 4, 2017
Replace uses of `#[unsafe_destructor_blind_to_params]` with `#[may_dangle]`

CC rust-lang#34761

r? @pnkfelix
frewsxcv pushed a commit to frewsxcv/rust that referenced this issue Jan 9, 2017
sanxiyn added a commit to sanxiyn/rust that referenced this issue Jan 10, 2017
Replace uses of `#[unsafe_destructor_blind_to_params]` with `#[may_dangle]`

CC rust-lang#34761

r? @pnkfelix
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Jan 19, 2017
Deprecate `#[unsafe_destructor_blind_to_params]`

CC rust-lang#34761

r? @pnkfelix
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Feb 2, 2017
@RalfJung
Copy link
Member

I have to say I find may_dangle clearer than eyepatch. After all the idea (if I understood it correctly) is that for the given parameters, it may be the case that they do not necessarily outlive the current function call.

@brson brson added the B-unstable Blocker: Implemented in the nightly compiler and unstable. label Mar 1, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC label Jul 22, 2017
@asajeffrey
Copy link

An issue that came up in the context of Josephine is asajeffrey/josephine#52. This is mainly an issue for the interaction between the drop checker and untagged unions (#32836 (comment)) but does have impact on the code guidelines for when may_dangle may be used safely.

The issue is the text "When used on a type, e.g. #[may_dangle] T, the programmer is asserting the only uses of values of that type will be to move or drop them," and whether the programmer is allowed to discard values of type T, that is reclaim the memory used by the value without running T's drop code. In particular, is the programmer allowed to safely mem::forget a value of type T?

@SimonSapin
Copy link
Contributor

std::mem::forget itself is not unsafe. I believe that leaking any value (never running destructors) is considered sound in Rust. (This is why for example scoped threads are based on closures rather than "guard" values with destructors.)

@Centril Centril reopened this Jul 13, 2019
@qnighy
Copy link
Contributor

qnighy commented Nov 24, 2019

Is there anything I can help to stabilize this feature?

@pnkfelix
Copy link
Member Author

I don't think we intend to stabilize #[may_dangle] as is. It is a stop-gap measure until we can develop a more disciplined solution to the problem (e.g., some kind of where-clause on the type, perhaps coupled with an effect-system to allow us to actually check that one is adhering to the may-dangle rules).

@vincent-163
Copy link

Hello. I'm proposing out-of-lifetime references which may be a potential solution to this problem: https://internals.rust-lang.org/t/proposal-about-out-of-lifetime-references/11675/11. Please take a look and leave any feedback or suggestions!

@jonas-schievink jonas-schievink added the T-lang Relevant to the language team, which will review and decide on the PR/issue. label Jan 31, 2020
@KodrAus
Copy link
Contributor

KodrAus commented Sep 8, 2020

We have some new docs in the forge on #[may_dangle] that folks trying to understand it may find helpful. It includes a real example of where we (by we I mean me) got it wrong the first time.

@joshtriplett joshtriplett added the S-tracking-perma-unstable Status: The feature will stay unstable indefinitely. label Jan 19, 2022
@m-ou-se
Copy link
Member

m-ou-se commented Jun 22, 2022

I don't think we intend to stabilize #[may_dangle] as is. It is a stop-gap measure until we can develop a more disciplined solution to the problem (e.g., some kind of where-clause on the type, perhaps coupled with an effect-system to allow us to actually check that one is adhering to the may-dangle rules).

It's been a few years, and it doesn't seem like there will be a proper solution for this problem any time soon. Maybe it's time to stabilze #[may_dangle], to allow crates other than core/alloc/std to use it?

@camsteffen
Copy link
Contributor

camsteffen commented Aug 8, 2022

Trivial blocker. There doesn't seem to be any restriction on where #[may_dangle] is allowed. It is currently allowed on const params, types, statements, etc. and shouldn't be AFAICT. (playground)

@Manishearth
Copy link
Member

perhaps coupled with an effect-system to allow us to actually check that one is adhering to the may-dangle rules

I feel like for an unsafe escape hatch it's probably not a great idea to have a strong effect system situation? If you're using this you're already doing some unsafe tricky thing; the user is already in a situation where they can't prove safety to the compiler; asking them to prove safety seems suboptimal

I also do agree that this should be stable; being able to implement a stable Vec<T> seems like something that rust should let you do, and there's no easy workaround to this at the moment.

@Manishearth
Copy link
Member

A workaround hack that can sometimes work is taking types of the form:

pub struct MyAbstraction<'a, T> {
   stuff: *const T,
   more_stuff: bool,
   even_more_stuff: usize,
   marker: PhantomData<&'a T>,
}

and turning them into this:

pub struct MyAbstraction<'a, T> {
   eyepatch: MyAbstractionEyepatchHack< T>,
      marker: PhantomData<&'a T>,
}
struct MyAbstractionEyepatchHack<T> {
   stuff: *const T,
   more_stuff: bool,
   even_more_stuff: usize,
}

and implementing Drop on the inner type. It's more of a problem if the lifetime is really needed in your internal stuff, though.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Aug 18, 2024
…piler-errors

Check that `#[may_dangle]` is properly applied

It's only valid when applied to a type or lifetime parameter in `Drop` trait implementation.

Tracking issue: rust-lang#34761
cc rust-lang#34761 (comment)
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Aug 18, 2024
…piler-errors

Check that `#[may_dangle]` is properly applied

It's only valid when applied to a type or lifetime parameter in `Drop` trait implementation.

Tracking issue: rust-lang#34761
cc rust-lang#34761 (comment)
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Aug 19, 2024
…piler-errors

Check that `#[may_dangle]` is properly applied

It's only valid when applied to a type or lifetime parameter in `Drop` trait implementation.

Tracking issue: rust-lang#34761
cc rust-lang#34761 (comment)
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Aug 19, 2024
Rollup merge of rust-lang#129235 - GoldsteinE:check-may-dangle, r=compiler-errors

Check that `#[may_dangle]` is properly applied

It's only valid when applied to a type or lifetime parameter in `Drop` trait implementation.

Tracking issue: rust-lang#34761
cc rust-lang#34761 (comment)
@GoldsteinE
Copy link
Contributor

It is a stop-gap measure until we can develop a more disciplined solution to the problem (e.g., some kind of where-clause on the type, perhaps coupled with an effect-system to allow us to actually check that one is adhering to the may-dangle rules).

Nearly five years have passed since this comment. As far as I’m aware, there’ve been no proposed solution and no goal to make one. I think it’s safe to say by this point that unless some actions are deliberately taken, the status quo will remain indefinitely.

I think this state of affairs is less than satisfactory. As of now, it’s impossible to properly implement collections outside of the standard library. This interacts poorly with std’s (completely reasonable) desire to remain lean and leave more complex features to the ecosystem. For example, #56167 was recently closed because one can just use hashbrown, but hashbrown can’t provide a proper dropck interaction on stable. You need to choose between accurate dropck and advanced collection features.

I don’t think blocking this on figuring out effect system, a massive feature that’s not really planned, is reasonable. A different syntax (e.g. where clause instead of an attribute) doesn’t need to block stabilization of this one — a lot of features changed syntax over time, e.g. try!() or recently addr_of!(), and it’s usually not a big deal.

I think it’s time to at least reevaluate the decision to leave this perma-unstable (even if it results in “yes, we’re leaving this perma-unstable because {reasons}” from T-lang). For what it’s worth, I fixed the technical blocker highlighted by @camsteffen.

@GoldsteinE
Copy link
Contributor

I chatted in Zulip for a bit, so here’s the status of this as I understood it.

The current version of #[may_dangle] will not be stabilized as it’s too error-prone. There’re some proposals for a different semantics, most notably rust-lang/rfcs#3417, but they’re kinda stalled. lcnr, who is the author of that one, said:

I would be happy if someone were to look into that RFC to push it's core idea over the finish line though and would then be fine with stabilizing it. That's a lang team question though

I’m not totally sure, but as far as I understand it means it’s blocked on t-lang.

Here’re the Zulip threads:

@RalfJung
Copy link
Member

RalfJung commented Sep 1, 2024

It's not fully blocked on t-lang -- it needs someone who's taking over that PR to suggest and explain the idea to t-lang.

However, before doing that it'd probably be good to ask for a t-lang liaison to ensure the team has the bandwidth to consider the proposal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
B-RFC-approved Blocker: Approved by a merged RFC but not yet implemented. B-unstable Blocker: Implemented in the nightly compiler and unstable. C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC F-dropck_eyepatch `#![feature(dropck_eyepatch)]` S-tracking-perma-unstable Status: The feature will stay unstable indefinitely. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.