Skip to content

Commit 6840b48

Browse files
committed
trans_cast_to_int is hard to explain; make it trans_get_discr instead.
1 parent 8dca7be commit 6840b48

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
@@ -231,39 +231,36 @@ fn generic_fields_of(cx: @CrateContext, r: &Repr, sizing: bool)
231231
}
232232

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

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

263+
/// Helper for cases where the discriminant is simply loaded.
267264
fn load_discr(bcx: block, scrutinee: ValueRef, min: int, max: int)
268265
-> ValueRef {
269266
let ptr = GEPi(bcx, scrutinee, [0, 0]);
@@ -285,6 +282,7 @@ fn load_discr(bcx: block, scrutinee: ValueRef, min: int, max: int)
285282
/**
286283
* Yield information about how to dispatch a case of the
287284
* discriminant-like value returned by `trans_switch`.
285+
*
288286
* This should ideally be less tightly tied to `_match`.
289287
*/
290288
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
@@ -1645,7 +1645,7 @@ fn trans_imm_cast(bcx: block, expr: @ast::expr,
16451645
(cast_enum, cast_float) => {
16461646
let bcx = bcx;
16471647
let repr = adt::represent_type(ccx, t_in);
1648-
let lldiscrim_a = adt::trans_cast_to_int(bcx, repr, llexpr);
1648+
let lldiscrim_a = adt::trans_get_discr(bcx, repr, llexpr);
16491649
match k_out {
16501650
cast_integral => int_cast(bcx, ll_t_out,
16511651
val_ty(lldiscrim_a),

0 commit comments

Comments
 (0)