#What is `rustc_insignificant_dtor` and how is it used in RC
1 messages · Page 1 of 1 (latest)
?eval ```rs
let mut v = 5;
let rc = std::rc::Rc::new(&mut v);
v += 1;
()
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..
It’s like. I’ve got to test it out. It was going some really strange things with mutable RCs and stuff where the 2nd order transparent methods were lost. Don’t know if this is just me messing up or yeah
Can’t you get the same behavior by wrapping a type with a dtor in a newtype?
?eval
let mut v = 5;
let rc = Box::new(&mut v);
v += 1;
()
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.
Source of the Rust file library/alloc/src/boxed.rs.
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
?eval ```rs
struct NotBox<T>(T);
impl<T> Drop for NotBox<T> { fn drop(&mut self) {} }
let mut v = 5;
let b = NotBox(&mut v);
v += 1;
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`.