Skip to content

Commit 8205f73

Browse files
committed
auto merge of #6001 : jld/rust/enum-nullable, r=pcwalton
Specifically: all enums with two variants, where one has zero size (and thus at most one inhabitant) and the other has a field where the null value would not be allowed (such as a safe pointer), are now represented by storing a null pointer in the field in question. This is a generalization of representing `Option<~T>`, `Option<@t>`, and `Option<&T>` with nullable pointers, thus fixing Tony Hoare's “billion dollar mistake”.
2 parents aee2567 + edc1324 commit 8205f73

File tree

7 files changed

+328
-49
lines changed

7 files changed

+328
-49
lines changed

src/librustc/middle/trans/adt.rs

+150-36
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@
2929
* that might contain one and adjust GEP indices accordingly. See
3030
* issue #4578.
3131
*
32-
* - Rendering `Option<&T>` as a possibly-null `*T` instead of using
33-
* an extra word (and likewise for `@T` and `~T`). Can and probably
34-
* should also apply to any enum with one empty case and one case
35-
* starting with a non-null pointer (e.g., `Result<(), ~str>`).
36-
*
3732
* - Using smaller integer types for discriminants.
3833
*
3934
* - Store nested enums' discriminants in the same word. Rather, if
@@ -54,7 +49,8 @@ use core::libc::c_ulonglong;
5449
use core::option::{Option, Some, None};
5550
use core::vec;
5651

57-
use lib::llvm::{ValueRef, TypeRef, True};
52+
use lib::llvm::{ValueRef, TypeRef, True, IntEQ, IntNE};
53+
use lib::llvm::llvm::LLVMDumpValue;
5854
use middle::trans::_match;
5955
use middle::trans::build::*;
6056
use middle::trans::common::*;
@@ -81,7 +77,20 @@ pub enum Repr {
8177
* General-case enums: for each case there is a struct, and they
8278
* all start with a field for the discriminant.
8379
*/
84-
General(~[Struct])
80+
General(~[Struct]),
81+
/**
82+
* Two cases distinguished by a nullable pointer: the case with discriminant
83+
* `nndiscr` is represented by the struct `nonnull`, where the `ptrfield`th
84+
* field is known to be nonnull due to its type; if that field is null, then
85+
* it represents the other case, which is inhabited by at most one value
86+
* (and all other fields are undefined/unused).
87+
*
88+
* For example, `core::option::Option` instantiated at a safe pointer type
89+
* is represented such that `None` is a null pointer and `Some` is the
90+
* identity function.
91+
*/
92+
NullablePointer{ nonnull: Struct, nndiscr: int, ptrfield: uint,
93+
nullfields: ~[ty::t] }
8594
}
8695

