Skip to content

Commit

Permalink
handle associated types correctly in null pointer optimization
Browse files Browse the repository at this point in the history
Fixes #27532

Thanks @eefriedman for the test.
  • Loading branch information
arielb1 authored and Ariel Ben-Yehuda committed Aug 6, 2015
1 parent 3494233 commit c533f96
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/librustc_trans/trans/adt.rs
Expand Up @@ -462,8 +462,7 @@ fn find_discr_field_candidate<'tcx>(tcx: &ty::ctxt<'tcx>,
// let's recurse and find out
ty::TyStruct(def, substs) => {
for (j, field) in def.struct_variant().fields.iter().enumerate() {
// TODO(#27532)
let field_ty = field.ty(tcx, substs);
let field_ty = monomorphize::field_ty(tcx, substs, field);
if let Some(mut fpath) = find_discr_field_candidate(tcx, field_ty, path.clone()) {
fpath.push(j);
return Some(fpath);
Expand Down
7 changes: 7 additions & 0 deletions src/test/run-pass/enum-null-pointer-opt.rs
Expand Up @@ -18,6 +18,10 @@ use std::rc::Rc;
use std::sync::Arc;

trait Trait { fn dummy(&self) { } }
trait Mirror { type Image; }
impl<T> Mirror for T { type Image = T; }
struct ParamTypeStruct<T>(T);
struct AssocTypeStruct<T>(<T as Mirror>::Image);

fn main() {
// Functions
Expand Down Expand Up @@ -66,4 +70,7 @@ fn main() {
// Should apply to types that have NonZero transitively
assert_eq!(size_of::<String>(), size_of::<Option<String>>());

// Should apply to types where the pointer is substituted
assert_eq!(size_of::<&u8>(), size_of::<Option<ParamTypeStruct<&u8>>>());
assert_eq!(size_of::<&u8>(), size_of::<Option<AssocTypeStruct<&u8>>>());
}

0 comments on commit c533f96

Please sign in to comment.