Skip to content

Commit 81e8137

Browse files
Inline trans_switch to simplify code
1 parent 426c558 commit 81e8137

File tree

2 files changed

+13
-33
lines changed

2 files changed

+13
-33
lines changed

src/librustc_trans/adt.rs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,6 @@ use monomorphize;
5656
use type_::Type;
5757
use type_of;
5858

59-
#[derive(Copy, Clone, PartialEq)]
60-
pub enum BranchKind {
61-
Switch,
62-
Single
63-
}
64-
6559
/// Given an enum, struct, closure, or tuple, extracts fields.
6660
/// Treats closures as a struct with one variant.
6761
/// `empty_if_no_variants` is a switch to deal with empty enums.
@@ -273,28 +267,6 @@ fn struct_llfields<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, fields: &Vec<Ty<'tcx>>
273267
}
274268
}
275269

276-
/// Obtain a representation of the discriminant sufficient to translate
277-
/// destructuring; this may or may not involve the actual discriminant.
278-
pub fn trans_switch<'a, 'tcx>(
279-
bcx: &Builder<'a, 'tcx>,
280-
t: Ty<'tcx>,
281-
scrutinee: ValueRef,
282-
range_assert: bool
283-
) -> (BranchKind, Option<ValueRef>) {
284-
let l = bcx.ccx.layout_of(t);
285-
match *l {
286-
layout::CEnum { .. } | layout::General { .. } |
287-
layout::RawNullablePointer { .. } | layout::StructWrappedNullablePointer { .. } => {
288-
(BranchKind::Switch, Some(trans_get_discr(bcx, t, scrutinee, None, range_assert)))
289-
}
290-
layout::Univariant { .. } | layout::UntaggedUnion { .. } => {
291-
// N.B.: Univariant means <= 1 enum variants (*not* == 1 variants).
292-
(BranchKind::Single, None)
293-
},
294-
_ => bug!("{} is not an enum.", t)
295-
}
296-
}
297-
298270
pub fn is_discr_signed<'tcx>(l: &layout::Layout) -> bool {
299271
match *l {
300272
layout::CEnum { signed, .. }=> signed,

src/librustc_trans/glue.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use llvm::{ValueRef, get_param};
2020
use middle::lang_items::BoxFreeFnLangItem;
2121
use rustc::ty::subst::{Substs};
2222
use rustc::traits;
23-
use rustc::ty::{self, AdtDef, AdtKind, Ty, TypeFoldable};
23+
use rustc::ty::{self, layout, AdtDef, AdtKind, Ty, TypeFoldable};
2424
use rustc::ty::subst::Kind;
2525
use rustc::mir::tcx::LvalueTy;
2626
use mir::lvalue::LvalueRef;
@@ -471,14 +471,22 @@ fn drop_structural_ty<'a, 'tcx>(cx: Builder<'a, 'tcx>, ptr: LvalueRef<'tcx>) ->
471471
// NB: we must hit the discriminant first so that structural
472472
// comparison know not to proceed when the discriminants differ.
473473

474-
match adt::trans_switch(&cx, t, ptr.llval, false) {
475-
(adt::BranchKind::Single, None) => {
474+
// Obtain a representation of the discriminant sufficient to translate
475+
// destructuring; this may or may not involve the actual discriminant.
476+
let l = cx.ccx.layout_of(t);
477+
match *l {
478+
layout::Univariant { .. } |
479+
layout::UntaggedUnion { .. } => {
476480
if n_variants != 0 {
477481
assert!(n_variants == 1);
478482
iter_variant(&cx, ptr, &adt, 0, substs);
479483
}
480484
}
481-
(adt::BranchKind::Switch, Some(lldiscrim_a)) => {
485+
layout::CEnum { .. } |
486+
layout::General { .. } |
487+
layout::RawNullablePointer { .. } |
488+
layout::StructWrappedNullablePointer { .. } => {
489+
let lldiscrim_a = adt::trans_get_discr(&cx, t, ptr.llval, None, false);
482490
let tcx = cx.tcx();
483491
drop_ty(&cx, LvalueRef::new_sized_ty(lldiscrim_a, tcx.types.isize));
484492

@@ -511,7 +519,7 @@ fn drop_structural_ty<'a, 'tcx>(cx: Builder<'a, 'tcx>, ptr: LvalueRef<'tcx>) ->
511519
}
512520
cx = next_cx;
513521
}
514-
_ => cx.sess().unimpl("value from adt::trans_switch in drop_structural_ty"),
522+
_ => bug!("{} is not an enum.", t),
515523
}
516524
}
517525
},

0 commit comments

Comments
 (0)