Skip to content

Commit 5fb8576

Browse files
committed
librustc: Add NonZero lang item and use it if possible for nullable pointer enum opt.
1 parent eaeb425 commit 5fb8576

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
@@ -308,6 +308,8 @@ lets_do_this! {
308308
NoSyncItem, "no_sync_bound", no_sync_bound;
309309
ManagedItem, "managed_bound", managed_bound;
310310

311+
NonZeroItem, "non_zero", non_zero;
312+
311313
IteratorItem, "iterator", iterator;
312314

313315
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
@@ -305,7 +305,19 @@ fn find_discr_field_candidate<'tcx>(tcx: &ty::ctxt<'tcx>, ty: Ty<'tcx>) -> Optio
305305
// Closures are a pair of pointers: the code and environment
306306
ty::ty_closure(..) => Some(vec![FAT_PTR_ADDR]),
307307

308-
// Perhaps one of the fields of this struct is non-null
308+
// Is this the NonZero lang item wrapping a pointer or integer type?
309+
ty::ty_struct(did, ref substs) if Some(did) == tcx.lang_items.non_zero() => {
310+
let nonzero_fields = ty::lookup_struct_fields(tcx, did);
311+
assert_eq!(nonzero_fields.len(), 1);
312+
let nonzero_field = ty::lookup_field_type(tcx, did, nonzero_fields[0].id, substs);
313+
match nonzero_field.sty {
314+
ty::ty_ptr(..) | ty::ty_int(..) |
315+
ty::ty_uint(..) => Some(vec![0]),
316+
_ => None
317+
}
318+
},
319+
320+
// Perhaps one of the fields of this struct is non-zero
309321
// let's recurse and find out
310322
ty::ty_struct(def_id, ref substs) => {
311323
let fields = ty::lookup_struct_fields(tcx, def_id);

0 commit comments

Comments
 (0)