#What is `rustc_insignificant_dtor` and how is it used in RC

1 messages · Page 1 of 1 (latest)

keen sentinel
#

I’ve been looking at RC’s implementation and found a funky macro #[rustc_insignificant_dtor]. What is this and why does it break #[repr(transparent)]? Have I been using it incorrectly?

tepid creek
#

?eval ```rs
let mut v = 5;
let rc = std::rc::Rc::new(&mut v);
v += 1;

charred lichenBOT
#
()
tepid creek
#

That this code compiles mandates that the rc is dropped before the third statement (if it were dropped at the end of scope like other types are you'd have mutation while a &mut to the value exists)

#

Normally, rustc is not allowed to reorder drops like that, but I'm guessing that attribute gives it the ability to

#

I'm not sure why it would interact with #[repr(transparent)] at all though..

keen sentinel
reef wraith
#

Can’t you get the same behavior by wrapping a type with a dtor in a newtype?

winged grove
charred lichenBOT
#
()
winged grove
#

also works, and the source for Box doesn't seem to contain any mention of #[rustc_insignificant_dtor]. I could be wrong, but I always thought the above code works because of #[feature(nll)]'s early drop capability.

reef wraith
#

Usually destructors are always run at the end of scope, NLL doesn’t effect this

#

Types that don’t implement Drop can have their drop glue moved about but types that do implement Drop don’t unless they have the attribute

tepid creek
charred lichenBOT
#
warning: unused variable: `b`
 --> src/main.rs:5:5
  |
5 | let b = NotBox(&mut v);
  |     ^ help: if this is intentional, prefix it with an underscore: `_b`
  |
  = note: `#[warn(unused_variables)]` on by default

warning: value assigned to `v` is never read
 --> src/main.rs:6:1
  |
6 | v += 1;
  | ^
  |
  = note: `#[warn(unused_assignments)]` on by default
  = help: maybe it is overwritten before being read?

error[E0503]: cannot use `v` because it was mutably borrowed
 --> src/main.rs:6:1
  |
5 | let b = NotBox(&mut v);
  |                ------ borrow of `v` occurs here
6 | v += 1;
  | ^^^^^^ use of borrowed `v`
7 | }); }
  | - borrow might be used here, when `b` is dropped and runs the `Drop` code for type `NotBox`

For more information about this error, try `rustc --explain E0503`.