Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 6c01273

Browse files
committed
Plumb dyn trait representation through ty::Dynamic
1 parent eff35e5 commit 6c01273

File tree

29 files changed

+110
-57
lines changed

29 files changed

+110
-57
lines changed

compiler/rustc_codegen_ssa/src/meth.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl<'a, 'tcx> VirtualIndex {
6969
fn expect_dyn_trait_in_self<'tcx>(ty: Ty<'tcx>) -> ty::PolyExistentialTraitRef<'tcx> {
7070
for arg in ty.peel_refs().walk() {
7171
if let GenericArgKind::Type(ty) = arg.unpack() {
72-
if let ty::Dynamic(data, _) = ty.kind() {
72+
if let ty::Dynamic(data, _, _) = ty.kind() {
7373
return data.principal().expect("expected principal trait object");
7474
}
7575
}

compiler/rustc_const_eval/src/interpret/cast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
312312
let new_vptr = self.get_vtable_ptr(ty, data_b.principal())?;
313313
self.write_immediate(Immediate::new_dyn_trait(old_data, new_vptr, self), dest)
314314
}
315-
(_, &ty::Dynamic(ref data, _)) => {
315+
(_, &ty::Dynamic(ref data, _, _repr)) => {
316316
// Initial cast from sized to dyn trait
317317
let vtable = self.get_vtable_ptr(src_pointee_ty, data.principal())?;
318318
let ptr = self.read_scalar(src)?;

compiler/rustc_const_eval/src/interpret/intrinsics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ pub(crate) fn eval_nullary_intrinsic<'tcx>(
9595
| ty::Ref(_, _, _)
9696
| ty::FnDef(_, _)
9797
| ty::FnPtr(_)
98-
| ty::Dynamic(_, _)
98+
| ty::Dynamic(_, _, _)
9999
| ty::Closure(_, _)
100100
| ty::Generator(_, _, _)
101101
| ty::GeneratorWitness(_)

compiler/rustc_const_eval/src/interpret/intrinsics/type_name.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
4848
| ty::FnPtr(_)
4949
| ty::Never
5050
| ty::Tuple(_)
51-
| ty::Dynamic(_, _) => self.pretty_print_type(ty),
51+
| ty::Dynamic(_, _, _) => self.pretty_print_type(ty),
5252

5353
// Placeholders (all printed as `_` to uniformize them).
5454
ty::Param(_) | ty::Bound(..) | ty::Placeholder(_) | ty::Infer(_) | ty::Error(_) => {

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ pub struct TraitObjectVisitor(pub FxHashSet<DefId>);
544544
impl<'tcx> TypeVisitor<'tcx> for TraitObjectVisitor {
545545
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
546546
match t.kind() {
547-
ty::Dynamic(preds, re) if re.is_static() => {
547+
ty::Dynamic(preds, re, _) if re.is_static() => {
548548
if let Some(def_id) = preds.principal_def_id() {
549549
self.0.insert(def_id);
550550
}

compiler/rustc_lint/src/unused.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
222222
}
223223
has_emitted
224224
}
225-
ty::Dynamic(binder, _) => {
225+
ty::Dynamic(binder, _, _) => {
226226
let mut has_emitted = false;
227227
for predicate in binder.iter() {
228228
if let ty::ExistentialPredicate::Trait(ref trait_ref) =

compiler/rustc_middle/src/ty/context.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ use rustc_span::{Span, DUMMY_SP};
6363
use rustc_target::abi::{Layout, LayoutS, TargetDataLayout, VariantIdx};
6464
use rustc_target::spec::abi;
6565
use rustc_type_ir::sty::TyKind::*;
66-
use rustc_type_ir::{InternAs, InternIteratorElement, Interner, TypeFlags};
66+
use rustc_type_ir::{
67+
InternAs, InternIteratorElement, Interner, TraitObjectRepresentation, TypeFlags,
68+
};
6769

6870
use std::any::Any;
6971
use std::borrow::Borrow;
@@ -2545,8 +2547,9 @@ impl<'tcx> TyCtxt<'tcx> {
25452547
self,
25462548
obj: &'tcx List<ty::Binder<'tcx, ExistentialPredicate<'tcx>>>,
25472549
reg: ty::Region<'tcx>,
2550+
repr: TraitObjectRepresentation,
25482551
) -> Ty<'tcx> {
2549-
self.mk_ty(Dynamic(obj, reg))
2552+
self.mk_ty(Dynamic(obj, reg, repr))
25502553
}
25512554

25522555
#[inline]

compiler/rustc_middle/src/ty/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ impl<'tcx> TypeVisitor<'tcx> for IsSuggestableVisitor<'tcx> {
467467
}
468468
}
469469

470-
Dynamic(dty, _) => {
470+
Dynamic(dty, _, _) => {
471471
for pred in *dty {
472472
match pred.skip_binder() {
473473
ExistentialPredicate::Trait(_) | ExistentialPredicate::Projection(_) => {

compiler/rustc_middle/src/ty/flags.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl FlagComputation {
171171
self.add_substs(substs);
172172
}
173173

174-
&ty::Dynamic(obj, r) => {
174+
&ty::Dynamic(obj, r, _) => {
175175
for predicate in obj.iter() {
176176
self.bound_computation(predicate, |computation, predicate| match predicate {
177177
ty::ExistentialPredicate::Trait(tr) => computation.add_substs(tr.substs),

compiler/rustc_middle/src/ty/layout.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2464,7 +2464,8 @@ where
24642464

24652465
match tcx.struct_tail_erasing_lifetimes(pointee, cx.param_env()).kind() {
24662466
ty::Slice(_) | ty::Str => TyMaybeWithLayout::Ty(tcx.types.usize),
2467-
ty::Dynamic(_, _) => {
2467+
// FIXME(eholk): Do the right thing with trait object representation
2468+
ty::Dynamic(_, _, _repr) => {
24682469
TyMaybeWithLayout::Ty(tcx.mk_imm_ref(
24692470
tcx.lifetimes.re_static,
24702471
tcx.mk_array(tcx.types.usize, 3),

compiler/rustc_middle/src/ty/print/pretty.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rustc_session::cstore::{ExternCrate, ExternCrateSource};
1616
use rustc_span::symbol::{kw, Ident, Symbol};
1717
use rustc_target::abi::Size;
1818
use rustc_target::spec::abi::Abi;
19+
use rustc_type_ir::TraitObjectRepresentation;
1920

2021
use std::cell::Cell;
2122
use std::char;
@@ -619,12 +620,16 @@ pub trait PrettyPrinter<'tcx>:
619620
ty::Adt(def, substs) => {
620621
p!(print_def_path(def.did(), substs));
621622
}
622-
ty::Dynamic(data, r) => {
623+
ty::Dynamic(data, r, repr) => {
623624
let print_r = self.should_print_region(r);
624625
if print_r {
625626
p!("(");
626627
}
627-
p!("dyn ", print(data));
628+
match repr {
629+
TraitObjectRepresentation::Unsized => p!("dyn "),
630+
TraitObjectRepresentation::Sized => p!("dyn* "),
631+
}
632+
p!(print(data));
628633
if print_r {
629634
p!(" + ", print(r), ")");
630635
}

compiler/rustc_middle/src/ty/relate.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,9 @@ pub fn super_relate_tys<'tcx, R: TypeRelation<'tcx>>(
441441

442442
(&ty::Foreign(a_id), &ty::Foreign(b_id)) if a_id == b_id => Ok(tcx.mk_foreign(a_id)),
443443

444-
(&ty::Dynamic(a_obj, a_region), &ty::Dynamic(b_obj, b_region)) => {
444+
(&ty::Dynamic(a_obj, a_region, a_repr), &ty::Dynamic(b_obj, b_region, b_repr))
445+
if a_repr == b_repr =>
446+
{
445447
let region_bound = relation.with_cause(Cause::ExistentialRegionBound, |relation| {
446448
relation.relate_with_variance(
447449
ty::Contravariant,
@@ -450,7 +452,7 @@ pub fn super_relate_tys<'tcx, R: TypeRelation<'tcx>>(
450452
b_region,
451453
)
452454
})?;
453-
Ok(tcx.mk_dynamic(relation.relate(a_obj, b_obj)?, region_bound))
455+
Ok(tcx.mk_dynamic(relation.relate(a_obj, b_obj)?, region_bound, a_repr))
454456
}
455457

456458
(&ty::Generator(a_id, a_substs, movability), &ty::Generator(b_id, b_substs, _))

compiler/rustc_middle/src/ty/structural_impls.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,9 +1014,11 @@ impl<'tcx> TypeSuperFoldable<'tcx> for Ty<'tcx> {
10141014
ty::Array(typ, sz) => ty::Array(typ.try_fold_with(folder)?, sz.try_fold_with(folder)?),
10151015
ty::Slice(typ) => ty::Slice(typ.try_fold_with(folder)?),
10161016
ty::Adt(tid, substs) => ty::Adt(tid, substs.try_fold_with(folder)?),
1017-
ty::Dynamic(trait_ty, region) => {
1018-
ty::Dynamic(trait_ty.try_fold_with(folder)?, region.try_fold_with(folder)?)
1019-
}
1017+
ty::Dynamic(trait_ty, region, representation) => ty::Dynamic(
1018+
trait_ty.try_fold_with(folder)?,
1019+
region.try_fold_with(folder)?,
1020+
representation,
1021+
),
10201022
ty::Tuple(ts) => ty::Tuple(ts.try_fold_with(folder)?),
10211023
ty::FnDef(def_id, substs) => ty::FnDef(def_id, substs.try_fold_with(folder)?),
10221024
ty::FnPtr(f) => ty::FnPtr(f.try_fold_with(folder)?),
@@ -1060,7 +1062,7 @@ impl<'tcx> TypeSuperVisitable<'tcx> for Ty<'tcx> {
10601062
}
10611063
ty::Slice(typ) => typ.visit_with(visitor),
10621064
ty::Adt(_, substs) => substs.visit_with(visitor),
1063-
ty::Dynamic(ref trait_ty, ref reg) => {
1065+
ty::Dynamic(ref trait_ty, ref reg, _) => {
10641066
trait_ty.visit_with(visitor)?;
10651067
reg.visit_with(visitor)
10661068
}

compiler/rustc_middle/src/ty/walk.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ fn push_inner<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent: GenericArg<'tcx>)
152152
ty::Projection(data) => {
153153
stack.extend(data.substs.iter().rev());
154154
}
155-
ty::Dynamic(obj, lt) => {
155+
ty::Dynamic(obj, lt, _) => {
156156
stack.push(lt.into());
157157
stack.extend(obj.iter().rev().flat_map(|predicate| {
158158
let (substs, opt_ty) = match predicate.skip_binder() {

compiler/rustc_symbol_mangling/src/typeid/typeid_itanium_cxx_abi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ fn encode_ty<'tcx>(
627627
}
628628

629629
// Trait types
630-
ty::Dynamic(predicates, region) => {
630+
ty::Dynamic(predicates, region, _repr) => {
631631
// u3dynI<element-type1[..element-typeN]>E, where <element-type> is <predicate>, as
632632
// vendor extended type.
633633
let mut s = String::from("u3dynI");

compiler/rustc_symbol_mangling/src/v0.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> {
479479
})?;
480480
}
481481

482-
ty::Dynamic(predicates, r) => {
482+
ty::Dynamic(predicates, r, _repr) => {
483483
self.push("D");
484484
self = self.print_dyn_existential(predicates)?;
485485
self = r.print(self)?;

compiler/rustc_trait_selection/src/traits/error_reporting/on_unimplemented.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
256256
}
257257
}
258258
}
259-
if let ty::Dynamic(traits, _) = self_ty.kind() {
259+
if let ty::Dynamic(traits, _, _) = self_ty.kind() {
260260
for t in traits.iter() {
261261
if let ty::ExistentialPredicate::Trait(trait_ref) = t.skip_binder() {
262262
flags.push((sym::_Self, Some(self.tcx.def_path_str(trait_ref.def_id))))

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
10671067
self_ty: Ty<'tcx>,
10681068
object_ty: Ty<'tcx>,
10691069
) {
1070-
let ty::Dynamic(predicates, _) = object_ty.kind() else { return; };
1070+
let ty::Dynamic(predicates, _, _) = object_ty.kind() else { return; };
10711071
let self_ref_ty = self.tcx.mk_imm_ref(self.tcx.lifetimes.re_erased, self_ty);
10721072

10731073
for predicate in predicates.iter() {
@@ -1365,7 +1365,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
13651365
let trait_pred = self.resolve_vars_if_possible(trait_pred);
13661366
let ty = trait_pred.skip_binder().self_ty();
13671367
let is_object_safe = match ty.kind() {
1368-
ty::Dynamic(predicates, _) => {
1368+
ty::Dynamic(predicates, _, _) => {
13691369
// If the `dyn Trait` is not object safe, do not suggest `Box<dyn Trait>`.
13701370
predicates
13711371
.principal_def_id()
@@ -1425,7 +1425,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
14251425
let mut spans_and_needs_box = vec![];
14261426

14271427
match liberated_sig.output().kind() {
1428-
ty::Dynamic(predicates, _) => {
1428+
ty::Dynamic(predicates, _, _) => {
14291429
let cause = ObligationCause::misc(ret_ty.span, fn_hir_id);
14301430
let param_env = ty::ParamEnv::empty();
14311431

compiler/rustc_trait_selection/src/traits/object_safety.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use rustc_middle::ty::abstract_const::{walk_abstract_const, AbstractConst};
2121
use rustc_middle::ty::subst::{GenericArg, InternalSubsts, Subst};
2222
use rustc_middle::ty::{
2323
self, EarlyBinder, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitor,
24+
TraitObjectRepresentation,
2425
};
2526
use rustc_middle::ty::{Predicate, ToPredicate};
2627
use rustc_session::lint::builtin::WHERE_CLAUSES_OBJECT_SAFETY;
@@ -600,7 +601,8 @@ fn object_ty_for_trait<'tcx>(
600601
let existential_predicates = tcx
601602
.mk_poly_existential_predicates(iter::once(trait_predicate).chain(projection_predicates));
602603

603-
let object_ty = tcx.mk_dynamic(existential_predicates, lifetime);
604+
let object_ty =
605+
tcx.mk_dynamic(existential_predicates, lifetime, TraitObjectRepresentation::Unsized);
604606

605607
debug!("object_ty_for_trait: object_ty=`{}`", object_ty);
606608

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
784784
let upcast_trait_ref;
785785
match (source.kind(), target.kind()) {
786786
// TraitA+Kx+'a -> TraitB+Ky+'b (trait upcasting coercion).
787-
(&ty::Dynamic(ref data_a, r_a), &ty::Dynamic(ref data_b, r_b)) => {
787+
(&ty::Dynamic(ref data_a, r_a, repr_a), &ty::Dynamic(ref data_b, r_b, repr_b))
788+
if repr_a == repr_b =>
789+
{
788790
// See `assemble_candidates_for_unsizing` for more info.
789791
// We already checked the compatibility of auto traits within `assemble_candidates_for_unsizing`.
790792
let principal_a = data_a.principal().unwrap();
@@ -810,7 +812,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
810812
.map(ty::Binder::dummy),
811813
);
812814
let existential_predicates = tcx.mk_poly_existential_predicates(iter);
813-
let source_trait = tcx.mk_dynamic(existential_predicates, r_b);
815+
let source_trait = tcx.mk_dynamic(existential_predicates, r_b, repr_b);
814816

815817
// Require that the traits involved in this upcast are **equal**;
816818
// only the **lifetime bound** is changed.
@@ -888,7 +890,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
888890
let mut nested = vec![];
889891
match (source.kind(), target.kind()) {
890892
// Trait+Kx+'a -> Trait+Ky+'b (auto traits and lifetime subtyping).
891-
(&ty::Dynamic(ref data_a, r_a), &ty::Dynamic(ref data_b, r_b)) => {
893+
(&ty::Dynamic(ref data_a, r_a, repr_a), &ty::Dynamic(ref data_b, r_b, repr_b))
894+
if repr_a == repr_b =>
895+
{
892896
// See `assemble_candidates_for_unsizing` for more info.
893897
// We already checked the compatibility of auto traits within `assemble_candidates_for_unsizing`.
894898
let iter = data_a
@@ -907,7 +911,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
907911
.map(ty::Binder::dummy),
908912
);
909913
let existential_predicates = tcx.mk_poly_existential_predicates(iter);
910-
let source_trait = tcx.mk_dynamic(existential_predicates, r_b);
914+
let source_trait = tcx.mk_dynamic(existential_predicates, r_b, repr_b);
911915

912916
// Require that the traits involved in this upcast are **equal**;
913917
// only the **lifetime bound** is changed.
@@ -934,7 +938,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
934938
}
935939

936940
// `T` -> `Trait`
937-
(_, &ty::Dynamic(ref data, r)) => {
941+
(_, &ty::Dynamic(ref data, r, _repr)) => {
938942
let mut object_dids = data.auto_traits().chain(data.principal_def_id());
939943
if let Some(did) = object_dids.find(|did| !tcx.is_object_safe(*did)) {
940944
return Err(TraitNotObjectSafe(did));

compiler/rustc_trait_selection/src/traits/wf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ impl<'tcx> WfPredicates<'tcx> {
639639
}
640640
}
641641

642-
ty::Dynamic(data, r) => {
642+
ty::Dynamic(data, r, _) => {
643643
// WfObject
644644
//
645645
// Here, we defer WF checking due to higher-ranked

compiler/rustc_traits/src/chalk/lowering.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::Ty<RustInterner<'tcx>>> for Ty<'tcx> {
326326
)),
327327
})
328328
}
329-
ty::Dynamic(predicates, region) => chalk_ir::TyKind::Dyn(chalk_ir::DynTy {
329+
ty::Dynamic(predicates, region, _repr) => chalk_ir::TyKind::Dyn(chalk_ir::DynTy {
330330
bounds: predicates.lower_into(interner),
331331
lifetime: region.lower_into(interner),
332332
}),

compiler/rustc_ty_utils/src/ty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ fn issue33140_self_ty(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Ty<'_>> {
389389

390390
let self_ty = trait_ref.self_ty();
391391
let self_ty_matches = match self_ty.kind() {
392-
ty::Dynamic(ref data, re) if re.is_static() => data.principal().is_none(),
392+
ty::Dynamic(ref data, re, _) if re.is_static() => data.principal().is_none(),
393393
_ => false,
394394
};
395395

0 commit comments

Comments
 (0)