Skip to content

Commit 9e779c6

Browse files
committed
trans_cast_to_int is hard to explain; make it trans_get_discr instead.
1 parent cafebfe commit 9e779c6

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

src/librustc/middle/trans/adt.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -230,39 +230,36 @@ fn generic_fields_of(cx: @CrateContext, r: &Repr, sizing: bool)
230230
}
231231

232232
/**
233-
* Obtain as much of a "discriminant" as this representation has.
233+
* Obtain a representation of the discriminant sufficient to translate
234+
* destructuring; this may or may not involve the actual discriminant.
235+
*
234236
* This should ideally be less tightly tied to `_match`.
235237
*/
236238
pub fn trans_switch(bcx: block, r: &Repr, scrutinee: ValueRef)
237239
-> (_match::branch_kind, Option<ValueRef>) {
238240
match *r {
239241
CEnum(*) | General(*) => {
240-
(_match::switch, Some(trans_cast_to_int(bcx, r, scrutinee)))
242+
(_match::switch, Some(trans_get_discr(bcx, r, scrutinee)))
241243
}
242244
Unit(*) | Univariant(*) => {
243245
(_match::single, None)
244246
}
245247
}
246248
}
247249

248-
/**
249-
* If the representation is potentially of a C-like enum, implement
250-
* coercion to numeric types.
251-
*/
252-
pub fn trans_cast_to_int(bcx: block, r: &Repr, scrutinee: ValueRef)
250+
/// Obtain the actual discriminant of a value.
251+
pub fn trans_get_discr(bcx: block, r: &Repr, scrutinee: ValueRef)
253252
-> ValueRef {
254253
match *r {
255254
Unit(the_disc) => C_int(bcx.ccx(), the_disc),
256255
CEnum(min, max) => load_discr(bcx, scrutinee, min, max),
257-
Univariant(*) => bcx.ccx().sess.bug(~"type has no explicit \
258-
discriminant"),
259-
// Note: this case is used internally by trans_switch,
260-
// even though it shouldn't be reached by an external caller.
256+
Univariant(*) => C_int(bcx.ccx(), 0),
261257
General(ref cases) => load_discr(bcx, scrutinee, 0,
262258
(cases.len() - 1) as int)
263259
}
264260
}
265261

262+
/// Helper for cases where the discriminant is simply loaded.
266263
fn load_discr(bcx: block, scrutinee: ValueRef, min: int, max: int)
267264
-> ValueRef {
268265
let ptr = GEPi(bcx, scrutinee, [0, 0]);
@@ -284,6 +281,7 @@ fn load_discr(bcx: block, scrutinee: ValueRef, min: int, max: int)
284281
/**
285282
* Yield information about how to dispatch a case of the
286283
* discriminant-like value returned by `trans_switch`.
284+
*
287285
* This should ideally be less tightly tied to `_match`.
288286
*/
289287
pub fn trans_case(bcx: block, r: &Repr, discr: int) -> _match::opt_result {

src/librustc/middle/trans/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1597,7 +1597,7 @@ fn trans_imm_cast(bcx: block, expr: @ast::expr,
15971597
(cast_enum, cast_float) => {
15981598
let bcx = bcx;
15991599
let repr = adt::represent_type(ccx, t_in);
1600-
let lldiscrim_a = adt::trans_cast_to_int(bcx, repr, llexpr);
1600+
let lldiscrim_a = adt::trans_get_discr(bcx, repr, llexpr);
16011601
match k_out {
16021602
cast_integral => int_cast(bcx, ll_t_out,
16031603
val_ty(lldiscrim_a),

0 commit comments

Comments
 (0)