rustc_hir_analysis/hir_ty_lowering/
dyn_compatibility.rs

1use 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    /// Lower a trait object type from the HIR to our internal notion of a type.
26    #[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        // We  don't support empty trait objects.
80        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        // We don't support >1 principal
86        if regular_traits.len() > 1 {
87            let guar = self.report_trait_object_addition_traits(&regular_traits);
88            return Ty::new_error(tcx, guar);
89        }
90        // Don't create a dyn trait if we have errors in the principal.
91        if let Err(guar) = regular_traits.error_reported() {
92            return Ty::new_error(tcx, guar);
93        }
94
95        // Check that there are no gross dyn-compatibility violations;
96        // most importantly, that the supertraits don't contain `Self`,
97        // to avoid ICEs.
98        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        // Map the projection bounds onto a key that makes it easy to remove redundant
116        // bounds that are constrained by supertraits of the principal def id.
117        //
118        // Also make sure we detect conflicting bounds from expanding a trait alias and
119        // also specifying it manually, like:
120        // ```
121        // type Alias = Trait<Assoc = i32>;
122        // let _: &dyn Alias<Assoc = u32> = /* ... */;
123        // ```
124        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                        // With trait alias and type alias combined, type resolver
131                        // may not be able to catch all illegal `Self` usages (issue 139082)
132                        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        // A stable ordering of associated types from the principal trait and all its
173        // supertraits. We use this to ensure that different substitutions of a trait
174        // don't result in `dyn Trait` types with different projections lists, which
175        // can be unsound: <https://github.com/rust-lang/rust/pull/136458>.
176        // We achieve a stable ordering by walking over the unsubstituted principal
177        // trait ref.
178        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                        // FIXME(negative_bounds): Handle this correctly...
202                        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                                // We only care about associated types.
208                                .filter(|item| item.is_type())
209                                // No RPITITs -- they're not dyn-compatible for now.
210                                .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                        // A `Self` within the original bound will be instantiated with a
217                        // `trait_object_dummy_self`, so check for that.
218                        let references_self = match pred.skip_binder().term.kind() {
219                            ty::TermKind::Ty(ty) => ty.walk().any(|arg| arg == dummy_self.into()),
220                            // FIXME(associated_const_equality): We should walk the const instead of not doing anything
221                            ty::TermKind::Const(_) => false,
222                        };
223
224                        // If the projection output contains `Self`, force the user to
225                        // elaborate it explicitly to avoid a lot of complexity.
226                        //
227                        // The "classically useful" case is the following:
228                        // ```
229                        //     trait MyTrait: FnMut() -> <Self as MyTrait>::MyOutput {
230                        //         type MyOutput;
231                        //     }
232                        // ```
233                        //
234                        // Here, the user could theoretically write `dyn MyTrait<MyOutput = X>`,
235                        // but actually supporting that would "expand" to an infinitely-long type
236                        // `fix $ τ → dyn MyTrait<MyOutput = X, Output = <τ as MyTrait>::MyOutput`.
237                        //
238                        // Instead, we force the user to write
239                        // `dyn MyTrait<MyOutput = X, Output = X>`, which is uglier but works. See
240                        // the discussion in #56288 for alternatives.
241                        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        // `dyn Trait<Assoc = Foo>` desugars to (not Rust syntax) `dyn Trait where
265        // <Self as Trait>::Assoc = Foo`. So every `Projection` clause is an
266        // `Assoc = Foo` bound. `needed_associated_types` contains all associated
267        // types that we expect to be provided by the user, so the following loop
268        // removes all the associated types that have a corresponding `Projection`
269        // clause, either from expanding trait aliases or written by the user.
270        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        // We compute the list of projection bounds taking the ordered associated types,
283        // and check if there was an entry in the collected `projection_bounds`. Those
284        // are computed by first taking the user-written associated types, then elaborating
285        // the principal trait ref, and only using those if there was no user-written.
286        // See note below about how we handle missing associated types with `Self: Sized`,
287        // which are not required to be provided, but are still used if they are provided.
288        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 the associated type has a `where Self: Sized` bound, then
296                    // we do not need to provide the associated type. This results in
297                    // a `dyn Trait` type that has a different number of projection
298                    // bounds, which may lead to type mismatches.
299                    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        // De-duplicate auto traits so that, e.g., `dyn Trait + Send + Send` is the same as
317        // `dyn Trait + Send`.
318        // We remove duplicates by inserting into a `FxHashSet` to avoid re-ordering
319        // the bounds
320        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        // Erase the `dummy_self` (`trait_object_dummy_self`) used above.
327        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                // Verify that `dummy_self` did not leak inside default type parameters. This
336                // could not be done at path creation, since we need to see through trait aliases.
337                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 `Self`
344                    .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                // Like for trait refs, verify that `dummy_self` did not leak inside default type
380                // parameters.
381                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        // N.b. principal, projections, auto traits
412        // FIXME: This is actually wrong with multiple principals in regards to symbol mangling
413        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        // Use explicitly-specified region bound, unless the bound is missing.
422        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                // Curiously, we prefer object lifetime default for `+ '_`...
427                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                                // Parent lifetime must have failed to resolve. Don't emit a redundant error.
439                                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    /// Check that elaborating the principal of a trait ref doesn't lead to projections
456    /// that are unconstrained. This can happen because an otherwise unconstrained
457    /// *type variable* can be substituted with a type that has late-bound regions. See
458    /// `elaborated-predicates-unconstrained-late-bound.rs` for a test.
459    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        // Find any late-bound regions declared in `ty` that are not
468        // declared in the trait-ref or assoc_item. These are not well-formed.
469        //
470        // Example:
471        //
472        //     for<'a> <T as Iterator>::Item = &'a str // <-- 'a is bad
473        //     for<'a> <T as FnMut<(&'a u32,)>>::Output = &'a str // <-- 'a is ok
474        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        // FIXME: point at the type params that don't have appropriate lifetimes:
482        // struct S1<F: for<'a> Fn(&i32, &i32) -> &'a i32>(F);
483        //                         ----  ----     ^^^^^^^
484        // NOTE(associated_const_equality): This error should be impossible to trigger
485        //                                  with associated const equality constraints.
486        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}