rustc_hir_analysis/hir_ty_lowering/
dyn_compatibility.rs1use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
2use rustc_errors::codes::*;
3use rustc_errors::struct_span_code_err;
4use rustc_hir as hir;
5use rustc_hir::def::{DefKind, Res};
6use rustc_lint_defs::builtin::UNUSED_ASSOCIATED_TYPE_BOUNDS;
7use rustc_middle::ty::elaborate::ClauseWithSupertraitSpan;
8use rustc_middle::ty::{
9 self, BottomUpFolder, DynKind, ExistentialPredicateStableCmpExt as _, Ty, TyCtxt, TypeFoldable,
10 TypeVisitableExt, Upcast,
11};
12use rustc_span::{ErrorGuaranteed, Span};
13use rustc_trait_selection::error_reporting::traits::report_dyn_incompatibility;
14use rustc_trait_selection::traits;
15use smallvec::{SmallVec, smallvec};
16use tracing::{debug, instrument};
17
18use super::HirTyLowerer;
19use crate::errors::SelfInTypeAlias;
20use crate::hir_ty_lowering::{
21 GenericArgCountMismatch, GenericArgCountResult, PredicateFilter, RegionInferReason,
22};
23
24impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
25 #[instrument(level = "debug", skip_all, ret)]
27 pub(super) fn lower_trait_object_ty(
28 &self,
29 span: Span,
30 hir_id: hir::HirId,
31 hir_bounds: &[hir::PolyTraitRef<'tcx>],
32 lifetime: &hir::Lifetime,
33 representation: DynKind,
34 ) -> Ty<'tcx> {
35 let tcx = self.tcx();
36 let dummy_self = tcx.types.trait_object_dummy_self;
37
38 let mut user_written_bounds = Vec::new();
39 let mut potential_assoc_types = Vec::new();
40 for trait_bound in hir_bounds.iter() {
41 if let hir::BoundPolarity::Maybe(_) = trait_bound.modifiers.polarity {
42 continue;
43 }
44 if let GenericArgCountResult {
45 correct:
46 Err(GenericArgCountMismatch { invalid_args: cur_potential_assoc_types, .. }),
47 ..
48 } = self.lower_poly_trait_ref(
49 &trait_bound.trait_ref,
50 trait_bound.span,
51 hir::BoundConstness::Never,
52 hir::BoundPolarity::Positive,
53 dummy_self,
54 &mut user_written_bounds,
55 PredicateFilter::SelfOnly,
56 ) {
57 potential_assoc_types.extend(cur_potential_assoc_types);
58 }
59 }
60
61 let ast_bounds: Vec<_> =
62 hir_bounds.iter().map(|&trait_ref| hir::GenericBound::Trait(trait_ref)).collect();
63
64 self.add_default_traits_with_filter(
65 &mut user_written_bounds,
66 dummy_self,
67 &ast_bounds,
68 None,
69 span,
70 |tr| tr != hir::LangItem::Sized,
71 );
72
73 let (elaborated_trait_bounds, elaborated_projection_bounds) =
74 traits::expand_trait_aliases(tcx, user_written_bounds.iter().copied());
75 let (regular_traits, mut auto_traits): (Vec<_>, Vec<_>) = elaborated_trait_bounds
76 .into_iter()
77 .partition(|(trait_ref, _)| !tcx.trait_is_auto(trait_ref.def_id()));
78
79 if regular_traits.is_empty() && auto_traits.is_empty() {
81 let guar =
82 self.report_trait_object_with_no_traits(span, user_written_bounds.iter().copied());
83 return Ty::new_error(tcx, guar);
84 }
85 if regular_traits.len() > 1 {
87 let guar = self.report_trait_object_addition_traits(®ular_traits);
88 return Ty::new_error(tcx, guar);
89 }
90 if let Err(guar) = regular_traits.error_reported() {
92 return Ty::new_error(tcx, guar);
93 }
94
95 for (clause, span) in user_written_bounds {
99 if let Some(trait_pred) = clause.as_trait_clause() {
100 let violations = self.dyn_compatibility_violations(trait_pred.def_id());
101 if !violations.is_empty() {
102 let reported = report_dyn_incompatibility(
103 tcx,
104 span,
105 Some(hir_id),
106 trait_pred.def_id(),
107 &violations,
108 )
109 .emit();
110 return Ty::new_error(tcx, reported);
111 }
112 }
113 }
114
115 let mut projection_bounds = FxIndexMap::default();
125 for (proj, proj_span) in elaborated_projection_bounds {
126 let proj = proj.map_bound(|mut b| {
127 if let Some(term_ty) = &b.term.as_type() {
128 let references_self = term_ty.walk().any(|arg| arg == dummy_self.into());
129 if references_self {
130 let guar = self.dcx().emit_err(SelfInTypeAlias { span });
133 b.term = replace_dummy_self_with_error(tcx, b.term, guar);
134 }
135 }
136 b
137 });
138
139 let key = (
140 proj.skip_binder().projection_term.def_id,
141 tcx.anonymize_bound_vars(
142 proj.map_bound(|proj| proj.projection_term.trait_ref(tcx)),
143 ),
144 );
145 if let Some((old_proj, old_proj_span)) =
146 projection_bounds.insert(key, (proj, proj_span))
147 && tcx.anonymize_bound_vars(proj) != tcx.anonymize_bound_vars(old_proj)
148 {
149 let item = tcx.item_name(proj.item_def_id());
150 self.dcx()
151 .struct_span_err(
152 span,
153 format!(
154 "conflicting associated type bounds for `{item}` when \
155 expanding trait alias"
156 ),
157 )
158 .with_span_label(
159 old_proj_span,
160 format!("`{item}` is specified to be `{}` here", old_proj.term()),
161 )
162 .with_span_label(
163 proj_span,
164 format!("`{item}` is specified to be `{}` here", proj.term()),
165 )
166 .emit();
167 }
168 }
169
170 let principal_trait = regular_traits.into_iter().next();
171
172 let mut ordered_associated_types = vec![];
179
180 if let Some((principal_trait, ref spans)) = principal_trait {
181 let principal_trait = principal_trait.map_bound(|trait_pred| {
182 assert_eq!(trait_pred.polarity, ty::PredicatePolarity::Positive);
183 trait_pred.trait_ref
184 });
185
186 for ClauseWithSupertraitSpan { clause, supertrait_span } in traits::elaborate(
187 tcx,
188 [ClauseWithSupertraitSpan::new(
189 ty::TraitRef::identity(tcx, principal_trait.def_id()).upcast(tcx),
190 *spans.last().unwrap(),
191 )],
192 )
193 .filter_only_self()
194 {
195 let clause = clause.instantiate_supertrait(tcx, principal_trait);
196 debug!("observing object predicate `{clause:?}`");
197
198 let bound_predicate = clause.kind();
199 match bound_predicate.skip_binder() {
200 ty::ClauseKind::Trait(pred) => {
201 let trait_ref =
203 tcx.anonymize_bound_vars(bound_predicate.rebind(pred.trait_ref));
204 ordered_associated_types.extend(
205 tcx.associated_items(pred.trait_ref.def_id)
206 .in_definition_order()
207 .filter(|item| item.is_type())
209 .filter(|item| !item.is_impl_trait_in_trait())
211 .map(|item| (item.def_id, trait_ref)),
212 );
213 }
214 ty::ClauseKind::Projection(pred) => {
215 let pred = bound_predicate.rebind(pred);
216 let references_self = match pred.skip_binder().term.kind() {
219 ty::TermKind::Ty(ty) => ty.walk().any(|arg| arg == dummy_self.into()),
220 ty::TermKind::Const(_) => false,
222 };
223
224 if !references_self {
242 let key = (
243 pred.skip_binder().projection_term.def_id,
244 tcx.anonymize_bound_vars(
245 pred.map_bound(|proj| proj.projection_term.trait_ref(tcx)),
246 ),
247 );
248 if !projection_bounds.contains_key(&key) {
249 projection_bounds.insert(key, (pred, supertrait_span));
250 }
251 }
252
253 self.check_elaborated_projection_mentions_input_lifetimes(
254 pred,
255 *spans.first().unwrap(),
256 supertrait_span,
257 );
258 }
259 _ => (),
260 }
261 }
262 }
263
264 for &(projection_bound, span) in projection_bounds.values() {
271 let def_id = projection_bound.item_def_id();
272 if tcx.generics_require_sized_self(def_id) {
273 tcx.emit_node_span_lint(
274 UNUSED_ASSOCIATED_TYPE_BOUNDS,
275 hir_id,
276 span,
277 crate::errors::UnusedAssociatedTypeBounds { span },
278 );
279 }
280 }
281
282 let mut missing_assoc_types = FxIndexSet::default();
289 let projection_bounds: Vec<_> = ordered_associated_types
290 .into_iter()
291 .filter_map(|key| {
292 if let Some(assoc) = projection_bounds.get(&key) {
293 Some(*assoc)
294 } else {
295 if !tcx.generics_require_sized_self(key.0) {
300 missing_assoc_types.insert(key);
301 }
302 None
303 }
304 })
305 .collect();
306
307 if let Err(guar) = self.check_for_required_assoc_tys(
308 principal_trait.as_ref().map_or(smallvec![], |(_, spans)| spans.clone()),
309 missing_assoc_types,
310 potential_assoc_types,
311 hir_bounds,
312 ) {
313 return Ty::new_error(tcx, guar);
314 }
315
316 let mut duplicates = FxHashSet::default();
321 auto_traits.retain(|(trait_pred, _)| duplicates.insert(trait_pred.def_id()));
322
323 debug!(?principal_trait);
324 debug!(?auto_traits);
325
326 let principal_trait_ref = principal_trait.map(|(trait_pred, spans)| {
328 trait_pred.map_bound(|trait_pred| {
329 let trait_ref = trait_pred.trait_ref;
330 assert_eq!(trait_pred.polarity, ty::PredicatePolarity::Positive);
331 assert_eq!(trait_ref.self_ty(), dummy_self);
332
333 let span = *spans.first().unwrap();
334
335 let mut missing_type_params = vec![];
338 let generics = tcx.generics_of(trait_ref.def_id);
339 let args: Vec<_> = trait_ref
340 .args
341 .iter()
342 .enumerate()
343 .skip(1)
345 .map(|(index, arg)| {
346 if arg.walk().any(|arg| arg == dummy_self.into()) {
347 let param = &generics.own_params[index];
348 missing_type_params.push(param.name);
349 Ty::new_misc_error(tcx).into()
350 } else {
351 arg
352 }
353 })
354 .collect();
355
356 let empty_generic_args = hir_bounds.iter().any(|hir_bound| {
357 hir_bound.trait_ref.path.res == Res::Def(DefKind::Trait, trait_ref.def_id)
358 && hir_bound.span.contains(span)
359 });
360 self.report_missing_type_params(
361 missing_type_params,
362 trait_ref.def_id,
363 span,
364 empty_generic_args,
365 );
366
367 ty::ExistentialPredicate::Trait(ty::ExistentialTraitRef::new(
368 tcx,
369 trait_ref.def_id,
370 args,
371 ))
372 })
373 });
374
375 let existential_projections = projection_bounds.into_iter().map(|(bound, _)| {
376 bound.map_bound(|mut b| {
377 assert_eq!(b.projection_term.self_ty(), dummy_self);
378
379 let references_self = b.projection_term.args.iter().skip(1).any(|arg| {
382 if arg.walk().any(|arg| arg == dummy_self.into()) {
383 return true;
384 }
385 false
386 });
387 if references_self {
388 let guar = tcx
389 .dcx()
390 .span_delayed_bug(span, "trait object projection bounds reference `Self`");
391 b.projection_term = replace_dummy_self_with_error(tcx, b.projection_term, guar);
392 }
393
394 ty::ExistentialPredicate::Projection(ty::ExistentialProjection::erase_self_ty(
395 tcx, b,
396 ))
397 })
398 });
399
400 let mut auto_trait_predicates: Vec<_> = auto_traits
401 .into_iter()
402 .map(|(trait_pred, _)| {
403 assert_eq!(trait_pred.polarity(), ty::PredicatePolarity::Positive);
404 assert_eq!(trait_pred.self_ty().skip_binder(), dummy_self);
405
406 ty::Binder::dummy(ty::ExistentialPredicate::AutoTrait(trait_pred.def_id()))
407 })
408 .collect();
409 auto_trait_predicates.dedup();
410
411 let mut v = principal_trait_ref
414 .into_iter()
415 .chain(existential_projections)
416 .chain(auto_trait_predicates)
417 .collect::<SmallVec<[_; 8]>>();
418 v.sort_by(|a, b| a.skip_binder().stable_cmp(tcx, &b.skip_binder()));
419 let existential_predicates = tcx.mk_poly_existential_predicates(&v);
420
421 let region_bound = if !lifetime.is_elided() {
423 self.lower_lifetime(lifetime, RegionInferReason::ExplicitObjectLifetime)
424 } else {
425 self.compute_object_lifetime_bound(span, existential_predicates).unwrap_or_else(|| {
426 if tcx.named_bound_var(lifetime.hir_id).is_some() {
428 self.lower_lifetime(lifetime, RegionInferReason::ExplicitObjectLifetime)
429 } else {
430 let reason =
431 if let hir::LifetimeKind::ImplicitObjectLifetimeDefault = lifetime.kind {
432 if let hir::Node::Ty(hir::Ty {
433 kind: hir::TyKind::Ref(parent_lifetime, _),
434 ..
435 }) = tcx.parent_hir_node(hir_id)
436 && tcx.named_bound_var(parent_lifetime.hir_id).is_none()
437 {
438 RegionInferReason::ExplicitObjectLifetime
440 } else {
441 RegionInferReason::ObjectLifetimeDefault
442 }
443 } else {
444 RegionInferReason::ExplicitObjectLifetime
445 };
446 self.re_infer(span, reason)
447 }
448 })
449 };
450 debug!(?region_bound);
451
452 Ty::new_dynamic(tcx, existential_predicates, region_bound, representation)
453 }
454
455 fn check_elaborated_projection_mentions_input_lifetimes(
460 &self,
461 pred: ty::PolyProjectionPredicate<'tcx>,
462 span: Span,
463 supertrait_span: Span,
464 ) {
465 let tcx = self.tcx();
466
467 let late_bound_in_projection_term =
475 tcx.collect_constrained_late_bound_regions(pred.map_bound(|pred| pred.projection_term));
476 let late_bound_in_term =
477 tcx.collect_referenced_late_bound_regions(pred.map_bound(|pred| pred.term));
478 debug!(?late_bound_in_projection_term);
479 debug!(?late_bound_in_term);
480
481 self.validate_late_bound_regions(
487 late_bound_in_projection_term,
488 late_bound_in_term,
489 |br_name| {
490 let item_name = tcx.item_name(pred.item_def_id());
491 struct_span_code_err!(
492 self.dcx(),
493 span,
494 E0582,
495 "binding for associated type `{}` references {}, \
496 which does not appear in the trait input types",
497 item_name,
498 br_name
499 )
500 .with_span_label(supertrait_span, "due to this supertrait")
501 },
502 );
503 }
504}
505
506fn replace_dummy_self_with_error<'tcx, T: TypeFoldable<TyCtxt<'tcx>>>(
507 tcx: TyCtxt<'tcx>,
508 t: T,
509 guar: ErrorGuaranteed,
510) -> T {
511 t.fold_with(&mut BottomUpFolder {
512 tcx,
513 ty_op: |ty| {
514 if ty == tcx.types.trait_object_dummy_self { Ty::new_error(tcx, guar) } else { ty }
515 },
516 lt_op: |lt| lt,
517 ct_op: |ct| ct,
518 })
519}