Skip to content

Commit 1352d19

Browse files
committed
Auto merge of #68493 - ecstatic-morse:enum-drop-elab-optim, r=<try>
[WIP] Don't `drop` enum if all fields are moved out of Previously, drop elaboration is smart enough to realize when all fields of a struct or tuple are moved out of along a given code path and will remove drop terminators on that code path. However, if all fields of an *enum* with multiple variants are moved out of, drop terminators are still emitted. After this PR, drop elaboration will eliminate drops of an `Option` after the value in the `Some` variant is moved out of. `Option::unwrap` is an example of code whose MIR should be a bit better optimized as a result of this. ```rust fn unwrap<T>(opt: Option<T>) -> T { match opt { Some(inner) => inner, None => panic!("unwrap"), } } ``` When I say all fields of an enum, I mean all fields across all variants, so this optimization does not apply when, e.g., the value in `Result::Ok` is moved out of. To improve this case, we would need to incorporate information about the variant that is currently live into the `{Un,}InitializedPlaces` dataflow analyses. This is more involved, but I plan to give it a try.
2 parents e0bbe79 + 06dbaec commit 1352d19

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

src/librustc_mir/util/elaborate_drops.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ where
472472
}
473473
let (normal, _) = self.drop_ladder(fields, succ, unwind);
474474
normal_blocks.push(normal);
475-
} else {
475+
} else if !adt.variants[variant_index].fields.is_empty() {
476476
have_otherwise = true;
477477
}
478478
}

0 commit comments

Comments
 (0)