]> git.lizzy.rs Git - rust.git/commitdiff
handle associated types correctly in null pointer optimization
authorAriel Ben-Yehuda <ariel.byd@gmail.com>
Thu, 6 Aug 2015 14:00:41 +0000 (17:00 +0300)
committerAriel Ben-Yehuda <arielb1@mail.tau.ac.il>
Thu, 6 Aug 2015 14:31:11 +0000 (17:31 +0300)
Fixes #27532

Thanks @eefriedman for the test.

src/librustc_trans/trans/adt.rs
src/test/run-pass/enum-null-pointer-opt.rs

index 9fb808a7d2d9340d8b7e35daddbfe45435ea0109..ec6823e7622550cb7378655587d2ae8de08fdbea 100644 (file)
@@ -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);
index dd88dc11ea70e47a11dd25b8c11970f4716cac23..e296aff2782c53a1480e30b6c4ef625d321dc271 100644 (file)
 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
@@ -66,4 +70,7 @@ struct Foo {
     // 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>>>());
 }