1use std::ops::ControlFlow;
2
3use rustc_errors::{Applicability, Diag, E0283, E0284, E0790, MultiSpan, struct_span_code_err};
4use rustc_hir as hir;
5use rustc_hir::LangItem;
6use rustc_hir::def::{DefKind, Res};
7use rustc_hir::def_id::{CRATE_DEF_ID, DefId};
8use rustc_hir::intravisit::Visitor as _;
9use rustc_infer::infer::{BoundRegionConversionTime, InferCtxt};
10use rustc_infer::traits::util::elaborate;
11use rustc_infer::traits::{
12 Obligation, ObligationCause, ObligationCauseCode, PolyTraitObligation, PredicateObligation,
13};
14use rustc_middle::ty::print::PrintPolyTraitPredicateExt;
15use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitable as _, TypeVisitableExt as _, Unnormalized};
16use rustc_session::errors::feature_err_unstable_feature_bound;
17use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span};
18use tracing::{debug, instrument};
19
20use crate::error_reporting::TypeErrCtxt;
21use crate::error_reporting::infer::need_type_info::TypeAnnotationNeeded;
22use crate::error_reporting::traits::{FindExprBySpan, to_pretty_impl_header};
23use crate::traits::ObligationCtxt;
24use crate::traits::query::evaluate_obligation::InferCtxtExt;
25
26#[derive(#[automatically_derived]
impl ::core::fmt::Debug for CandidateSource {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
CandidateSource::DefId(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "DefId",
&__self_0),
CandidateSource::ParamEnv(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"ParamEnv", &__self_0),
}
}
}Debug)]
27pub enum CandidateSource {
28 DefId(DefId),
29 ParamEnv(Span),
30}
31
32pub fn compute_applicable_impls_for_diagnostics<'tcx>(
33 infcx: &InferCtxt<'tcx>,
34 obligation: &PolyTraitObligation<'tcx>,
35 ignore_predicates_of_impls: bool,
36) -> Vec<CandidateSource> {
37 let tcx = infcx.tcx;
38 let param_env = obligation.param_env;
39
40 let predicate_polarity = obligation.predicate.skip_binder().polarity;
41
42 let impl_may_apply = |impl_def_id| {
43 let ocx = ObligationCtxt::new(infcx);
44 infcx.enter_forall(obligation.predicate, |placeholder_obligation| {
45 let obligation_trait_ref = ocx.normalize(
46 &ObligationCause::dummy(),
47 param_env,
48 Unnormalized::new_wip(placeholder_obligation.trait_ref),
49 );
50
51 let impl_args = infcx.fresh_args_for_item(DUMMY_SP, impl_def_id);
52 let impl_trait_ref =
53 tcx.impl_trait_ref(impl_def_id).instantiate(tcx, impl_args).skip_norm_wip();
54 let impl_trait_ref = ocx.normalize(
55 &ObligationCause::dummy(),
56 param_env,
57 Unnormalized::new_wip(impl_trait_ref),
58 );
59
60 if let Err(_) =
61 ocx.eq(&ObligationCause::dummy(), param_env, obligation_trait_ref, impl_trait_ref)
62 {
63 return false;
64 }
65
66 let impl_trait_header = tcx.impl_trait_header(impl_def_id);
67 let impl_polarity = impl_trait_header.polarity;
68
69 match (impl_polarity, predicate_polarity) {
70 (ty::ImplPolarity::Positive, ty::PredicatePolarity::Positive)
71 | (ty::ImplPolarity::Negative, ty::PredicatePolarity::Negative) => {}
72 _ => return false,
73 }
74
75 if !ignore_predicates_of_impls {
76 let obligations = tcx
77 .predicates_of(impl_def_id)
78 .instantiate(tcx, impl_args)
79 .into_iter()
80 .map(|(predicate, _)| {
81 Obligation::new(
82 tcx,
83 ObligationCause::dummy(),
84 param_env,
85 predicate.skip_norm_wip(),
86 )
87 })
88 .filter(|obligation| {
93 infcx.next_trait_solver() || infcx.evaluate_obligation(obligation).is_ok()
94 });
95 ocx.register_obligations(obligations);
96 }
97
98 ocx.try_evaluate_obligations().is_empty()
99 })
100 };
101
102 let param_env_candidate_may_apply = |poly_trait_predicate: ty::PolyTraitPredicate<'tcx>| {
103 let ocx = ObligationCtxt::new(infcx);
104 infcx.enter_forall(obligation.predicate, |placeholder_obligation| {
105 let obligation_trait_ref = ocx.normalize(
106 &ObligationCause::dummy(),
107 param_env,
108 Unnormalized::new_wip(placeholder_obligation.trait_ref),
109 );
110
111 let param_env_predicate = infcx.instantiate_binder_with_fresh_vars(
112 DUMMY_SP,
113 BoundRegionConversionTime::HigherRankedType,
114 poly_trait_predicate,
115 );
116 let param_env_trait_ref = ocx.normalize(
117 &ObligationCause::dummy(),
118 param_env,
119 Unnormalized::new_wip(param_env_predicate.trait_ref),
120 );
121
122 if let Err(_) = ocx.eq(
123 &ObligationCause::dummy(),
124 param_env,
125 obligation_trait_ref,
126 param_env_trait_ref,
127 ) {
128 return false;
129 }
130
131 ocx.try_evaluate_obligations().is_empty()
132 })
133 };
134
135 let mut ambiguities = Vec::new();
136
137 tcx.for_each_relevant_impl(
138 obligation.predicate.def_id(),
139 obligation.predicate.skip_binder().trait_ref.self_ty(),
140 |impl_def_id| {
141 if infcx.probe(|_| impl_may_apply(impl_def_id)) {
142 ambiguities.push(CandidateSource::DefId(impl_def_id))
143 }
144 },
145 );
146
147 let body_id = obligation.cause.body_id;
153 if body_id != CRATE_DEF_ID {
154 let predicates = tcx.predicates_of(body_id.to_def_id()).instantiate_identity(tcx);
155 for (pred, span) in
156 elaborate(tcx, predicates.into_iter().map(|(c, s)| (c.skip_norm_wip(), s)))
157 {
158 let kind = pred.kind();
159 if let ty::ClauseKind::Trait(trait_pred) = kind.skip_binder()
160 && param_env_candidate_may_apply(kind.rebind(trait_pred))
161 {
162 if kind.rebind(trait_pred.trait_ref)
163 == ty::Binder::dummy(ty::TraitRef::identity(tcx, trait_pred.def_id()))
164 {
165 ambiguities.push(CandidateSource::ParamEnv(tcx.def_span(trait_pred.def_id())))
166 } else {
167 ambiguities.push(CandidateSource::ParamEnv(span))
168 }
169 }
170 }
171 }
172
173 ambiguities
174}
175
176impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
177 #[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("maybe_report_ambiguity",
"rustc_trait_selection::error_reporting::traits::ambiguity",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/ambiguity.rs"),
::tracing_core::__macro_support::Option::Some(177u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::ambiguity"),
::tracing_core::field::FieldSet::new(&["obligation"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&obligation)
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: ErrorGuaranteed = loop {};
return __tracing_attr_fake_return;
}
{
let predicate =
self.resolve_vars_if_possible(obligation.predicate);
let span = obligation.cause.span;
let mut long_ty_path = None;
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/ambiguity.rs:191",
"rustc_trait_selection::error_reporting::traits::ambiguity",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/ambiguity.rs"),
::tracing_core::__macro_support::Option::Some(191u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::ambiguity"),
::tracing_core::field::FieldSet::new(&["predicate",
"obligation.cause.code"],
::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(&predicate)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&obligation.cause.code())
as &dyn Value))])
});
} else { ; }
};
let bound_predicate = predicate.kind();
let mut err =
match bound_predicate.skip_binder() {
ty::PredicateKind::Clause(ty::ClauseKind::Trait(data)) => {
let trait_pred = bound_predicate.rebind(data);
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/traits/ambiguity.rs:200",
"rustc_trait_selection::error_reporting::traits::ambiguity",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/traits/ambiguity.rs"),
::tracing_core::__macro_support::Option::Some(200u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::traits::ambiguity"),
::tracing_core::field::FieldSet::new(&["trait_pred"],
::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(&trait_pred)
as &dyn Value))])
});
} else { ; }
};
if let Err(e) = predicate.error_reported() { return e; }
if let Err(guar) =
self.tcx.ensure_result().coherent_trait(trait_pred.def_id())
{
return guar;
}
if #[allow(non_exhaustive_omitted_patterns)] match self.tcx.as_lang_item(trait_pred.def_id())
{
Some(LangItem::Sized | LangItem::MetaSized) => true,
_ => false,
} {
return match self.tainted_by_errors() {
None =>
self.emit_inference_failure_err(obligation.cause.body_id,
span, trait_pred.self_ty().skip_binder().into(),
TypeAnnotationNeeded::E0282, false).emit(),
Some(e) => e,
};
}
let term =
data.trait_ref.args.iter().filter_map(ty::GenericArg::as_term).find(|s|
s.has_non_region_infer());
let mut err =
if let Some(term) = term {
let candidates: Vec<_> =
self.tcx.all_impls(trait_pred.def_id()).filter_map(|def_id|
{
let imp = self.tcx.impl_trait_header(def_id);
if imp.polarity != ty::ImplPolarity::Positive ||
!self.tcx.is_user_visible_dep(def_id.krate) {
return None;
}
let imp = imp.trait_ref.skip_binder();
if imp.with_replaced_self_ty(self.tcx,
trait_pred.skip_binder().self_ty()) ==
trait_pred.skip_binder().trait_ref {
Some(imp.self_ty())
} else { None }
}).collect();
self.emit_inference_failure_err_with_type_hint(obligation.cause.body_id,
span, term, TypeAnnotationNeeded::E0283, true,
match &candidates[..] {
[candidate] => Some(*candidate),
_ => None,
})
} else {
{
self.dcx().struct_span_err(span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("type annotations needed: cannot satisfy `{0}`",
self.tcx.short_string(predicate, &mut long_ty_path)))
})).with_code(E0283)
}.with_long_ty_path(long_ty_path)
};
let mut ambiguities =
compute_applicable_impls_for_diagnostics(self.infcx,
&obligation.with(self.tcx, trait_pred), false);
let has_non_region_infer =
trait_pred.skip_binder().trait_ref.args.types().any(|t|
!t.is_ty_or_numeric_infer());
if ambiguities.len() > 5 {
let infcx = self.infcx;
if !ambiguities.iter().all(|option|
match option {
CandidateSource::DefId(did) =>
infcx.tcx.generics_of(*did).count() == 0,
CandidateSource::ParamEnv(_) => true,
}) {
ambiguities.retain(|option|
match option {
CandidateSource::DefId(did) =>
infcx.tcx.generics_of(*did).count() == 0,
CandidateSource::ParamEnv(_) => true,
});
}
}
if ambiguities.len() > 1 && ambiguities.len() < 10 &&
has_non_region_infer {
if let Some(e) = self.tainted_by_errors() && term.is_none()
{
err.cancel();
return e;
}
self.annotate_source_of_ambiguity(&mut err, &ambiguities,
predicate);
} else {
if let Some(e) = self.tainted_by_errors() {
err.cancel();
return e;
}
if let Some(clause) = predicate.as_trait_clause() &&
let ty::Infer(_) = clause.self_ty().skip_binder().kind() {
let tr =
self.tcx.short_string(clause.print_modifiers_and_trait_path(),
&mut err.long_ty_path());
err.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the type must implement `{0}`",
tr))
}));
} else {
let pred =
self.tcx.short_string(predicate, &mut err.long_ty_path());
err.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("cannot satisfy `{0}`",
pred))
}));
}
let impl_candidates =
self.find_similar_impl_candidates(predicate.as_trait_clause().unwrap());
if impl_candidates.len() < 40 {
self.report_similar_impl_candidates(impl_candidates.as_slice(),
obligation, trait_pred, obligation.cause.body_id, &mut err,
false, obligation.param_env);
}
}
if let ObligationCauseCode::WhereClause(def_id, _) |
ObligationCauseCode::WhereClauseInExpr(def_id, ..) =
*obligation.cause.code() {
self.suggest_fully_qualified_path(&mut err, def_id, span,
trait_pred.def_id());
}
if term.is_some_and(|term| term.as_type().is_some()) &&
let Some(body) =
self.tcx.hir_maybe_body_owned_by(obligation.cause.body_id) {
let mut expr_finder = FindExprBySpan::new(span, self.tcx);
expr_finder.visit_expr(&body.value);
if let Some(hir::Expr {
kind: hir::ExprKind::Call(hir::Expr {
kind: hir::ExprKind::Path(hir::QPath::Resolved(None, path)),
.. }, _) |
hir::ExprKind::Path(hir::QPath::Resolved(None, path)), .. })
= expr_finder.result &&
let [.., trait_path_segment @ hir::PathSegment {
res: Res::Def(DefKind::Trait, trait_id), .. },
hir::PathSegment {
ident: assoc_item_ident, res: Res::Def(_, item_id), .. }] =
path.segments && data.trait_ref.def_id == *trait_id &&
self.tcx.trait_of_assoc(*item_id) == Some(*trait_id) &&
let None = self.tainted_by_errors() {
let assoc_item = self.tcx.associated_item(*item_id);
let (verb, noun) =
match assoc_item.kind {
ty::AssocKind::Const { .. } => ("refer to the", "constant"),
ty::AssocKind::Fn { .. } => ("call", "function"),
ty::AssocKind::Type { .. } => ("refer to the", "type"),
};
err.cancel();
err =
self.dcx().struct_span_err(span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("cannot {0} associated {1} on trait without specifying the corresponding `impl` type",
verb, noun))
}));
err.code(E0790);
if item_id.is_local() {
let trait_ident = self.tcx.item_name(*trait_id);
err.span_label(self.tcx.def_span(*item_id),
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}::{1}` defined here",
trait_ident, assoc_item_ident))
}));
}
err.span_label(span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("cannot {0} associated {1} of trait",
verb, noun))
}));
let trait_impls =
self.tcx.trait_impls_of(data.trait_ref.def_id);
if let Some(&impl_def_id) =
trait_impls.non_blanket_impls().values().flatten().next() {
let non_blanket_impl_count =
trait_impls.non_blanket_impls().values().flatten().count();
let (message, self_types) =
if non_blanket_impl_count == 1 {
("use the fully-qualified path to the only available \
implementation",
::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}",
self.tcx.type_of(impl_def_id).instantiate_identity().skip_norm_wip()))
})])))
} else if non_blanket_impl_count < 20 {
("use a fully-qualified path to one of the available \
implementations",
trait_impls.non_blanket_impls().values().flatten().map(|&id|
{
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}",
self.tcx.type_of(id).instantiate_identity().skip_norm_wip()))
})
}).collect::<Vec<String>>())
} else {
("use a fully-qualified path to a specific available \
implementation",
::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
["/* self type */".to_string()])))
};
let suggestions: Vec<_> =
self_types.into_iter().map(|self_type|
{
let mut suggestions =
::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(path.span.shrink_to_lo(),
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("<{0} as ", self_type))
}))]));
if let Some(generic_arg) = trait_path_segment.args {
let between_span =
trait_path_segment.ident.span.between(generic_arg.span_ext);
suggestions.push((between_span, "".to_string()));
suggestions.push((generic_arg.span_ext.shrink_to_hi(),
">".to_string()));
} else {
suggestions.push((trait_path_segment.ident.span.shrink_to_hi(),
">".to_string()));
}
suggestions
}).collect();
err.multipart_suggestions(message, suggestions,
Applicability::MaybeIncorrect);
}
}
};
err
}
ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(term))
=> {
if let Err(e) = term.error_reported() { return e; }
if let Some(e) = self.tainted_by_errors() { return e; }
self.emit_inference_failure_err(obligation.cause.body_id,
span, term, TypeAnnotationNeeded::E0282, false)
}
ty::PredicateKind::Subtype(data) => {
if let Err(e) = data.error_reported() { return e; }
if let Some(e) = self.tainted_by_errors() { return e; }
let ty::SubtypePredicate { a_is_expected: _, a, b } = data;
if !(a.is_ty_var() && b.is_ty_var()) {
::core::panicking::panic("assertion failed: a.is_ty_var() && b.is_ty_var()")
};
self.emit_inference_failure_err(obligation.cause.body_id,
span, a.into(), TypeAnnotationNeeded::E0282, true)
}
ty::PredicateKind::Clause(ty::ClauseKind::Projection(data))
=> {
if let Err(e) = predicate.error_reported() { return e; }
if let Some(e) = self.tainted_by_errors() { return e; }
if data.projection_term.kind.is_trait_projection() &&
let Err(guar) =
self.tcx.ensure_result().coherent_trait(self.tcx.parent(data.def_id()))
{
return guar;
}
let term =
data.projection_term.args.iter().filter_map(ty::GenericArg::as_term).chain([data.term]).find(|g|
g.has_non_region_infer());
let predicate =
self.tcx.short_string(predicate, &mut long_ty_path);
if let Some(term) = term {
self.emit_inference_failure_err(obligation.cause.body_id,
span, term, TypeAnnotationNeeded::E0284,
true).with_note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("cannot satisfy `{0}`",
predicate))
})).with_long_ty_path(long_ty_path)
} else {
{
self.dcx().struct_span_err(span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("type annotations needed: cannot satisfy `{0}`",
predicate))
})).with_code(E0284)
}.with_span_label(span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("cannot satisfy `{0}`",
predicate))
})).with_long_ty_path(long_ty_path)
}
}
ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(data))
=> {
if let Err(e) = predicate.error_reported() { return e; }
if let Some(e) = self.tainted_by_errors() { return e; }
let term =
data.walk().filter_map(ty::GenericArg::as_term).find(|term|
term.is_infer());
if let Some(term) = term {
self.emit_inference_failure_err(obligation.cause.body_id,
span, term, TypeAnnotationNeeded::E0284, true)
} else {
let predicate =
self.tcx.short_string(predicate, &mut long_ty_path);
{
self.dcx().struct_span_err(span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("type annotations needed: cannot satisfy `{0}`",
predicate))
})).with_code(E0284)
}.with_span_label(span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("cannot satisfy `{0}`",
predicate))
})).with_long_ty_path(long_ty_path)
}
}
ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(ct,
..)) =>
self.emit_inference_failure_err(obligation.cause.body_id,
span, ct.into(), TypeAnnotationNeeded::E0284, true),
ty::PredicateKind::NormalizesTo(ty::NormalizesTo {
alias, term }) if term.is_infer() => {
if let Some(e) = self.tainted_by_errors() { return e; }
let alias = self.tcx.short_string(alias, &mut long_ty_path);
{
self.dcx().struct_span_err(span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("type annotations needed: cannot normalize `{0}`",
alias))
})).with_code(E0284)
}.with_span_label(span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("cannot normalize `{0}`",
alias))
})).with_long_ty_path(long_ty_path)
}
ty::PredicateKind::Clause(ty::ClauseKind::UnstableFeature(sym))
=> {
if let Some(e) = self.tainted_by_errors() { return e; }
if self.tcx.features().staged_api() {
self.dcx().struct_span_err(span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("unstable feature `{0}` is used without being enabled.",
sym))
})).with_help(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("The feature can be enabled by marking the current item with `#[unstable_feature_bound({0})]`",
sym))
}))
} else {
feature_err_unstable_feature_bound(&self.tcx.sess, sym,
span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("use of unstable library feature `{0}`",
sym))
}))
}
}
_ => {
if let Some(e) = self.tainted_by_errors() { return e; }
let predicate =
self.tcx.short_string(predicate, &mut long_ty_path);
{
self.dcx().struct_span_err(span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("type annotations needed: cannot satisfy `{0}`",
predicate))
})).with_code(E0284)
}.with_span_label(span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("cannot satisfy `{0}`",
predicate))
})).with_long_ty_path(long_ty_path)
}
};
self.note_obligation_cause(&mut err, obligation);
err.emit()
}
}
}#[instrument(skip(self), level = "debug")]
178 pub(super) fn maybe_report_ambiguity(
179 &self,
180 obligation: &PredicateObligation<'tcx>,
181 ) -> ErrorGuaranteed {
182 let predicate = self.resolve_vars_if_possible(obligation.predicate);
188 let span = obligation.cause.span;
189 let mut long_ty_path = None;
190
191 debug!(?predicate, obligation.cause.code = ?obligation.cause.code());
192
193 let bound_predicate = predicate.kind();
197 let mut err = match bound_predicate.skip_binder() {
198 ty::PredicateKind::Clause(ty::ClauseKind::Trait(data)) => {
199 let trait_pred = bound_predicate.rebind(data);
200 debug!(?trait_pred);
201
202 if let Err(e) = predicate.error_reported() {
203 return e;
204 }
205
206 if let Err(guar) = self.tcx.ensure_result().coherent_trait(trait_pred.def_id()) {
207 return guar;
210 }
211
212 if matches!(
228 self.tcx.as_lang_item(trait_pred.def_id()),
229 Some(LangItem::Sized | LangItem::MetaSized)
230 ) {
231 return match self.tainted_by_errors() {
232 None => self
233 .emit_inference_failure_err(
234 obligation.cause.body_id,
235 span,
236 trait_pred.self_ty().skip_binder().into(),
237 TypeAnnotationNeeded::E0282,
238 false,
239 )
240 .emit(),
241 Some(e) => e,
242 };
243 }
244
245 let term = data
259 .trait_ref
260 .args
261 .iter()
262 .filter_map(ty::GenericArg::as_term)
263 .find(|s| s.has_non_region_infer());
264
265 let mut err = if let Some(term) = term {
266 let candidates: Vec<_> = self
267 .tcx
268 .all_impls(trait_pred.def_id())
269 .filter_map(|def_id| {
270 let imp = self.tcx.impl_trait_header(def_id);
271 if imp.polarity != ty::ImplPolarity::Positive
272 || !self.tcx.is_user_visible_dep(def_id.krate)
273 {
274 return None;
275 }
276 let imp = imp.trait_ref.skip_binder();
277 if imp
278 .with_replaced_self_ty(self.tcx, trait_pred.skip_binder().self_ty())
279 == trait_pred.skip_binder().trait_ref
280 {
281 Some(imp.self_ty())
282 } else {
283 None
284 }
285 })
286 .collect();
287 self.emit_inference_failure_err_with_type_hint(
288 obligation.cause.body_id,
289 span,
290 term,
291 TypeAnnotationNeeded::E0283,
292 true,
293 match &candidates[..] {
294 [candidate] => Some(*candidate),
295 _ => None,
296 },
297 )
298 } else {
299 struct_span_code_err!(
300 self.dcx(),
301 span,
302 E0283,
303 "type annotations needed: cannot satisfy `{}`",
304 self.tcx.short_string(predicate, &mut long_ty_path),
305 )
306 .with_long_ty_path(long_ty_path)
307 };
308
309 let mut ambiguities = compute_applicable_impls_for_diagnostics(
310 self.infcx,
311 &obligation.with(self.tcx, trait_pred),
312 false,
313 );
314 let has_non_region_infer = trait_pred
315 .skip_binder()
316 .trait_ref
317 .args
318 .types()
319 .any(|t| !t.is_ty_or_numeric_infer());
320 if ambiguities.len() > 5 {
324 let infcx = self.infcx;
325 if !ambiguities.iter().all(|option| match option {
326 CandidateSource::DefId(did) => infcx.tcx.generics_of(*did).count() == 0,
327 CandidateSource::ParamEnv(_) => true,
328 }) {
329 ambiguities.retain(|option| match option {
331 CandidateSource::DefId(did) => infcx.tcx.generics_of(*did).count() == 0,
332 CandidateSource::ParamEnv(_) => true,
333 });
334 }
335 }
336 if ambiguities.len() > 1 && ambiguities.len() < 10 && has_non_region_infer {
337 if let Some(e) = self.tainted_by_errors()
338 && term.is_none()
339 {
340 err.cancel();
345 return e;
346 }
347 self.annotate_source_of_ambiguity(&mut err, &ambiguities, predicate);
348 } else {
349 if let Some(e) = self.tainted_by_errors() {
350 err.cancel();
351 return e;
352 }
353 if let Some(clause) = predicate.as_trait_clause()
354 && let ty::Infer(_) = clause.self_ty().skip_binder().kind()
355 {
356 let tr = self.tcx.short_string(
357 clause.print_modifiers_and_trait_path(),
358 &mut err.long_ty_path(),
359 );
360 err.note(format!("the type must implement `{tr}`"));
361 } else {
362 let pred = self.tcx.short_string(predicate, &mut err.long_ty_path());
363 err.note(format!("cannot satisfy `{pred}`"));
364 }
365 let impl_candidates =
366 self.find_similar_impl_candidates(predicate.as_trait_clause().unwrap());
367 if impl_candidates.len() < 40 {
368 self.report_similar_impl_candidates(
369 impl_candidates.as_slice(),
370 obligation,
371 trait_pred,
372 obligation.cause.body_id,
373 &mut err,
374 false,
375 obligation.param_env,
376 );
377 }
378 }
379
380 if let ObligationCauseCode::WhereClause(def_id, _)
381 | ObligationCauseCode::WhereClauseInExpr(def_id, ..) = *obligation.cause.code()
382 {
383 self.suggest_fully_qualified_path(&mut err, def_id, span, trait_pred.def_id());
384 }
385
386 if term.is_some_and(|term| term.as_type().is_some())
387 && let Some(body) = self.tcx.hir_maybe_body_owned_by(obligation.cause.body_id)
388 {
389 let mut expr_finder = FindExprBySpan::new(span, self.tcx);
390 expr_finder.visit_expr(&body.value);
391
392 if let Some(hir::Expr {
393 kind:
394 hir::ExprKind::Call(
395 hir::Expr {
396 kind: hir::ExprKind::Path(hir::QPath::Resolved(None, path)),
397 ..
398 },
399 _,
400 )
401 | hir::ExprKind::Path(hir::QPath::Resolved(None, path)),
402 ..
403 }) = expr_finder.result
404 && let [
405 ..,
406 trait_path_segment @ hir::PathSegment {
407 res: Res::Def(DefKind::Trait, trait_id),
408 ..
409 },
410 hir::PathSegment {
411 ident: assoc_item_ident,
412 res: Res::Def(_, item_id),
413 ..
414 },
415 ] = path.segments
416 && data.trait_ref.def_id == *trait_id
417 && self.tcx.trait_of_assoc(*item_id) == Some(*trait_id)
418 && let None = self.tainted_by_errors()
419 {
420 let assoc_item = self.tcx.associated_item(*item_id);
421 let (verb, noun) = match assoc_item.kind {
422 ty::AssocKind::Const { .. } => ("refer to the", "constant"),
423 ty::AssocKind::Fn { .. } => ("call", "function"),
424 ty::AssocKind::Type { .. } => ("refer to the", "type"),
427 };
428
429 err.cancel();
431 err = self.dcx().struct_span_err(
432 span,
433 format!(
434 "cannot {verb} associated {noun} on trait without specifying the \
435 corresponding `impl` type",
436 ),
437 );
438 err.code(E0790);
439
440 if item_id.is_local() {
441 let trait_ident = self.tcx.item_name(*trait_id);
442 err.span_label(
443 self.tcx.def_span(*item_id),
444 format!("`{trait_ident}::{assoc_item_ident}` defined here"),
445 );
446 }
447
448 err.span_label(span, format!("cannot {verb} associated {noun} of trait"));
449
450 let trait_impls = self.tcx.trait_impls_of(data.trait_ref.def_id);
451
452 if let Some(&impl_def_id) =
453 trait_impls.non_blanket_impls().values().flatten().next()
454 {
455 let non_blanket_impl_count =
456 trait_impls.non_blanket_impls().values().flatten().count();
457 let (message, self_types) = if non_blanket_impl_count == 1 {
460 (
461 "use the fully-qualified path to the only available \
462 implementation",
463 vec![format!(
464 "{}",
465 self.tcx
466 .type_of(impl_def_id)
467 .instantiate_identity()
468 .skip_norm_wip()
469 )],
470 )
471 } else if non_blanket_impl_count < 20 {
472 (
473 "use a fully-qualified path to one of the available \
474 implementations",
475 trait_impls
476 .non_blanket_impls()
477 .values()
478 .flatten()
479 .map(|&id| {
480 format!(
481 "{}",
482 self.tcx
483 .type_of(id)
484 .instantiate_identity()
485 .skip_norm_wip()
486 )
487 })
488 .collect::<Vec<String>>(),
489 )
490 } else {
491 (
492 "use a fully-qualified path to a specific available \
493 implementation",
494 vec!["/* self type */".to_string()],
495 )
496 };
497 let suggestions: Vec<_> = self_types
498 .into_iter()
499 .map(|self_type| {
500 let mut suggestions = vec![(
501 path.span.shrink_to_lo(),
502 format!("<{self_type} as "),
503 )];
504 if let Some(generic_arg) = trait_path_segment.args {
505 let between_span = trait_path_segment
506 .ident
507 .span
508 .between(generic_arg.span_ext);
509 suggestions.push((between_span, "".to_string()));
512 suggestions.push((
513 generic_arg.span_ext.shrink_to_hi(),
514 ">".to_string(),
515 ));
516 } else {
517 suggestions.push((
518 trait_path_segment.ident.span.shrink_to_hi(),
519 ">".to_string(),
520 ));
521 }
522 suggestions
523 })
524 .collect();
525 err.multipart_suggestions(
526 message,
527 suggestions,
528 Applicability::MaybeIncorrect,
529 );
530 }
531 }
532 };
533
534 err
535 }
536
537 ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(term)) => {
538 if let Err(e) = term.error_reported() {
542 return e;
543 }
544 if let Some(e) = self.tainted_by_errors() {
545 return e;
546 }
547
548 self.emit_inference_failure_err(
549 obligation.cause.body_id,
550 span,
551 term,
552 TypeAnnotationNeeded::E0282,
553 false,
554 )
555 }
556
557 ty::PredicateKind::Subtype(data) => {
558 if let Err(e) = data.error_reported() {
559 return e;
560 }
561 if let Some(e) = self.tainted_by_errors() {
562 return e;
563 }
564 let ty::SubtypePredicate { a_is_expected: _, a, b } = data;
565 assert!(a.is_ty_var() && b.is_ty_var());
567 self.emit_inference_failure_err(
568 obligation.cause.body_id,
569 span,
570 a.into(),
571 TypeAnnotationNeeded::E0282,
572 true,
573 )
574 }
575
576 ty::PredicateKind::Clause(ty::ClauseKind::Projection(data)) => {
577 if let Err(e) = predicate.error_reported() {
578 return e;
579 }
580 if let Some(e) = self.tainted_by_errors() {
581 return e;
582 }
583
584 if data.projection_term.kind.is_trait_projection()
585 && let Err(guar) =
586 self.tcx.ensure_result().coherent_trait(self.tcx.parent(data.def_id()))
587 {
588 return guar;
591 }
592 let term = data
593 .projection_term
594 .args
595 .iter()
596 .filter_map(ty::GenericArg::as_term)
597 .chain([data.term])
598 .find(|g| g.has_non_region_infer());
599 let predicate = self.tcx.short_string(predicate, &mut long_ty_path);
600 if let Some(term) = term {
601 self.emit_inference_failure_err(
602 obligation.cause.body_id,
603 span,
604 term,
605 TypeAnnotationNeeded::E0284,
606 true,
607 )
608 .with_note(format!("cannot satisfy `{predicate}`"))
609 .with_long_ty_path(long_ty_path)
610 } else {
611 struct_span_code_err!(
613 self.dcx(),
614 span,
615 E0284,
616 "type annotations needed: cannot satisfy `{predicate}`",
617 )
618 .with_span_label(span, format!("cannot satisfy `{predicate}`"))
619 .with_long_ty_path(long_ty_path)
620 }
621 }
622
623 ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(data)) => {
624 if let Err(e) = predicate.error_reported() {
625 return e;
626 }
627 if let Some(e) = self.tainted_by_errors() {
628 return e;
629 }
630 let term =
631 data.walk().filter_map(ty::GenericArg::as_term).find(|term| term.is_infer());
632 if let Some(term) = term {
633 self.emit_inference_failure_err(
634 obligation.cause.body_id,
635 span,
636 term,
637 TypeAnnotationNeeded::E0284,
638 true,
639 )
640 } else {
641 let predicate = self.tcx.short_string(predicate, &mut long_ty_path);
643 struct_span_code_err!(
644 self.dcx(),
645 span,
646 E0284,
647 "type annotations needed: cannot satisfy `{predicate}`",
648 )
649 .with_span_label(span, format!("cannot satisfy `{predicate}`"))
650 .with_long_ty_path(long_ty_path)
651 }
652 }
653
654 ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(ct, ..)) => self
655 .emit_inference_failure_err(
656 obligation.cause.body_id,
657 span,
658 ct.into(),
659 TypeAnnotationNeeded::E0284,
660 true,
661 ),
662
663 ty::PredicateKind::NormalizesTo(ty::NormalizesTo { alias, term })
664 if term.is_infer() =>
665 {
666 if let Some(e) = self.tainted_by_errors() {
667 return e;
668 }
669 let alias = self.tcx.short_string(alias, &mut long_ty_path);
670 struct_span_code_err!(
671 self.dcx(),
672 span,
673 E0284,
674 "type annotations needed: cannot normalize `{alias}`",
675 )
676 .with_span_label(span, format!("cannot normalize `{alias}`"))
677 .with_long_ty_path(long_ty_path)
678 }
679
680 ty::PredicateKind::Clause(ty::ClauseKind::UnstableFeature(sym)) => {
681 if let Some(e) = self.tainted_by_errors() {
682 return e;
683 }
684
685 if self.tcx.features().staged_api() {
686 self.dcx().struct_span_err(
687 span,
688 format!("unstable feature `{sym}` is used without being enabled."),
689 ).with_help(format!("The feature can be enabled by marking the current item with `#[unstable_feature_bound({sym})]`"))
690 } else {
691 feature_err_unstable_feature_bound(
692 &self.tcx.sess,
693 sym,
694 span,
695 format!("use of unstable library feature `{sym}`"),
696 )
697 }
698 }
699
700 _ => {
701 if let Some(e) = self.tainted_by_errors() {
702 return e;
703 }
704 let predicate = self.tcx.short_string(predicate, &mut long_ty_path);
705 struct_span_code_err!(
706 self.dcx(),
707 span,
708 E0284,
709 "type annotations needed: cannot satisfy `{predicate}`",
710 )
711 .with_span_label(span, format!("cannot satisfy `{predicate}`"))
712 .with_long_ty_path(long_ty_path)
713 }
714 };
715 self.note_obligation_cause(&mut err, obligation);
716 err.emit()
717 }
718
719 fn annotate_source_of_ambiguity(
720 &self,
721 err: &mut Diag<'_>,
722 ambiguities: &[CandidateSource],
723 predicate: ty::Predicate<'tcx>,
724 ) {
725 let mut spans = ::alloc::vec::Vec::new()vec![];
726 let mut crates = ::alloc::vec::Vec::new()vec![];
727 let mut post = ::alloc::vec::Vec::new()vec![];
728 let mut has_param_env = false;
729 for ambiguity in ambiguities {
730 match ambiguity {
731 CandidateSource::DefId(impl_def_id) => match self.tcx.span_of_impl(*impl_def_id) {
732 Ok(span) => spans.push(span),
733 Err(name) => {
734 crates.push(name);
735 if let Some(header) = to_pretty_impl_header(self.tcx, *impl_def_id) {
736 post.push(header);
737 }
738 }
739 },
740 CandidateSource::ParamEnv(span) => {
741 has_param_env = true;
742 spans.push(*span);
743 }
744 }
745 }
746 let mut crate_names: Vec<_> = crates.iter().map(|n| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}`", n))
})format!("`{n}`")).collect();
747 crate_names.sort();
748 crate_names.dedup();
749 post.sort();
750 post.dedup();
751
752 if self.tainted_by_errors().is_some()
753 && (crate_names.len() == 1
754 && spans.len() == 0
755 && ["`core`", "`alloc`", "`std`"].contains(&crate_names[0].as_str())
756 || predicate.visit_with(&mut HasNumericInferVisitor).is_break())
757 {
758 err.downgrade_to_delayed_bug();
764 return;
765 }
766
767 let msg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("multiple `impl`s{0} satisfying `{1}` found",
if has_param_env { " or `where` clauses" } else { "" },
predicate))
})format!(
768 "multiple `impl`s{} satisfying `{}` found",
769 if has_param_env { " or `where` clauses" } else { "" },
770 predicate
771 );
772 let post = if post.len() > 1 || (post.len() == 1 && post[0].contains('\n')) {
773 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!(":\n{0}",
post.iter().map(|p|
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("- {0}", p))
})).collect::<Vec<_>>().join("\n")))
})format!(":\n{}", post.iter().map(|p| format!("- {p}")).collect::<Vec<_>>().join("\n"))
774 } else if post.len() == 1 {
775 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!(": `{0}`", post[0]))
})format!(": `{}`", post[0])
776 } else {
777 String::new()
778 };
779
780 match (spans.len(), crates.len(), crate_names.len()) {
781 (0, 0, 0) => {
782 err.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("cannot satisfy `{0}`", predicate))
})format!("cannot satisfy `{predicate}`"));
783 }
784 (0, _, 1) => {
785 err.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{1} in the `{0}` crate{2}",
crates[0], msg, post))
})format!("{msg} in the `{}` crate{post}", crates[0]));
786 }
787 (0, _, _) => {
788 err.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} in the following crates: {1}{2}",
msg, crate_names.join(", "), post))
})format!(
789 "{} in the following crates: {}{}",
790 msg,
791 crate_names.join(", "),
792 post,
793 ));
794 }
795 (_, 0, 0) => {
796 let span: MultiSpan = spans.into();
797 err.span_note(span, msg);
798 }
799 (_, 1, 1) => {
800 let span: MultiSpan = spans.into();
801 err.span_note(span, msg);
802 err.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("and another `impl` found in the `{0}` crate{1}",
crates[0], post))
})format!("and another `impl` found in the `{}` crate{post}", crates[0]));
803 }
804 _ => {
805 let span: MultiSpan = spans.into();
806 err.span_note(span, msg);
807 err.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("and more `impl`s found in the following crates: {0}{1}",
crate_names.join(", "), post))
})format!(
808 "and more `impl`s found in the following crates: {}{}",
809 crate_names.join(", "),
810 post,
811 ));
812 }
813 }
814 }
815}
816
817struct HasNumericInferVisitor;
818
819impl<'tcx> ty::TypeVisitor<TyCtxt<'tcx>> for HasNumericInferVisitor {
820 type Result = ControlFlow<()>;
821
822 fn visit_ty(&mut self, ty: Ty<'tcx>) -> Self::Result {
823 if #[allow(non_exhaustive_omitted_patterns)] match ty.kind() {
ty::Infer(ty::FloatVar(_) | ty::IntVar(_)) => true,
_ => false,
}matches!(ty.kind(), ty::Infer(ty::FloatVar(_) | ty::IntVar(_))) {
824 ControlFlow::Break(())
825 } else {
826 ControlFlow::Continue(())
827 }
828 }
829}