Skip to content

Commit c533f96

Browse files
arielb1Ariel Ben-Yehuda
authored and
Ariel Ben-Yehuda
committed
handle associated types correctly in null pointer optimization
Fixes #27532 Thanks @eefriedman for the test.
1 parent 3494233 commit c533f96

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/librustc_trans/trans/adt.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,7 @@ fn find_discr_field_candidate<'tcx>(tcx: &ty::ctxt<'tcx>,
462462
// let's recurse and find out
463463
ty::TyStruct(def, substs) => {
464464
for (j, field) in def.struct_variant().fields.iter().enumerate() {
465-
// TODO(#27532)
466-
let field_ty = field.ty(tcx, substs);
465+
let field_ty = monomorphize::field_ty(tcx, substs, field);
467466
if let Some(mut fpath) = find_discr_field_candidate(tcx, field_ty, path.clone()) {
468467
fpath.push(j);
469468
return Some(fpath);

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

+7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ use std::rc::Rc;
1818
use std::sync::Arc;
1919

2020
trait Trait { fn dummy(&self) { } }
21+
trait Mirror { type Image; }
22+
impl<T> Mirror for T { type Image = T; }
23+
struct ParamTypeStruct<T>(T);
24+
struct AssocTypeStruct<T>(<T as Mirror>::Image);
2125

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

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

0 commit comments

Comments
 (0)