8796
/// For structs, and struct-like parts of anything fancier.
@@ -108,9 +117,16 @@ pub fn represent_type(cx: @CrateContext, t: ty::t) -> @Repr {
108117
Some(repr) => return *repr,
109118
None => { }
110119
}
111-
let repr = @match ty::get(t).sty {
120+
let repr = @represent_type_uncached(cx, t);
121+
debug!("Represented as: %?", repr)
122+
cx.adt_reprs.insert(t, repr);
123+
return repr;
124+
}
125+
126+
fn represent_type_uncached(cx: @CrateContext, t: ty::t) -> Repr {
127+
match ty::get(t).sty {
112128
ty::ty_tup(ref elems) => {
113-
Univariant(mk_struct(cx, *elems, false), false)
129+
return Univariant(mk_struct(cx, *elems, false), false)
114130
}
115131
ty::ty_struct(def_id, ref substs) => {
116132
let fields = ty::lookup_struct_fields(cx.tcx, def_id);
@@ -121,45 +137,78 @@ pub fn represent_type(cx: @CrateContext, t: ty::t) -> @Repr {
121137
let dtor = ty::ty_dtor(cx.tcx, def_id).is_present();
122138
let ftys =
123139
if dtor { ftys + [ty::mk_bool(cx.tcx)] } else { ftys };
124-
Univariant(mk_struct(cx, ftys, packed), dtor)
140+
return Univariant(mk_struct(cx, ftys, packed), dtor)
125141
}
126142
ty::ty_enum(def_id, ref substs) => {
127143
struct Case { discr: int, tys: ~[ty::t] };
144+
impl Case {
145+
fn is_zerolen(&self, cx: @CrateContext) -> bool {
146+
mk_struct(cx, self.tys, false).size == 0
147+
}
148+
fn find_ptr(&self) -> Option<uint> {
149+
self.tys.position(|&ty| mono_data_classify(ty) == MonoNonNull)
150+
}
151+
}
128152

129153
let cases = do ty::enum_variants(cx.tcx, def_id).map |vi| {
130154
let arg_tys = do vi.args.map |&raw_ty| {
131155
ty::subst(cx.tcx, substs, raw_ty)
132156
};
133157
Case { discr: vi.disr_val, tys: arg_tys }
134158
};
159+
135160
if cases.len() == 0 {
136161
// Uninhabitable; represent as unit
137-
Univariant(mk_struct(cx, ~[], false), false)
138-
} else if cases.all(|c| c.tys.len() == 0) {
162+
return Univariant(mk_struct(cx, ~[], false), false);
163+
}
164+
165+
if cases.all(|c| c.tys.len() == 0) {
139166
// All bodies empty -> intlike
140167
let discrs = cases.map(|c| c.discr);
141-
CEnum(discrs.min(), discrs.max())
142-
} else if cases.len() == 1 {
168+
return CEnum(discrs.min(), discrs.max());
169+
}
170+
171+
if cases.len() == 1 {
143172
// Equivalent to a struct/tuple/newtype.
144173
assert!(cases[0].discr == 0);
145-
Univariant(mk_struct(cx, cases[0].tys, false), false)
146-
} else {
147-
// The general case. Since there's at least one
148-
// non-empty body, explicit discriminants should have
149-
// been rejected by a checker before this point.
150-
if !cases.alli(|i,c| c.discr == (i as int)) {
151-
cx.sess.bug(fmt!("non-C-like enum %s with specified \
152-
discriminants",
153-
ty::item_path_str(cx.tcx, def_id)))
174+
return Univariant(mk_struct(cx, cases[0].tys, false), false)
175+
}
176+
177+
// Since there's at least one
178+
// non-empty body, explicit discriminants should have
179+
// been rejected by a checker before this point.
180+
if !cases.alli(|i,c| c.discr == (i as int)) {
181+
cx.sess.bug(fmt!("non-C-like enum %s with specified \
182+
discriminants",
183+
ty::item_path_str(cx.tcx, def_id)))
184+
}
185+
186+
if cases.len() == 2 {
187+
let mut discr = 0;
188+
while discr < 2 {
189+
if cases[1 - discr].is_zerolen(cx) {
190+
match cases[discr].find_ptr() {
191+
Some(ptrfield) => {
192+
return NullablePointer {
193+
nndiscr: discr,
194+
nonnull: mk_struct(cx, cases[discr].tys, false),
195+
ptrfield: ptrfield,
196+
nullfields: copy cases[1 - discr].tys
197+
}
198+
}
199+
None => { }
200+
}
201+
}
202+
discr += 1;
154203
}
155-
let discr = ~[ty::mk_int(cx.tcx)];
156-
General(cases.map(|c| mk_struct(cx, discr + c.tys, false)))
157204
}
205+
206+
// The general case.
207+
let discr = ~[ty::mk_int(cx.tcx)];
208+
return General(cases.map(|c| mk_struct(cx, discr + c.tys, false)))
158209
}
159210
_ => cx.sess.bug(~"adt::represent_type called on non-ADT type")
160-
};
161-
cx.adt_reprs.insert(t, repr);
162-
return repr;
211+
}
163212
}
164213

165214
fn mk_struct(cx: @CrateContext, tys: &[ty::t], packed: bool) -> Struct {
@@ -190,6 +239,7 @@ fn generic_fields_of(cx: @CrateContext, r: &Repr, sizing: bool)
190239
match *r {
191240
CEnum(*) => ~[T_enum_discrim(cx)],
192241
Univariant(ref st, _dtor) => struct_llfields(cx, st, sizing),
242+
NullablePointer{ nonnull: ref st, _ } => struct_llfields(cx, st, sizing),
193243
General(ref sts) => {
194244
// To get "the" type of a general enum, we pick the case
195245
// with the largest alignment (so it will always align
@@ -239,23 +289,40 @@ pub fn trans_switch(bcx: block, r: &Repr, scrutinee: ValueRef)
239289
CEnum(*) | General(*) => {
240290
(_match::switch, Some(trans_get_discr(bcx, r, scrutinee)))
241291
}
292+
NullablePointer{ nonnull: ref nonnull, nndiscr, ptrfield, _ } => {
293+
(_match::switch, Some(nullable_bitdiscr(bcx, nonnull, nndiscr, ptrfield, scrutinee)))
294+
}
242295
Univariant(*) => {
243296
(_match::single, None)
244297
}
245298
}
246299
}
247300

301+
302+
248303
/// Obtain the actual discriminant of a value.
249304
pub fn trans_get_discr(bcx: block, r: &Repr, scrutinee: ValueRef)
250305
-> ValueRef {
251306
match *r {
252307
CEnum(min, max) => load_discr(bcx, scrutinee, min, max),
253308
Univariant(*) => C_int(bcx.ccx(), 0),
254309
General(ref cases) => load_discr(bcx, scrutinee, 0,
255-
(cases.len() - 1) as int)
310+
(cases.len() - 1) as int),
311+
NullablePointer{ nonnull: ref nonnull, nndiscr, ptrfield, _ } => {
312+
ZExt(bcx, nullable_bitdiscr(bcx, nonnull, nndiscr, ptrfield, scrutinee),
313+
T_enum_discrim(bcx.ccx()))
314+
}
256315
}
257316
}
258317

318+
fn nullable_bitdiscr(bcx: block, nonnull: &Struct, nndiscr: int, ptrfield: uint,
319+
scrutinee: ValueRef) -> ValueRef {
320+
let cmp = if nndiscr == 0 { IntEQ } else { IntNE };
321+
let llptr = Load(bcx, GEPi(bcx, scrutinee, [0, ptrfield]));
322+
let llptrty = type_of::type_of(bcx.ccx(), nonnull.fields[ptrfield]);
323+
ICmp(bcx, cmp, llptr, C_null(llptrty))
324+
}
325+
259326
/// Helper for cases where the discriminant is simply loaded.
260327
fn load_discr(bcx: block, scrutinee: ValueRef, min: int, max: int)
261328
-> ValueRef {
@@ -286,12 +353,16 @@ pub fn trans_case(bcx: block, r: &Repr, discr: int) -> _match::opt_result {
286353
CEnum(*) => {
287354
_match::single_result(rslt(bcx, C_int(bcx.ccx(), discr)))
288355
}
289-
Univariant(*)=> {
356+
Univariant(*) => {
290357
bcx.ccx().sess.bug(~"no cases for univariants or structs")
291358
}
292359
General(*) => {
293360
_match::single_result(rslt(bcx, C_int(bcx.ccx(), discr)))
294361
}
362+
NullablePointer{ _ } => {
363+
assert!(discr == 0 || discr == 1);
364+
_match::single_result(rslt(bcx, C_i1(discr != 0)))
365+
}
295366
}
296367
}
297368

@@ -317,6 +388,13 @@ pub fn trans_start_init(bcx: block, r: &Repr, val: ValueRef, discr: int) {
317388
General(*) => {
318389
Store(bcx, C_int(bcx.ccx(), discr), GEPi(bcx, val, [0, 0]))
319390
}
391+
NullablePointer{ nonnull: ref nonnull, nndiscr, ptrfield, _ } => {
392+
if discr != nndiscr {
393+
let llptrptr = GEPi(bcx, val, [0, ptrfield]);
394+
let llptrty = type_of::type_of(bcx.ccx(), nonnull.fields[ptrfield]);
395+
Store(bcx, C_null(llptrty), llptrptr)
396+
}
397+
}
320398
}
321399
}
322400

@@ -331,7 +409,10 @@ pub fn num_args(r: &Repr, discr: int) -> uint {
331409
assert!(discr == 0);
332410
st.fields.len() - (if dtor { 1 } else { 0 })
333411
}
334-
General(ref cases) => cases[discr as uint].fields.len() - 1
412+
General(ref cases) => cases[discr as uint].fields.len() - 1,
413+
NullablePointer{ nonnull: ref nonnull, nndiscr, _ } => {
414+
if discr == nndiscr { nonnull.fields.len() } else { 0 }
415+
}
335416
}
336417
}
337418

