Skip to content

Commit a5d39cf

Browse files
review comment: Deduplicate dyn ty_and_layout_field code
1 parent 2011316 commit a5d39cf

File tree

1 file changed

+19
-36
lines changed

1 file changed

+19
-36
lines changed

compiler/rustc_middle/src/ty/layout.rs

+19-36
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,23 @@ where
670670
});
671671
}
672672

673+
let mk_dyn_vtable = || {
674+
tcx.mk_imm_ref(tcx.lifetimes.re_static, tcx.mk_array(tcx.types.usize, 3))
675+
/* FIXME: use actual fn pointers
676+
Warning: naively computing the number of entries in the
677+
vtable by counting the methods on the trait + methods on
678+
all parent traits does not work, because some methods can
679+
be not object safe and thus excluded from the vtable.
680+
Increase this counter if you tried to implement this but
681+
failed to do it without duplicating a lot of code from
682+
other places in the compiler: 2
683+
tcx.mk_tup(&[
684+
tcx.mk_array(tcx.types.usize, 3),
685+
tcx.mk_array(Option<fn()>),
686+
])
687+
*/
688+
};
689+
673690
let metadata = if let Some(metadata_def_id) = tcx.lang_items().metadata_type() {
674691
let metadata = tcx.normalize_erasing_regions(
675692
cx.param_env(),
@@ -684,48 +701,14 @@ where
684701
&& Some(def.did()) == tcx.lang_items().dyn_metadata()
685702
&& substs.type_at(0).is_trait()
686703
{
687-
tcx.mk_imm_ref(
688-
tcx.lifetimes.re_static,
689-
tcx.mk_array(tcx.types.usize, 3),
690-
)
691-
/* FIXME: use actual fn pointers
692-
Warning: naively computing the number of entries in the
693-
vtable by counting the methods on the trait + methods on
694-
all parent traits does not work, because some methods can
695-
be not object safe and thus excluded from the vtable.
696-
Increase this counter if you tried to implement this but
697-
failed to do it without duplicating a lot of code from
698-
other places in the compiler: 2
699-
tcx.mk_tup(&[
700-
tcx.mk_array(tcx.types.usize, 3),
701-
tcx.mk_array(Option<fn()>),
702-
])
703-
*/
704+
mk_dyn_vtable()
704705
} else {
705706
metadata
706707
}
707708
} else {
708709
match tcx.struct_tail_erasing_lifetimes(pointee, cx.param_env()).kind() {
709710
ty::Slice(_) | ty::Str => tcx.types.usize,
710-
ty::Dynamic(_, _, ty::Dyn) => {
711-
tcx.mk_imm_ref(
712-
tcx.lifetimes.re_static,
713-
tcx.mk_array(tcx.types.usize, 3),
714-
)
715-
/* FIXME: use actual fn pointers
716-
Warning: naively computing the number of entries in the
717-
vtable by counting the methods on the trait + methods on
718-
all parent traits does not work, because some methods can
719-
be not object safe and thus excluded from the vtable.
720-
Increase this counter if you tried to implement this but
721-
failed to do it without duplicating a lot of code from
722-
other places in the compiler: 2
723-
tcx.mk_tup(&[
724-
tcx.mk_array(tcx.types.usize, 3),
725-
tcx.mk_array(Option<fn()>),
726-
])
727-
*/
728-
}
711+
ty::Dynamic(_, _, ty::Dyn) => mk_dyn_vtable(),
729712
_ => bug!("TyAndLayout::field({:?}): not applicable", this),
730713
}
731714
};

0 commit comments

Comments
 (0)