mirror of
https://github.com/rust-lang/rust.git
synced 2026-06-01 22:18:23 +03:00
avoid downcasting for enum variants without further fields
This commit is contained in:
committed by
Oliver Schneider
parent
a91ee4bb03
commit
fda5cc9b4c
@@ -561,13 +561,18 @@ pub(super) fn acquire_valid(&mut self, lvalue: Lvalue<'tcx>, ty: Ty<'tcx>, outer
|
||||
let variant_idx = adt.discriminants(self.tcx)
|
||||
.position(|variant_discr| variant_discr.to_u128_unchecked() == discr)
|
||||
.ok_or(EvalError::InvalidDiscriminant)?;
|
||||
|
||||
// Downcast to this variant
|
||||
let lvalue = self.eval_lvalue_projection(lvalue, ty, &mir::ProjectionElem::Downcast(adt, variant_idx))?;
|
||||
|
||||
// Recursively validate the fields
|
||||
let variant = &adt.variants[variant_idx];
|
||||
self.validate_variant(lvalue, ty, variant, subst, outer_mutbl)
|
||||
|
||||
if variant.fields.len() > 0 {
|
||||
// Downcast to this variant
|
||||
let lvalue = self.eval_lvalue_projection(lvalue, ty, &mir::ProjectionElem::Downcast(adt, variant_idx))?;
|
||||
|
||||
// Recursively validate the fields
|
||||
self.validate_variant(lvalue, ty, variant, subst, outer_mutbl)
|
||||
} else {
|
||||
// No fields, nothing left to check. Downcasting may fail, e.g. in case of a CEnum.
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
AdtKind::Struct => {
|
||||
self.validate_variant(lvalue, ty, adt.struct_variant(), subst, outer_mutbl)
|
||||
|
||||
Reference in New Issue
Block a user