@@ -352,6 +433,19 @@ pub fn trans_field_ptr(bcx: block, r: &Repr, val: ValueRef, discr: int,
352433
General(ref cases) => {
353434
struct_field_ptr(bcx, &cases[discr as uint], val, ix + 1, true)
354435
}
436+
NullablePointer{ nonnull: ref nonnull, nullfields: ref nullfields, nndiscr, _ } => {
437+
if (discr == nndiscr) {
438+
struct_field_ptr(bcx, nonnull, val, ix, false)
439+
} else {
440+
// The unit-like case might have a nonzero number of unit-like fields.
441+
// (e.g., Result or Either with () as one side.)
442+
let llty = type_of::type_of(bcx.ccx(), nullfields[ix]);
443+
assert!(machine::llsize_of_alloc(bcx.ccx(), llty) == 0);
444+
// The contents of memory at this pointer can't matter, but use
445+
// the value that's "reasonable" in case of pointer comparison.
446+
PointerCast(bcx, val, T_ptr(llty))
447+
}
448+
}
355449
}
356450
}
357451

@@ -420,6 +514,18 @@ pub fn trans_const(ccx: @CrateContext, r: &Repr, discr: int,
420514
~[C_int(ccx, discr)] + vals);
421515
C_struct(contents + [padding(max_sz - case.size)])
422516
}
517+
NullablePointer{ nonnull: ref nonnull, nndiscr, ptrfield, _ } => {
518+
if discr == nndiscr {
519+
C_struct(build_const_struct(ccx, nonnull, vals))
520+
} else {
521+
assert!(vals.len() == 0);
522+
let vals = do nonnull.fields.mapi |i, &ty| {
523+
let llty = type_of::sizing_type_of(ccx, ty);
524+
if i == ptrfield { C_null(llty) } else { C_undef(llty) }
525+
};
526+
C_struct(build_const_struct(ccx, nonnull, vals))
527+
}
528+
}
423529
}
424530
}
425531

