Skip to content

Commit 0c7c94e

Browse files
committed
Auto merge of #30562 - nagisa:mir-unit-enums, r=luqmana
Thanks @arielb1! r? @luqmana
2 parents 3150ddd + 7b68b5f commit 0c7c94e

File tree

3 files changed

+36
-17
lines changed

3 files changed

+36
-17
lines changed

src/librustc_mir/hair/cx/expr.rs

+26-13
Original file line numberDiff line numberDiff line change
@@ -520,26 +520,39 @@ fn convert_path_expr<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>, expr: &'tcx hir::Expr)
520520
// Otherwise there may be def_map borrow conflicts
521521
let def = cx.tcx.def_map.borrow()[&expr.id].full_def();
522522
let (def_id, kind) = match def {
523-
// A variant constructor.
524-
def::DefVariant(_, def_id, false) => (def_id, ItemKind::Function),
525523
// A regular function.
526524
def::DefFn(def_id, _) => (def_id, ItemKind::Function),
527525
def::DefMethod(def_id) => (def_id, ItemKind::Method),
528-
def::DefStruct(def_id) => {
529-
match cx.tcx.node_id_to_type(expr.id).sty {
530-
// A tuple-struct constructor.
531-
ty::TyBareFn(..) => (def_id, ItemKind::Function),
532-
// This is a special case: a unit struct which is used as a value. We return a
533-
// completely different ExprKind here to account for this special case.
534-
ty::TyStruct(adt_def, substs) => return ExprKind::Adt {
526+
def::DefStruct(def_id) => match cx.tcx.node_id_to_type(expr.id).sty {
527+
// A tuple-struct constructor.
528+
ty::TyBareFn(..) => (def_id, ItemKind::Function),
529+
// This is a special case: a unit struct which is used as a value. We return a
530+
// completely different ExprKind here to account for this special case.
531+
ty::TyStruct(adt_def, substs) => return ExprKind::Adt {
532+
adt_def: adt_def,
533+
variant_index: 0,
534+
substs: substs,
535+
fields: vec![],
536+
base: None
537+
},
538+
ref sty => panic!("unexpected sty: {:?}", sty)
539+
},
540+
def::DefVariant(enum_id, variant_id, false) => match cx.tcx.node_id_to_type(expr.id).sty {
541+
// A variant constructor.
542+
ty::TyBareFn(..) => (variant_id, ItemKind::Function),
543+
// A unit variant, similar special case to the struct case above.
544+
ty::TyEnum(adt_def, substs) => {
545+
debug_assert!(adt_def.did == enum_id);
546+
let index = adt_def.variant_index_with_id(variant_id);
547+
return ExprKind::Adt {
535548
adt_def: adt_def,
536-
variant_index: 0,
537549
substs: substs,
550+
variant_index: index,
538551
fields: vec![],
539552
base: None
540-
},
541-
ref sty => panic!("unexpected sty: {:?}", sty)
542-
}
553+
};
554+
},
555+
ref sty => panic!("unexpected sty: {:?}", sty)
543556
},
544557
def::DefConst(def_id) |
545558
def::DefAssociatedConst(def_id) => {

src/librustc_trans/trans/mir/rvalue.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
9797

9898
mir::Rvalue::Aggregate(ref kind, ref operands) => {
9999
match *kind {
100-
// Unit struct, which is translated very differently compared to any other
101-
// aggregate
102-
mir::AggregateKind::Adt(adt_def, 0, _)
103-
if adt_def.struct_variant().kind() == ty::VariantKind::Unit => {
100+
// Unit struct or variant; both are translated very differently compared to any
101+
// other aggregate
102+
mir::AggregateKind::Adt(adt_def, index, _)
103+
if adt_def.variants[index].kind() == ty::VariantKind::Unit => {
104104
let repr = adt::represent_type(bcx.ccx(), dest.ty.to_ty(bcx.tcx()));
105105
adt::trans_set_discr(bcx, &*repr, dest.llval, 0);
106106
},

src/test/run-pass/mir_refs_correct.rs

+6
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,11 @@ fn t21() -> Unit {
182182
Unit
183183
}
184184

185+
#[rustc_mir]
186+
fn t22() -> Option<u8> {
187+
None
188+
}
189+
185190
fn main(){
186191
unsafe {
187192
assert_eq!(t1()(), regular());
@@ -222,5 +227,6 @@ fn main(){
222227
assert_eq!(t19()(322u64, 2u32), F::f(322u64, 2u32));
223228
assert_eq!(t20()(123u64, 38u32), <u32 as T<_, _>>::staticmeth(123, 38));
224229
assert_eq!(t21(), Unit);
230+
assert_eq!(t22(), None);
225231
}
226232
}

0 commit comments

Comments
 (0)