Skip to content

Dropping inconsistent between match and if #43565

Closed
@PieterPenninckx

Description

@PieterPenninckx

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions