1use rustc_ast::TraitObjectSyntax;
2use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
3use rustc_errors::codes::*;
4use rustc_errors::{
5 Applicability, Diag, DiagCtxtHandle, Diagnostic, EmissionGuarantee, Level, StashKey,
6 Suggestions, struct_span_code_err,
7};
8use rustc_hir::def::{DefKind, Res};
9use rustc_hir::def_id::DefId;
10use rustc_hir::{self as hir, HirId, LangItem};
11use rustc_lint_defs::builtin::{BARE_TRAIT_OBJECTS, UNUSED_ASSOCIATED_TYPE_BOUNDS};
12use rustc_middle::ty::elaborate::ClauseWithSupertraitSpan;
13use rustc_middle::ty::{
14 self, BottomUpFolder, ExistentialPredicateStableCmpExt as _, Ty, TyCtxt, TypeFoldable,
15 TypeVisitableExt, Upcast,
16};
17use rustc_span::edit_distance::find_best_match_for_name;
18use rustc_span::{ErrorGuaranteed, Span};
19use rustc_trait_selection::error_reporting::traits::report_dyn_incompatibility;
20use rustc_trait_selection::error_reporting::traits::suggestions::NextTypeParamName;
21use rustc_trait_selection::traits;
22use smallvec::{SmallVec, smallvec};
23use tracing::{debug, instrument};
24
25use super::HirTyLowerer;
26use crate::errors::DynTraitAssocItemBindingMentionsSelf;
27use crate::hir_ty_lowering::{
28 GenericArgCountMismatch, ImpliedBoundsContext, OverlappingAsssocItemConstraints,
29 PredicateFilter, RegionInferReason,
30};
31
32impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
33 x;#[instrument(level = "debug", skip_all, ret)]
35 pub(super) fn lower_trait_object_ty(
36 &self,
37 span: Span,
38 hir_id: hir::HirId,
39 hir_bounds: &[hir::PolyTraitRef<'tcx>],
40 lifetime: &hir::Lifetime,
41 syntax: TraitObjectSyntax,
42 ) -> Ty<'tcx> {
43 let tcx = self.tcx();
44 let dummy_self = tcx.types.trait_object_dummy_self;
45
46 match syntax {
47 TraitObjectSyntax::Dyn => {}
48 TraitObjectSyntax::None => {
49 match self.prohibit_or_lint_bare_trait_object_ty(span, hir_id, hir_bounds) {
50 Some(guar) => return Ty::new_error(tcx, guar),
54 None => {}
55 }
56 }
57 }
58
59 let mut user_written_bounds = Vec::new();
60 let mut potential_assoc_items = Vec::new();
61 for poly_trait_ref in hir_bounds.iter() {
62 let result = self.lower_poly_trait_ref(
68 poly_trait_ref,
69 dummy_self,
70 &mut user_written_bounds,
71 PredicateFilter::SelfOnly,
72 OverlappingAsssocItemConstraints::Forbidden,
73 );
74 if let Err(GenericArgCountMismatch { invalid_args, .. }) = result.correct {
75 potential_assoc_items.extend(invalid_args);
76 }
77 }
78
79 self.add_default_traits(
80 &mut user_written_bounds,
81 dummy_self,
82 &hir_bounds
83 .iter()
84 .map(|&trait_ref| hir::GenericBound::Trait(trait_ref))
85 .collect::<Vec<_>>(),
86 ImpliedBoundsContext::AssociatedTypeOrImplTrait,
87 span,
88 );
89
90 let (mut elaborated_trait_bounds, elaborated_projection_bounds) =
91 traits::expand_trait_aliases(tcx, user_written_bounds.iter().copied());
92
93 debug!(?user_written_bounds, ?elaborated_trait_bounds);
95 let meta_sized_did = tcx.require_lang_item(LangItem::MetaSized, span);
96 if user_written_bounds
99 .iter()
100 .all(|(clause, _)| clause.as_trait_clause().map(|p| p.def_id()) != Some(meta_sized_did))
101 {
102 elaborated_trait_bounds.retain(|(pred, _)| pred.def_id() != meta_sized_did);
103 }
104 debug!(?user_written_bounds, ?elaborated_trait_bounds);
105
106 let (regular_traits, mut auto_traits): (Vec<_>, Vec<_>) = elaborated_trait_bounds
107 .into_iter()
108 .partition(|(trait_ref, _)| !tcx.trait_is_auto(trait_ref.def_id()));
109
110 if regular_traits.is_empty() && auto_traits.is_empty() {
112 let guar =
113 self.report_trait_object_with_no_traits(span, user_written_bounds.iter().copied());
114 return Ty::new_error(tcx, guar);
115 }
116 if regular_traits.len() > 1 {
118 let guar = self.report_trait_object_addition_traits(®ular_traits);
119 return Ty::new_error(tcx, guar);
120 }
121 if let Err(guar) = regular_traits.error_reported() {
123 return Ty::new_error(tcx, guar);
124 }
125
126 for (clause, span) in user_written_bounds {
130 if let Some(trait_pred) = clause.as_trait_clause() {
131 let violations = self.dyn_compatibility_violations(trait_pred.def_id());
132 if !violations.is_empty() {
133 let reported = report_dyn_incompatibility(
134 tcx,
135 span,
136 Some(hir_id),
137 trait_pred.def_id(),
138 &violations,
139 )
140 .emit();
141 return Ty::new_error(tcx, reported);
142 }
143 }
144 }
145
146 let mut projection_bounds = FxIndexMap::default();
156 for (proj, proj_span) in elaborated_projection_bounds {
157 let item_def_id = proj.item_def_id();
158
159 let proj = proj.map_bound(|mut proj| {
160 let references_self = proj.term.walk().any(|arg| arg == dummy_self.into());
161 if references_self {
162 let guar = self.dcx().emit_err(DynTraitAssocItemBindingMentionsSelf {
163 span,
164 kind: tcx.def_descr(item_def_id),
165 binding: proj_span,
166 });
167 proj.term = replace_dummy_self_with_error(tcx, proj.term, guar);
168 }
169 proj
170 });
171
172 let key = (
173 item_def_id,
174 tcx.anonymize_bound_vars(
175 proj.map_bound(|proj| proj.projection_term.trait_ref(tcx)),
176 ),
177 );
178 if let Some((old_proj, old_proj_span)) =
179 projection_bounds.insert(key, (proj, proj_span))
180 && tcx.anonymize_bound_vars(proj) != tcx.anonymize_bound_vars(old_proj)
181 {
182 let kind = tcx.def_descr(item_def_id);
183 let name = tcx.item_name(item_def_id);
184 self.dcx()
185 .struct_span_err(span, format!("conflicting {kind} bindings for `{name}`"))
186 .with_span_label(
187 old_proj_span,
188 format!("`{name}` is specified to be `{}` here", old_proj.term()),
189 )
190 .with_span_label(
191 proj_span,
192 format!("`{name}` is specified to be `{}` here", proj.term()),
193 )
194 .emit();
195 }
196 }
197
198 let principal_trait = regular_traits.into_iter().next();
199
200 let mut ordered_associated_items = vec![];
206
207 if let Some((principal_trait, ref spans)) = principal_trait {
208 let principal_trait = principal_trait.map_bound(|trait_pred| {
209 assert_eq!(trait_pred.polarity, ty::PredicatePolarity::Positive);
210 trait_pred.trait_ref
211 });
212
213 for ClauseWithSupertraitSpan { clause, supertrait_span } in traits::elaborate(
214 tcx,
215 [ClauseWithSupertraitSpan::new(
216 ty::TraitRef::identity(tcx, principal_trait.def_id()).upcast(tcx),
217 *spans.last().unwrap(),
218 )],
219 )
220 .filter_only_self()
221 {
222 let clause = clause.instantiate_supertrait(tcx, principal_trait);
223 debug!("observing object predicate `{clause:?}`");
224
225 let bound_predicate = clause.kind();
226 match bound_predicate.skip_binder() {
227 ty::ClauseKind::Trait(pred) => {
228 let trait_ref =
230 tcx.anonymize_bound_vars(bound_predicate.rebind(pred.trait_ref));
231 ordered_associated_items.extend(
232 tcx.associated_items(pred.trait_ref.def_id)
233 .in_definition_order()
234 .filter(|item| item.is_type() || item.is_type_const())
237 .filter(|item| !item.is_impl_trait_in_trait())
239 .map(|item| (item.def_id, trait_ref)),
240 );
241 }
242 ty::ClauseKind::Projection(pred) => {
243 let pred = bound_predicate.rebind(pred);
244 let references_self =
247 pred.skip_binder().term.walk().any(|arg| arg == dummy_self.into());
248
249 if !references_self {
267 let key = (
268 pred.item_def_id(),
269 tcx.anonymize_bound_vars(
270 pred.map_bound(|proj| proj.projection_term.trait_ref(tcx)),
271 ),
272 );
273 if !projection_bounds.contains_key(&key) {
274 projection_bounds.insert(key, (pred, supertrait_span));
275 }
276 }
277
278 self.check_elaborated_projection_mentions_input_lifetimes(
279 pred,
280 *spans.first().unwrap(),
281 supertrait_span,
282 );
283 }
284 _ => (),
285 }
286 }
287 }
288
289 for &(projection_bound, span) in projection_bounds.values() {
291 let def_id = projection_bound.item_def_id();
292 if tcx.generics_require_sized_self(def_id) {
293 tcx.emit_node_span_lint(
297 UNUSED_ASSOCIATED_TYPE_BOUNDS,
298 hir_id,
299 span,
300 crate::errors::UnusedAssociatedTypeBounds { span },
301 );
302 }
303 }
304
305 let mut missing_assoc_items = FxIndexSet::default();
317 let projection_bounds: Vec<_> = ordered_associated_items
318 .into_iter()
319 .filter_map(|key @ (def_id, _)| {
320 if let Some(&assoc) = projection_bounds.get(&key) {
321 return Some(assoc);
322 }
323 if !tcx.generics_require_sized_self(def_id) {
324 missing_assoc_items.insert(key);
325 }
326 None
327 })
328 .collect();
329
330 if let Err(guar) = self.check_for_required_assoc_items(
332 principal_trait.as_ref().map_or(smallvec![], |(_, spans)| spans.clone()),
333 missing_assoc_items,
334 potential_assoc_items,
335 hir_bounds,
336 ) {
337 return Ty::new_error(tcx, guar);
338 }
339
340 let mut duplicates = FxHashSet::default();
345 auto_traits.retain(|(trait_pred, _)| duplicates.insert(trait_pred.def_id()));
346
347 debug!(?principal_trait);
348 debug!(?auto_traits);
349
350 let principal_trait_ref = principal_trait.map(|(trait_pred, spans)| {
352 trait_pred.map_bound(|trait_pred| {
353 let trait_ref = trait_pred.trait_ref;
354 assert_eq!(trait_pred.polarity, ty::PredicatePolarity::Positive);
355 assert_eq!(trait_ref.self_ty(), dummy_self);
356
357 let span = *spans.first().unwrap();
358
359 let mut missing_generic_params = Vec::new();
362 let generics = tcx.generics_of(trait_ref.def_id);
363 let args: Vec<_> = trait_ref
364 .args
365 .iter()
366 .enumerate()
367 .skip(1)
369 .map(|(index, arg)| {
370 if arg.walk().any(|arg| arg == dummy_self.into()) {
371 let param = &generics.own_params[index];
372 missing_generic_params.push((param.name, param.kind.clone()));
373 param.to_error(tcx)
374 } else {
375 arg
376 }
377 })
378 .collect();
379
380 let empty_generic_args = hir_bounds.iter().any(|hir_bound| {
381 hir_bound.trait_ref.path.res == Res::Def(DefKind::Trait, trait_ref.def_id)
382 && hir_bound.span.contains(span)
383 });
384 self.report_missing_generic_params(
385 missing_generic_params,
386 trait_ref.def_id,
387 span,
388 empty_generic_args,
389 );
390
391 ty::ExistentialPredicate::Trait(ty::ExistentialTraitRef::new(
392 tcx,
393 trait_ref.def_id,
394 args,
395 ))
396 })
397 });
398
399 let existential_projections = projection_bounds.into_iter().map(|(bound, _)| {
400 bound.map_bound(|mut b| {
401 assert_eq!(b.projection_term.self_ty(), dummy_self);
402
403 let references_self = b.projection_term.args.iter().skip(1).any(|arg| {
406 if arg.walk().any(|arg| arg == dummy_self.into()) {
407 return true;
408 }
409 false
410 });
411 if references_self {
412 let guar = tcx
413 .dcx()
414 .span_delayed_bug(span, "trait object projection bounds reference `Self`");
415 b.projection_term = replace_dummy_self_with_error(tcx, b.projection_term, guar);
416 }
417
418 ty::ExistentialPredicate::Projection(ty::ExistentialProjection::erase_self_ty(
419 tcx, b,
420 ))
421 })
422 });
423
424 let mut auto_trait_predicates: Vec<_> = auto_traits
425 .into_iter()
426 .map(|(trait_pred, _)| {
427 assert_eq!(trait_pred.polarity(), ty::PredicatePolarity::Positive);
428 assert_eq!(trait_pred.self_ty().skip_binder(), dummy_self);
429
430 ty::Binder::dummy(ty::ExistentialPredicate::AutoTrait(trait_pred.def_id()))
431 })
432 .collect();
433 auto_trait_predicates.dedup();
434
435 let mut v = principal_trait_ref
438 .into_iter()
439 .chain(existential_projections)
440 .chain(auto_trait_predicates)
441 .collect::<SmallVec<[_; 8]>>();
442 v.sort_by(|a, b| a.skip_binder().stable_cmp(tcx, &b.skip_binder()));
443 let existential_predicates = tcx.mk_poly_existential_predicates(&v);
444
445 let region_bound = if !lifetime.is_elided() {
447 self.lower_lifetime(lifetime, RegionInferReason::ExplicitObjectLifetime)
448 } else {
449 self.compute_object_lifetime_bound(span, existential_predicates).unwrap_or_else(|| {
450 if tcx.named_bound_var(lifetime.hir_id).is_some() {
452 self.lower_lifetime(lifetime, RegionInferReason::ExplicitObjectLifetime)
453 } else {
454 let reason =
455 if let hir::LifetimeKind::ImplicitObjectLifetimeDefault = lifetime.kind {
456 RegionInferReason::ObjectLifetimeDefault(span.shrink_to_hi())
457 } else {
458 RegionInferReason::ExplicitObjectLifetime
459 };
460 self.re_infer(span, reason)
461 }
462 })
463 };
464 debug!(?region_bound);
465
466 Ty::new_dynamic(tcx, existential_predicates, region_bound)
467 }
468
469 fn check_elaborated_projection_mentions_input_lifetimes(
474 &self,
475 pred: ty::PolyProjectionPredicate<'tcx>,
476 span: Span,
477 supertrait_span: Span,
478 ) {
479 let tcx = self.tcx();
480
481 let late_bound_in_projection_term =
489 tcx.collect_constrained_late_bound_regions(pred.map_bound(|pred| pred.projection_term));
490 let late_bound_in_term =
491 tcx.collect_referenced_late_bound_regions(pred.map_bound(|pred| pred.term));
492 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_trait.rs:492",
"rustc_hir_analysis::hir_ty_lowering::dyn_trait",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_trait.rs"),
::tracing_core::__macro_support::Option::Some(492u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering::dyn_trait"),
::tracing_core::field::FieldSet::new(&["late_bound_in_projection_term"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&late_bound_in_projection_term)
as &dyn Value))])
});
} else { ; }
};debug!(?late_bound_in_projection_term);
493 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_trait.rs:493",
"rustc_hir_analysis::hir_ty_lowering::dyn_trait",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_trait.rs"),
::tracing_core::__macro_support::Option::Some(493u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering::dyn_trait"),
::tracing_core::field::FieldSet::new(&["late_bound_in_term"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&late_bound_in_term)
as &dyn Value))])
});
} else { ; }
};debug!(?late_bound_in_term);
494
495 self.validate_late_bound_regions(
500 late_bound_in_projection_term,
501 late_bound_in_term,
502 |br_name| {
503 let item_name = tcx.item_name(pred.item_def_id());
504 {
self.dcx().struct_span_err(span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("binding for associated type `{0}` references {1}, which does not appear in the trait input types",
item_name, br_name))
})).with_code(E0582)
}struct_span_code_err!(
505 self.dcx(),
506 span,
507 E0582,
508 "binding for associated type `{}` references {}, \
509 which does not appear in the trait input types",
510 item_name,
511 br_name
512 )
513 .with_span_label(supertrait_span, "due to this supertrait")
514 },
515 );
516 }
517
518 fn prohibit_or_lint_bare_trait_object_ty(
523 &self,
524 span: Span,
525 hir_id: hir::HirId,
526 hir_bounds: &[hir::PolyTraitRef<'tcx>],
527 ) -> Option<ErrorGuaranteed> {
528 struct TraitObjectWithoutDyn<'a, 'tcx> {
529 span: Span,
530 hir_id: HirId,
531 sugg: Vec<(Span, String)>,
532 this: &'a dyn HirTyLowerer<'tcx>,
533 }
534
535 impl<'a, 'b, 'tcx> Diagnostic<'a, ()> for TraitObjectWithoutDyn<'b, 'tcx> {
536 fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, ()> {
537 let Self { span, hir_id, sugg, this } = self;
538 let mut lint =
539 Diag::new(dcx, level, "trait objects without an explicit `dyn` are deprecated");
540 if span.can_be_used_for_suggestions() {
541 lint.multipart_suggestion(
542 "if this is a dyn-compatible trait, use `dyn`",
543 sugg,
544 Applicability::MachineApplicable,
545 );
546 }
547 this.maybe_suggest_blanket_trait_impl(span, hir_id, &mut lint);
548 lint
549 }
550 }
551
552 let tcx = self.tcx();
553 let [poly_trait_ref, ..] = hir_bounds else { return None };
554
555 let in_path = match tcx.parent_hir_node(hir_id) {
556 hir::Node::Ty(hir::Ty {
557 kind: hir::TyKind::Path(hir::QPath::TypeRelative(qself, _)),
558 ..
559 })
560 | hir::Node::Expr(hir::Expr {
561 kind: hir::ExprKind::Path(hir::QPath::TypeRelative(qself, _)),
562 ..
563 })
564 | hir::Node::PatExpr(hir::PatExpr {
565 kind: hir::PatExprKind::Path(hir::QPath::TypeRelative(qself, _)),
566 ..
567 }) if qself.hir_id == hir_id => true,
568 _ => false,
569 };
570 let needs_bracket = in_path
571 && !tcx
572 .sess
573 .source_map()
574 .span_to_prev_source(span)
575 .ok()
576 .is_some_and(|s| s.trim_end().ends_with('<'));
577
578 let is_global = poly_trait_ref.trait_ref.path.is_global();
579
580 let mut sugg = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(span.shrink_to_lo(),
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}dyn {1}",
if needs_bracket { "<" } else { "" },
if is_global { "(" } else { "" }))
}))]))vec![(
581 span.shrink_to_lo(),
582 format!(
583 "{}dyn {}",
584 if needs_bracket { "<" } else { "" },
585 if is_global { "(" } else { "" },
586 ),
587 )];
588
589 if is_global || needs_bracket {
590 sugg.push((
591 span.shrink_to_hi(),
592 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}{1}",
if is_global { ")" } else { "" },
if needs_bracket { ">" } else { "" }))
})format!(
593 "{}{}",
594 if is_global { ")" } else { "" },
595 if needs_bracket { ">" } else { "" },
596 ),
597 ));
598 }
599
600 if span.edition().at_least_rust_2021() {
601 let mut diag = {
self.dcx().struct_span_err(span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}",
"expected a type, found a trait"))
})).with_code(E0782)
}rustc_errors::struct_span_code_err!(
602 self.dcx(),
603 span,
604 E0782,
605 "{}",
606 "expected a type, found a trait"
607 );
608 if span.can_be_used_for_suggestions()
609 && poly_trait_ref.trait_ref.trait_def_id().is_some()
610 && !self.maybe_suggest_impl_trait(span, hir_id, hir_bounds, &mut diag)
611 && !self.maybe_suggest_dyn_trait(hir_id, span, sugg, &mut diag)
612 {
613 self.maybe_suggest_add_generic_impl_trait(span, hir_id, &mut diag);
614 }
615 self.maybe_suggest_blanket_trait_impl(span, hir_id, &mut diag);
617 self.maybe_suggest_assoc_ty_bound(hir_id, &mut diag);
618 self.maybe_suggest_typoed_method(
619 hir_id,
620 poly_trait_ref.trait_ref.trait_def_id(),
621 &mut diag,
622 );
623 if let Some(mut sugg) =
626 self.dcx().steal_non_err(span, StashKey::AssociatedTypeSuggestion)
627 && let Suggestions::Enabled(ref mut s1) = diag.suggestions
628 && let Suggestions::Enabled(ref mut s2) = sugg.suggestions
629 {
630 s1.append(s2);
631 sugg.cancel();
632 }
633 Some(diag.emit())
634 } else {
635 tcx.emit_node_span_lint(
636 BARE_TRAIT_OBJECTS,
637 hir_id,
638 span,
639 TraitObjectWithoutDyn { span, hir_id, sugg, this: self },
640 );
641 None
642 }
643 }
644
645 fn maybe_suggest_add_generic_impl_trait(
648 &self,
649 span: Span,
650 hir_id: hir::HirId,
651 diag: &mut Diag<'_>,
652 ) -> bool {
653 let tcx = self.tcx();
654
655 let parent_hir_id = tcx.parent_hir_id(hir_id);
656 let parent_item = tcx.hir_get_parent_item(hir_id).def_id;
657
658 let generics = match tcx.hir_node_by_def_id(parent_item) {
659 hir::Node::Item(hir::Item {
660 kind: hir::ItemKind::Struct(_, generics, variant),
661 ..
662 }) => {
663 if !variant.fields().iter().any(|field| field.hir_id == parent_hir_id) {
664 return false;
665 }
666 generics
667 }
668 hir::Node::Item(hir::Item { kind: hir::ItemKind::Enum(_, generics, def), .. }) => {
669 if !def
670 .variants
671 .iter()
672 .flat_map(|variant| variant.data.fields().iter())
673 .any(|field| field.hir_id == parent_hir_id)
674 {
675 return false;
676 }
677 generics
678 }
679 _ => return false,
680 };
681
682 let Ok(rendered_ty) = tcx.sess.source_map().span_to_snippet(span) else {
683 return false;
684 };
685
686 let param = "TUV"
687 .chars()
688 .map(|c| c.to_string())
689 .chain((0..).map(|i| ::alloc::__export::must_use({ ::alloc::fmt::format(format_args!("P{0}", i)) })format!("P{i}")))
690 .find(|s| !generics.params.iter().any(|param| param.name.ident().as_str() == s))
691 .expect("we definitely can find at least one param name to generate");
692 let mut sugg = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(span, param.to_string())]))vec![(span, param.to_string())];
693 if let Some(insertion_span) = generics.span_for_param_suggestion() {
694 sugg.push((insertion_span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!(", {1}: {0}", rendered_ty, param))
})format!(", {param}: {}", rendered_ty)));
695 } else {
696 sugg.push((generics.where_clause_span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("<{1}: {0}>", rendered_ty, param))
})format!("<{param}: {}>", rendered_ty)));
697 }
698 diag.multipart_suggestion(
699 "you might be missing a type parameter",
700 sugg,
701 Applicability::MachineApplicable,
702 );
703 true
704 }
705
706 fn maybe_suggest_blanket_trait_impl<G: EmissionGuarantee>(
708 &self,
709 span: Span,
710 hir_id: hir::HirId,
711 diag: &mut Diag<'_, G>,
712 ) {
713 let tcx = self.tcx();
714 let parent_id = tcx.hir_get_parent_item(hir_id).def_id;
715 if let hir::Node::Item(hir::Item {
716 kind: hir::ItemKind::Impl(hir::Impl { self_ty: impl_self_ty, of_trait, generics, .. }),
717 ..
718 }) = tcx.hir_node_by_def_id(parent_id)
719 && hir_id == impl_self_ty.hir_id
720 {
721 let Some(of_trait) = of_trait else {
722 diag.span_suggestion_verbose(
723 impl_self_ty.span.shrink_to_hi(),
724 "you might have intended to implement this trait for a given type",
725 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" for /* Type */"))
})format!(" for /* Type */"),
726 Applicability::HasPlaceholders,
727 );
728 return;
729 };
730 if !of_trait.trait_ref.trait_def_id().is_some_and(|def_id| def_id.is_local()) {
731 return;
732 }
733 let of_trait_span = of_trait.trait_ref.path.span;
734 let Ok(of_trait_name) = tcx.sess.source_map().span_to_snippet(of_trait_span) else {
736 return;
737 };
738
739 let Ok(impl_trait_name) = self.tcx().sess.source_map().span_to_snippet(span) else {
740 return;
741 };
742 let sugg = self.add_generic_param_suggestion(generics, span, &impl_trait_name);
743 diag.multipart_suggestion(
744 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("alternatively use a blanket implementation to implement `{0}` for all types that also implement `{1}`",
of_trait_name, impl_trait_name))
})format!(
745 "alternatively use a blanket implementation to implement `{of_trait_name}` for \
746 all types that also implement `{impl_trait_name}`"
747 ),
748 sugg,
749 Applicability::MaybeIncorrect,
750 );
751 }
752 }
753
754 fn maybe_suggest_dyn_trait(
762 &self,
763 hir_id: hir::HirId,
764 span: Span,
765 sugg: Vec<(Span, String)>,
766 diag: &mut Diag<'_>,
767 ) -> bool {
768 let tcx = self.tcx();
769 if span.in_derive_expansion() {
770 return false;
771 }
772
773 match tcx.parent_hir_node(hir_id) {
776 hir::Node::Ty(_)
782 | hir::Node::Expr(_)
783 | hir::Node::PatExpr(_)
784 | hir::Node::PathSegment(_)
785 | hir::Node::AssocItemConstraint(_)
786 | hir::Node::TraitRef(_)
787 | hir::Node::Item(_)
788 | hir::Node::WherePredicate(_) => {}
789
790 hir::Node::Field(field) => {
791 if let hir::Node::Item(hir::Item {
793 kind: hir::ItemKind::Struct(_, _, variant), ..
794 }) = tcx.parent_hir_node(field.hir_id)
795 && variant
796 .fields()
797 .last()
798 .is_some_and(|tail_field| tail_field.hir_id == field.hir_id)
799 {
800 } else {
802 return false;
803 }
804 }
805 _ => return false,
806 }
807
808 diag.multipart_suggestion(
810 "you can add the `dyn` keyword if you want a trait object",
811 sugg,
812 Applicability::MachineApplicable,
813 );
814 true
815 }
816
817 fn add_generic_param_suggestion(
818 &self,
819 generics: &hir::Generics<'_>,
820 self_ty_span: Span,
821 impl_trait_name: &str,
822 ) -> Vec<(Span, String)> {
823 let param_name = generics.params.next_type_param_name(None);
825
826 let add_generic_sugg = if let Some(span) = generics.span_for_param_suggestion() {
827 (span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!(", {0}: {1}", param_name,
impl_trait_name))
})format!(", {param_name}: {impl_trait_name}"))
828 } else {
829 (generics.span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("<{0}: {1}>", param_name,
impl_trait_name))
})format!("<{param_name}: {impl_trait_name}>"))
830 };
831 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(self_ty_span, param_name), add_generic_sugg]))vec![(self_ty_span, param_name), add_generic_sugg]
832 }
833
834 fn maybe_suggest_impl_trait(
836 &self,
837 span: Span,
838 hir_id: hir::HirId,
839 hir_bounds: &[hir::PolyTraitRef<'tcx>],
840 diag: &mut Diag<'_>,
841 ) -> bool {
842 let tcx = self.tcx();
843 let parent_id = tcx.hir_get_parent_item(hir_id).def_id;
844 let (sig, generics) = match tcx.hir_node_by_def_id(parent_id) {
851 hir::Node::Item(hir::Item {
852 kind: hir::ItemKind::Fn { sig, generics, .. }, ..
853 }) => (sig, generics),
854 hir::Node::TraitItem(hir::TraitItem {
855 kind: hir::TraitItemKind::Fn(sig, _),
856 generics,
857 ..
858 }) => (sig, generics),
859 hir::Node::ImplItem(hir::ImplItem {
860 kind: hir::ImplItemKind::Fn(sig, _),
861 generics,
862 ..
863 }) => (sig, generics),
864 _ => return false,
865 };
866 let Ok(trait_name) = tcx.sess.source_map().span_to_snippet(span) else {
867 return false;
868 };
869 let impl_sugg = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(span.shrink_to_lo(), "impl ".to_string())]))vec![(span.shrink_to_lo(), "impl ".to_string())];
870 let is_dyn_compatible = hir_bounds.iter().all(|bound| match bound.trait_ref.path.res {
872 Res::Def(DefKind::Trait, id) => tcx.is_dyn_compatible(id),
873 _ => false,
874 });
875
876 let borrowed = #[allow(non_exhaustive_omitted_patterns)] match tcx.parent_hir_node(hir_id) {
hir::Node::Ty(hir::Ty { kind: hir::TyKind::Ref(..), .. }) => true,
_ => false,
}matches!(
877 tcx.parent_hir_node(hir_id),
878 hir::Node::Ty(hir::Ty { kind: hir::TyKind::Ref(..), .. })
879 );
880
881 if let hir::FnRetTy::Return(ty) = sig.decl.output
883 && ty.peel_refs().hir_id == hir_id
884 {
885 let pre = if !is_dyn_compatible {
886 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` is dyn-incompatible, ",
trait_name))
})format!("`{trait_name}` is dyn-incompatible, ")
887 } else {
888 String::new()
889 };
890 let msg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}use `impl {1}` to return an opaque type, as long as you return a single underlying type",
pre, trait_name))
})format!(
891 "{pre}use `impl {trait_name}` to return an opaque type, as long as you return a \
892 single underlying type",
893 );
894
895 diag.multipart_suggestion(msg, impl_sugg, Applicability::MachineApplicable);
896
897 if is_dyn_compatible {
899 let suggestion = if borrowed {
903 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(ty.span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("Box<dyn {0}>",
trait_name))
}))]))vec![(ty.span, format!("Box<dyn {trait_name}>"))]
904 } else {
905 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(ty.span.shrink_to_lo(), "Box<dyn ".to_string()),
(ty.span.shrink_to_hi(), ">".to_string())]))vec![
906 (ty.span.shrink_to_lo(), "Box<dyn ".to_string()),
907 (ty.span.shrink_to_hi(), ">".to_string()),
908 ]
909 };
910
911 diag.multipart_suggestion(
912 "alternatively, you can return an owned trait object",
913 suggestion,
914 Applicability::MachineApplicable,
915 );
916 }
917 return true;
918 }
919
920 for ty in sig.decl.inputs {
922 if ty.peel_refs().hir_id != hir_id {
923 continue;
924 }
925 let sugg = self.add_generic_param_suggestion(generics, span, &trait_name);
926 diag.multipart_suggestion(
927 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("use a new generic type parameter, constrained by `{0}`",
trait_name))
})format!("use a new generic type parameter, constrained by `{trait_name}`"),
928 sugg,
929 Applicability::MachineApplicable,
930 );
931 diag.multipart_suggestion(
932 "you can also use an opaque type, but users won't be able to specify the type \
933 parameter when calling the `fn`, having to rely exclusively on type inference",
934 impl_sugg,
935 Applicability::MachineApplicable,
936 );
937 if !is_dyn_compatible {
938 diag.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` is dyn-incompatible, otherwise a trait object could be used",
trait_name))
})format!(
939 "`{trait_name}` is dyn-incompatible, otherwise a trait object could be used"
940 ));
941 } else {
942 let (dyn_str, paren_dyn_str) =
944 if borrowed { ("dyn ", "(dyn ") } else { ("&dyn ", "&(dyn ") };
945
946 let sugg = if let [_, _, ..] = hir_bounds {
947 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(span.shrink_to_lo(), paren_dyn_str.to_string()),
(span.shrink_to_hi(), ")".to_string())]))vec![
949 (span.shrink_to_lo(), paren_dyn_str.to_string()),
950 (span.shrink_to_hi(), ")".to_string()),
951 ]
952 } else {
953 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(span.shrink_to_lo(), dyn_str.to_string())]))vec![(span.shrink_to_lo(), dyn_str.to_string())]
954 };
955 diag.multipart_suggestion(
956 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("alternatively, use a trait object to accept any type that implements `{0}`, accessing its methods at runtime using dynamic dispatch",
trait_name))
})format!(
957 "alternatively, use a trait object to accept any type that implements \
958 `{trait_name}`, accessing its methods at runtime using dynamic dispatch",
959 ),
960 sugg,
961 Applicability::MachineApplicable,
962 );
963 }
964 return true;
965 }
966 false
967 }
968
969 fn maybe_suggest_assoc_ty_bound(&self, hir_id: hir::HirId, diag: &mut Diag<'_>) {
970 let mut parents = self.tcx().hir_parent_iter(hir_id);
971
972 if let Some((c_hir_id, hir::Node::AssocItemConstraint(constraint))) = parents.next()
973 && let Some(obj_ty) = constraint.ty()
974 && let Some((_, hir::Node::TraitRef(trait_ref))) = parents.next()
975 {
976 if let Some((_, hir::Node::Ty(ty))) = parents.next()
977 && let hir::TyKind::TraitObject(..) = ty.kind
978 {
979 return;
981 }
982
983 if trait_ref
984 .path
985 .segments
986 .iter()
987 .find_map(|seg| {
988 seg.args.filter(|args| args.constraints.iter().any(|c| c.hir_id == c_hir_id))
989 })
990 .is_none_or(|args| args.parenthesized != hir::GenericArgsParentheses::No)
991 {
992 return;
994 }
995
996 let lo = if constraint.gen_args.span_ext.is_dummy() {
997 constraint.ident.span
998 } else {
999 constraint.gen_args.span_ext
1000 };
1001 let hi = obj_ty.span;
1002
1003 if !lo.eq_ctxt(hi) {
1004 return;
1005 }
1006
1007 diag.span_suggestion_verbose(
1008 lo.between(hi),
1009 "you might have meant to write a bound here",
1010 ": ",
1011 Applicability::MaybeIncorrect,
1012 );
1013 }
1014 }
1015
1016 fn maybe_suggest_typoed_method(
1017 &self,
1018 hir_id: hir::HirId,
1019 trait_def_id: Option<DefId>,
1020 diag: &mut Diag<'_>,
1021 ) {
1022 let tcx = self.tcx();
1023 let Some(trait_def_id) = trait_def_id else {
1024 return;
1025 };
1026 let hir::Node::Expr(hir::Expr {
1027 kind: hir::ExprKind::Path(hir::QPath::TypeRelative(path_ty, segment)),
1028 ..
1029 }) = tcx.parent_hir_node(hir_id)
1030 else {
1031 return;
1032 };
1033 if path_ty.hir_id != hir_id {
1034 return;
1035 }
1036 let names: Vec<_> = tcx
1037 .associated_items(trait_def_id)
1038 .in_definition_order()
1039 .filter(|assoc| assoc.namespace() == hir::def::Namespace::ValueNS)
1040 .map(|cand| cand.name())
1041 .collect();
1042 if let Some(typo) = find_best_match_for_name(&names, segment.ident.name, None) {
1043 diag.span_suggestion_verbose(
1044 segment.ident.span,
1045 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("you may have misspelled this associated item, causing `{0}` to be interpreted as a type rather than a trait",
tcx.item_name(trait_def_id)))
})format!(
1046 "you may have misspelled this associated item, causing `{}` \
1047 to be interpreted as a type rather than a trait",
1048 tcx.item_name(trait_def_id),
1049 ),
1050 typo,
1051 Applicability::MaybeIncorrect,
1052 );
1053 }
1054 }
1055}
1056
1057fn replace_dummy_self_with_error<'tcx, T: TypeFoldable<TyCtxt<'tcx>>>(
1058 tcx: TyCtxt<'tcx>,
1059 t: T,
1060 guar: ErrorGuaranteed,
1061) -> T {
1062 t.fold_with(&mut BottomUpFolder {
1063 tcx,
1064 ty_op: |ty| {
1065 if ty == tcx.types.trait_object_dummy_self { Ty::new_error(tcx, guar) } else { ty }
1066 },
1067 lt_op: |lt| lt,
1068 ct_op: |ct| ct,
1069 })
1070}