Skip to content

Commit

Permalink
librustc: Add NonZero lang item and use it if possible for nullable p…
Browse files Browse the repository at this point in the history
…ointer enum opt.
  • Loading branch information
luqmana committed Dec 29, 2014
1 parent 6d91419 commit 46e7376
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/librustc/middle/lang_items.rs
Expand Up @@ -327,6 +327,8 @@ lets_do_this! {
NoSyncItem, "no_sync_bound", no_sync_bound;
ManagedItem, "managed_bound", managed_bound;

NonZeroItem, "non_zero", non_zero;

IteratorItem, "iterator", iterator;

StackExhaustedLangItem, "stack_exhausted", stack_exhausted;
Expand Down
14 changes: 13 additions & 1 deletion src/librustc_trans/trans/adt.rs
Expand Up @@ -357,7 +357,19 @@ fn find_discr_field_candidate<'tcx>(tcx: &ty::ctxt<'tcx>, ty: Ty<'tcx>) -> Optio
// Closures are a pair of pointers: the code and environment
ty::ty_closure(..) => Some(vec![FAT_PTR_ADDR]),

// Perhaps one of the fields of this struct is non-null
// Is this the NonZero lang item wrapping a pointer or integer type?
ty::ty_struct(did, ref substs) if Some(did) == tcx.lang_items.non_zero() => {
let nonzero_fields = ty::lookup_struct_fields(tcx, did);
assert_eq!(nonzero_fields.len(), 1);
let nonzero_field = ty::lookup_field_type(tcx, did, nonzero_fields[0].id, substs);
match nonzero_field.sty {
ty::ty_ptr(..) | ty::ty_int(..) |
ty::ty_uint(..) => Some(vec![0]),
_ => None
}
},

// Perhaps one of the fields of this struct is non-zero
// let's recurse and find out
ty::ty_struct(def_id, ref substs) => {
let fields = ty::lookup_struct_fields(tcx, def_id);
Expand Down

0 comments on commit 46e7376

Please sign in to comment.