Skip to content

Commit 46e7376

Browse files
committed
librustc: Add NonZero lang item and use it if possible for nullable pointer enum opt.
1 parent 6d91419 commit 46e7376

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/librustc/middle/lang_items.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,8 @@ lets_do_this! {
327327
NoSyncItem, "no_sync_bound", no_sync_bound;
328328
ManagedItem, "managed_bound", managed_bound;
329329

330+
NonZeroItem, "non_zero", non_zero;
331+
330332
IteratorItem, "iterator", iterator;
331333

332334
StackExhaustedLangItem, "stack_exhausted", stack_exhausted;

src/librustc_trans/trans/adt.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,19 @@ fn find_discr_field_candidate<'tcx>(tcx: &ty::ctxt<'tcx>, ty: Ty<'tcx>) -> Optio
357357
// Closures are a pair of pointers: the code and environment
358358
ty::ty_closure(..) => Some(vec![FAT_PTR_ADDR]),
359359

360-
// Perhaps one of the fields of this struct is non-null
360+
// Is this the NonZero lang item wrapping a pointer or integer type?
361+
ty::ty_struct(did, ref substs) if Some(did) == tcx.lang_items.non_zero() => {
362+
let nonzero_fields = ty::lookup_struct_fields(tcx, did);
363+
assert_eq!(nonzero_fields.len(), 1);
364+
let nonzero_field = ty::lookup_field_type(tcx, did, nonzero_fields[0].id, substs);
365+
match nonzero_field.sty {
366+
ty::ty_ptr(..) | ty::ty_int(..) |
367+
ty::ty_uint(..) => Some(vec![0]),
368+
_ => None
369+
}
370+
},
371+
372+
// Perhaps one of the fields of this struct is non-zero
361373
// let's recurse and find out
362374
ty::ty_struct(def_id, ref substs) => {
363375
let fields = ty::lookup_struct_fields(tcx, def_id);

0 commit comments

Comments
 (0)