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

Drop order of fields? #82

Open
Hawk777 opened this issue Apr 1, 2023 · 0 comments
Open

Drop order of fields? #82

Hawk777 opened this issue Apr 1, 2023 · 0 comments

Comments

@Hawk777
Copy link

Hawk777 commented Apr 1, 2023

In standard Rust, without anything self-referential, struct fields are dropped in order of declaration:

struct Foo;
impl Drop for Foo {
	fn drop(&mut self) {
		println!("Drop Foo");
	}
}
struct Bar;
impl Drop for Bar {
	fn drop(&mut self) {
		println!("Drop Bar");
	}
}
struct Container {
	foo: Foo,
	bar: Bar,
}
fn main() {
	Container {
		foo: Foo,
		bar: Bar,
	};
}
// Prints “Drop Foo” and then “Drop Bar”

Experimentally, when Ouroboros gets involved, references are dropped before their referents, despite appearing after them in declaration order:

struct Foo;
impl Drop for Foo {
	fn drop(&mut self) {
		println!("Drop Foo");
	}
}
struct Bar<'foo>(&'foo Foo);
impl Drop for Bar<'_> {
	fn drop(&mut self) {
		println!("Drop Bar");
	}
}
#[ouroboros::self_referencing]
struct Container {
	foo: Foo,
	#[borrows(foo)]
	#[covariant]
	bar: Bar<'this>,
}
fn main() {
	ContainerBuilder {
		foo: Foo,
		bar_builder: |foo| Bar(foo)
	}.build();
}
// Prints “Drop Bar” and then “Drop Foo”

This is logical in the sense that a reference must be dropped before its referent; however, it is contrary to the usual drop order of struct fields, and it is also AFAICT not documented anywhere in the Ouroboros documentation. Is the drop order something I can rely on? Should it be documented somewhere?

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

No branches or pull requests

1 participant