Skip to content

Commit c4ae13b

Browse files
author
Alexander Regueiro
committed
Made adjustments suggested by reviews.
1 parent 794c4e8 commit c4ae13b

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

src/librustc_codegen_ssa/base.rs

Lines changed: 5 additions & 0 deletions
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

Lines changed: 0 additions & 8 deletions
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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2970,7 +2970,28 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
29702970
tcx.mk_dynamic(existential_predicates, region_b);
29712971

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

0 commit comments

Comments
 (0)