Skip to content

Commit b183789

Browse files
author
Alexander Regueiro
committed
Made adjustments suggested by reviews.
1 parent a9305a1 commit b183789

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

src/librustc_codegen_ssa/base.rs

+5
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,10 @@ pub fn unsized_info<'tcx, 'a, Bx: BuilderMethods<'a, 'tcx>>(
161161
// Trait upcast
162162

163163
let source_ptr = old_info.expect("unsized_info: missing old info for trait upcast");
164+
// Only bother offsetting into the parent vtable if we are upcasting to a
165+
// non-auto trait.
164166
let target_ptr = if let Some(target_trait_ref) = target_data.principal() {
167+
// Find the offset of the supertrait's vtable within the subtrait (parent) vtable.
165168
let trait_ref = target_trait_ref.with_self_ty(tcx, source);
166169
let vtable = tcx.codegen_fulfill_obligation((ty::ParamEnv::reveal_all(), trait_ref));
167170
let offset = match vtable {
@@ -172,12 +175,14 @@ pub fn unsized_info<'tcx, 'a, Bx: BuilderMethods<'a, 'tcx>>(
172175
_ => bug!("unsized_info: unexpected vtable kind {:?}", vtable),
173176
};
174177

178+
// Ensure the pointer to the parent vtable has the right LLVM type.
175179
let vtable_layout = bx.cx().layout_of(tcx.mk_mut_ptr(source));
176180
let source_ptr = bx.pointercast(
177181
source_ptr,
178182
bx.cx().scalar_pair_element_backend_type(vtable_layout, 1, true),
179183
);
180184

185+
// Perform the offset within the parent vtable.
181186
bx.struct_gep(source_ptr, offset as u64)
182187
} else {
183188
source_ptr

src/librustc_middle/ty/sty.rs

-8
Original file line numberDiff line numberDiff line change
@@ -1881,14 +1881,6 @@ impl<'tcx> TyS<'tcx> {
18811881
}
18821882
}
18831883

1884-
/// Panics if called on any type other than `PhantomData<T>`.
1885-
pub fn phantom_ty(&self) -> Ty<'tcx> {
1886-
match self.kind {
1887-
Adt(def, substs) if def.is_phantom_data() => substs.type_at(0),
1888-
_ => bug!("`phantom_ty` is called on non-phantom-data type {:?}", self),
1889-
}
1890-
}
1891-
18921884
/// A scalar type is one that denotes an atomic datum, with no sub-components.
18931885
/// (A RawPtr is scalar because it represents a non-managed pointer, so its
18941886
/// contents are abstract to rustc.)

src/librustc_trait_selection/traits/select.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -2971,7 +2971,28 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
29712971
tcx.mk_dynamic(existential_predicates, region_b);
29722972

29732973
if tcx.features().trait_upcasting {
2974-
// Register obligations for `dyn TraitA1 [TraitA2...]: TraitB1 [TraitB2...]`.
2974+
// Given that we are upcasting `dyn (TraitA1 + ... + TraitAn)` to
2975+
// `dyn (TraitB1 + ... + TraitBn)`, register proof obligations like
2976+
//
2977+
// dyn (TraitA1 + ... + TraitAn): TraitB1
2978+
// ...
2979+
// dyn (TraitA1 + ... + TraitAn): TraitBn
2980+
//
2981+
// So, if for example we are upcasting `dyn (Foo + Send)` to `dyn (Bar + Send)`,
2982+
// then we would check that `dyn (Foo + Send): Bar` and `dyn (Foo + Send): Send`
2983+
// -- or at least we would, were it not for the slight pre-filter hack.
2984+
//
2985+
// The pre-filter hack removes cases where `TraitBi` is an auto-trait like
2986+
// `Send`, so long as we see that auto-trait in the A type. In our example, this
2987+
// would skip the dyn (Foo + Send): Send obligation. There are two reasons for
2988+
// this. The first is efficiency -- this case trivially holds. The second is
2989+
// because we would otherwise encounter ambiguity errors during bootstrap, owing
2990+
// to the `rustc_datastructures::sync::Send` trait (which is not the same as the
2991+
// standard `Send` trait). This trait has a `impl<T: ?Sized> Send for T` impl,
2992+
// and thus we get an error when trying to choose between this impl versus and
2993+
// the automatic impl that we prove from `dyn Traits`. This ambiguity is silly
2994+
// (it doesn't matter which one we choose), but rather than resolve that in the
2995+
// general case (which is subtle), we can screen it out here easily enough.
29752996
nested.extend(
29762997
data_b.iter()
29772998
// HACK(alexreg | nikomatsakis): we handle auto traits specially here

0 commit comments

Comments
 (0)