@@ -451,10 +557,14 @@ fn build_const_struct(ccx: @CrateContext, st: &Struct, vals: &[ValueRef])
451557
cfields.push(padding(target_offset - offset));
452558
offset = target_offset;
453559
}
454-
assert!(!is_undef(vals[i]));
455-
// If that assert fails, could change it to wrap in a struct?
456-
// (See `const_struct_field` for why real fields must not be undef.)
457-
cfields.push(vals[i]);
560+
let val = if is_undef(vals[i]) {
561+
let wrapped = C_struct([vals[i]]);
562+
assert!(!is_undef(wrapped));
563+
wrapped
564+
} else {
565+
vals[i]
566+
};
567+
cfields.push(val);
458568
}
459569

460570
return cfields;
@@ -475,6 +585,9 @@ pub fn const_get_discrim(ccx: @CrateContext, r: &Repr, val: ValueRef)
475585
CEnum(*) => const_to_int(val) as int,
476586
Univariant(*) => 0,
477587
General(*) => const_to_int(const_get_elt(ccx, val, [0])) as int,
588+
NullablePointer{ nndiscr, ptrfield, _ } => {
589+
if is_null(const_struct_field(ccx, val, ptrfield)) { 1 - nndiscr } else { nndiscr }
590+
}
478591
}
479592
}
480593

@@ -490,7 +603,8 @@ pub fn const_get_field(ccx: @CrateContext, r: &Repr, val: ValueRef,
490603
match *r {
491604
CEnum(*) => ccx.sess.bug(~"element access in C-like enum const"),
492605
Univariant(*) => const_struct_field(ccx, val, ix),
493-
General(*) => const_struct_field(ccx, val, ix + 1)
606+
General(*) => const_struct_field(ccx, val, ix + 1),
607+
NullablePointer{ _ } => const_struct_field(ccx, val, ix)
494608
}
495609
}
496610

src/librustc/middle/trans/base.rs

+5
Original file line numberDiff line numberDiff line change
@@ -2007,6 +2007,11 @@ pub fn trans_enum_variant(ccx: @CrateContext,
20072007
// XXX is there a better way to reconstruct the ty::t?
20082008
let repr = adt::represent_type(ccx, enum_ty);
20092009

2010+
debug!("trans_enum_variant: name=%s tps=%s repr=%? enum_ty=%s",
2011+
unsafe { str::raw::from_c_str(llvm::LLVMGetValueName(llfndecl)) },
2012+
~"[" + str::connect(ty_param_substs.map(|&t| ty_to_str(ccx.tcx, t)), ", ") + ~"]",
2013+
repr, ty_to_str(ccx.tcx, enum_ty));
2014+
20102015
adt::trans_start_init(bcx, repr, fcx.llretptr.get(), disr);
20112016
for vec::eachi(args) |i, va| {
20122017
let lldestptr = adt::trans_field_ptr(bcx,

0 commit comments

Comments
 (0)