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

Dropping inconsistent between match and if #43565

Closed
PieterPenninckx opened this issue Jul 31, 2017 · 1 comment
Closed

Dropping inconsistent between match and if #43565

PieterPenninckx opened this issue Jul 31, 2017 · 1 comment

Comments

@PieterPenninckx
Copy link
Contributor

I wrote the following code:

struct Goodbye;

impl Goodbye {
	fn new() -> Self { Goodbye }
	
	fn get(&self) -> bool {	true }
}

impl Drop for Goodbye {
	fn drop(&mut self) {
		println!("Dropping");
	}
}

fn main() {
	let x = match Goodbye::new().get() {
		true => {
			println!("In match");
			1
		},
		false => 0
	};
	
	println!("------");
	
	let y = if Goodbye::new().get() {
		println!("In if");
		1
	} else {
		0
	};
}

When compile this with rustc 1.19 and I execute this, the output is:

In match
Dropping
------
Dropping
In if

Thanks to the people who commented on issue #37612, I now understand why, in the match expression, the temporary is only dropped after printing "In match", but I expected the same behaviour in the if-expression, in other words, I expected the output to be:

In match
Dropping
------
In if
Dropping

Am I missing something, or is this a bug?

Info about my version of the compiler:

rustc 1.19.0 (0ade33941 2017-07-17)
binary: rustc
commit-hash: 0ade339411587887bf01bcfa2e9ae4414c8900d4
commit-date: 2017-07-17
host: x86_64-unknown-linux-gnu
release: 1.19.0
LLVM version: 4.0

Out of curiosity, I have also tried with rustc 1.1.0 and then I got the output I expected.

@arielb1
Copy link
Contributor

arielb1 commented Jul 31, 2017

Yeah. if expressions drop their temporaries right after the expression ends - see #12033.

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

2 participants