1use rustc_errors::Applicability::{MachineApplicable, MaybeIncorrect};
2use rustc_errors::{Diag, MultiSpan, pluralize};
3use rustc_hir as hir;
4use rustc_hir::def::DefKind;
5use rustc_hir::find_attr;
6use rustc_middle::traits::{ObligationCause, ObligationCauseCode};
7use rustc_middle::ty::error::{ExpectedFound, TypeError};
8use rustc_middle::ty::fast_reject::DeepRejectCtxt;
9use rustc_middle::ty::print::{FmtPrinter, Printer};
10use rustc_middle::ty::{self, Ty, suggest_constraining_type_param};
11use rustc_span::def_id::DefId;
12use rustc_span::{BytePos, Span, Symbol};
13use tracing::debug;
14
15use crate::error_reporting::TypeErrCtxt;
16use crate::infer::InferCtxtExt;
17
18impl<'tcx> TypeErrCtxt<'_, 'tcx> {
19 pub fn note_and_explain_type_err(
20 &self,
21 diag: &mut Diag<'_>,
22 err: TypeError<'tcx>,
23 cause: &ObligationCause<'tcx>,
24 sp: Span,
25 body_owner_def_id: DefId,
26 ) {
27 {
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/infer/note_and_explain.rs:27",
"rustc_trait_selection::error_reporting::infer::note_and_explain",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs"),
::tracing_core::__macro_support::Option::Some(27u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer::note_and_explain"),
::tracing_core::field::FieldSet::new(&["message"],
::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(&format_args!("note_and_explain_type_err err={0:?} cause={1:?}",
err, cause) as &dyn Value))])
});
} else { ; }
};debug!("note_and_explain_type_err err={:?} cause={:?}", err, cause);
28
29 let tcx = self.tcx;
30
31 match err {
32 TypeError::ArgumentSorts(values, _) | TypeError::Sorts(values) => {
33 match (*values.expected.kind(), *values.found.kind()) {
34 (ty::Closure(..), ty::Closure(..)) => {
35 diag.note("no two closures, even if identical, have the same type");
36 diag.help("consider boxing your closure and/or using it as a trait object");
37 }
38 (ty::Coroutine(def_id1, ..), ty::Coroutine(def_id2, ..))
39 if self.tcx.coroutine_is_async(def_id1)
40 && self.tcx.coroutine_is_async(def_id2) =>
41 {
42 diag.note("no two async blocks, even if identical, have the same type");
43 diag.help(
44 "consider pinning your async block and casting it to a trait object",
45 );
46 }
47 (ty::Alias(ty::Opaque, ..), ty::Alias(ty::Opaque, ..)) => {
48 diag.note("distinct uses of `impl Trait` result in different opaque types");
50 }
51 (ty::Float(_), ty::Infer(ty::IntVar(_)))
52 if let Ok(
53 snippet,
55 ) = tcx.sess.source_map().span_to_snippet(sp) =>
56 {
57 if snippet.chars().all(|c| c.is_digit(10) || c == '-' || c == '_') {
58 diag.span_suggestion_verbose(
59 sp.shrink_to_hi(),
60 "use a float literal",
61 ".0",
62 MachineApplicable,
63 );
64 }
65 }
66 (ty::Param(expected), ty::Param(found)) => {
67 let generics = tcx.generics_of(body_owner_def_id);
68 let e_span = tcx.def_span(generics.type_param(expected, tcx).def_id);
69 if !sp.contains(e_span) {
70 diag.span_label(e_span, "expected type parameter");
71 }
72 let f_span = tcx.def_span(generics.type_param(found, tcx).def_id);
73 if !sp.contains(f_span) {
74 diag.span_label(f_span, "found type parameter");
75 }
76 diag.note(
77 "a type parameter was expected, but a different one was found; \
78 you might be missing a type parameter or trait bound",
79 );
80 diag.note(
81 "for more information, visit \
82 https://doc.rust-lang.org/book/ch10-02-traits.html\
83 #traits-as-parameters",
84 );
85 }
86 (
87 ty::Alias(ty::Projection | ty::Inherent, _),
88 ty::Alias(ty::Projection | ty::Inherent, _),
89 ) => {
90 diag.note("an associated type was expected, but a different one was found");
91 }
92 (ty::Param(p), ty::Alias(ty::Projection, proj))
94 | (ty::Alias(ty::Projection, proj), ty::Param(p))
95 if !tcx.is_impl_trait_in_trait(proj.def_id) =>
96 {
97 let param = tcx.generics_of(body_owner_def_id).type_param(p, tcx);
98 let p_def_id = param.def_id;
99 let p_span = tcx.def_span(p_def_id);
100 let expected = match (values.expected.kind(), values.found.kind()) {
101 (ty::Param(_), _) => "expected ",
102 (_, ty::Param(_)) => "found ",
103 _ => "",
104 };
105 if !sp.contains(p_span) {
106 diag.span_label(p_span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}this type parameter", expected))
})format!("{expected}this type parameter"));
107 }
108 let parent = p_def_id.as_local().and_then(|id| {
109 let local_id = tcx.local_def_id_to_hir_id(id);
110 let generics = tcx.parent_hir_node(local_id).generics()?;
111 Some((id, generics))
112 });
113 let mut note = true;
114 if let Some((local_id, generics)) = parent {
115 let (trait_ref, assoc_args) = proj.trait_ref_and_own_args(tcx);
118 let item_name = tcx.item_name(proj.def_id);
119 let item_args = self.format_generic_args(assoc_args);
120
121 let mut matching_span = None;
127 let mut matched_end_of_args = false;
128 for bound in generics.bounds_for_param(local_id) {
129 let potential_spans = bound.bounds.iter().find_map(|bound| {
130 let bound_trait_path = bound.trait_ref()?.path;
131 let def_id = bound_trait_path.res.opt_def_id()?;
132 let generic_args = bound_trait_path
133 .segments
134 .iter()
135 .last()
136 .map(|path| path.args());
137 (def_id == trait_ref.def_id)
138 .then_some((bound_trait_path.span, generic_args))
139 });
140
141 if let Some((end_of_trait, end_of_args)) = potential_spans {
142 let args_span = end_of_args.and_then(|args| args.span());
143 matched_end_of_args = args_span.is_some();
144 matching_span = args_span
145 .or_else(|| Some(end_of_trait))
146 .map(|span| span.shrink_to_hi());
147 break;
148 }
149 }
150
151 if matched_end_of_args {
152 let path = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!(", {0}{1} = {2}", item_name,
item_args, p))
})format!(", {item_name}{item_args} = {p}");
154 note = !suggest_constraining_type_param(
155 tcx,
156 generics,
157 diag,
158 &proj.self_ty().to_string(),
159 &path,
160 None,
161 matching_span,
162 );
163 } else {
164 let path = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("<{0}{1} = {2}>", item_name,
item_args, p))
})format!("<{item_name}{item_args} = {p}>");
168 note = !suggest_constraining_type_param(
169 tcx,
170 generics,
171 diag,
172 &proj.self_ty().to_string(),
173 &path,
174 None,
175 matching_span,
176 );
177 }
178 }
179 if note {
180 diag.note("you might be missing a type parameter or trait bound");
181 }
182 }
183 (ty::Param(p), ty::Dynamic(..) | ty::Alias(ty::Opaque, ..))
184 | (ty::Dynamic(..) | ty::Alias(ty::Opaque, ..), ty::Param(p)) => {
185 let generics = tcx.generics_of(body_owner_def_id);
186 let p_span = tcx.def_span(generics.type_param(p, tcx).def_id);
187 let expected = match (values.expected.kind(), values.found.kind()) {
188 (ty::Param(_), _) => "expected ",
189 (_, ty::Param(_)) => "found ",
190 _ => "",
191 };
192 if !sp.contains(p_span) {
193 diag.span_label(p_span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}this type parameter", expected))
})format!("{expected}this type parameter"));
194 }
195 diag.help("type parameters must be constrained to match other types");
196 if diag.code.is_some_and(|code| tcx.sess.teach(code)) {
197 diag.help(
198 "given a type parameter `T` and a method `foo`:
199```
200trait Trait<T> { fn foo(&self) -> T; }
201```
202the only ways to implement method `foo` are:
203- constrain `T` with an explicit type:
204```
205impl Trait<String> for X {
206 fn foo(&self) -> String { String::new() }
207}
208```
209- add a trait bound to `T` and call a method on that trait that returns `Self`:
210```
211impl<T: std::default::Default> Trait<T> for X {
212 fn foo(&self) -> T { <T as std::default::Default>::default() }
213}
214```
215- change `foo` to return an argument of type `T`:
216```
217impl<T> Trait<T> for X {
218 fn foo(&self, x: T) -> T { x }
219}
220```",
221 );
222 }
223 diag.note(
224 "for more information, visit \
225 https://doc.rust-lang.org/book/ch10-02-traits.html\
226 #traits-as-parameters",
227 );
228 }
229 (
230 ty::Param(p),
231 ty::Closure(..) | ty::CoroutineClosure(..) | ty::Coroutine(..),
232 ) => {
233 let generics = tcx.generics_of(body_owner_def_id);
234 let p_span = tcx.def_span(generics.type_param(p, tcx).def_id);
235 if !sp.contains(p_span) {
236 diag.span_label(p_span, "expected this type parameter");
237 }
238 diag.help(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("every closure has a distinct type and so could not always match the caller-chosen type of parameter `{0}`",
p))
})format!(
239 "every closure has a distinct type and so could not always match the \
240 caller-chosen type of parameter `{p}`"
241 ));
242 }
243 (ty::Param(p), _) | (_, ty::Param(p)) => {
244 let generics = tcx.generics_of(body_owner_def_id);
245 let p_span = tcx.def_span(generics.type_param(p, tcx).def_id);
246 let expected = match (values.expected.kind(), values.found.kind()) {
247 (ty::Param(_), _) => "expected ",
248 (_, ty::Param(_)) => "found ",
249 _ => "",
250 };
251 if !sp.contains(p_span) {
252 diag.span_label(p_span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}this type parameter", expected))
})format!("{expected}this type parameter"));
253 }
254 }
255 (ty::Alias(ty::Projection | ty::Inherent, proj_ty), _)
256 if !tcx.is_impl_trait_in_trait(proj_ty.def_id) =>
257 {
258 self.expected_projection(
259 diag,
260 proj_ty,
261 values,
262 body_owner_def_id,
263 cause.code(),
264 );
265 }
266 (_, ty::Alias(ty::Projection | ty::Inherent, proj_ty))
267 if !tcx.is_impl_trait_in_trait(proj_ty.def_id) =>
268 {
269 let msg = || {
270 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("consider constraining the associated type `{0}` to `{1}`",
values.found, values.expected))
})format!(
271 "consider constraining the associated type `{}` to `{}`",
272 values.found, values.expected,
273 )
274 };
275 if !(self.suggest_constraining_opaque_associated_type(
276 diag,
277 msg,
278 proj_ty,
279 values.expected,
280 ) || self.suggest_constraint(
281 diag,
282 &msg,
283 body_owner_def_id,
284 proj_ty,
285 values.expected,
286 )) {
287 diag.help(msg());
288 diag.note(
289 "for more information, visit \
290 https://doc.rust-lang.org/book/ch19-03-advanced-traits.html",
291 );
292 }
293 }
294 (ty::Dynamic(t, _), ty::Alias(ty::Opaque, alias))
295 if let Some(def_id) = t.principal_def_id()
296 && tcx
297 .explicit_item_self_bounds(alias.def_id)
298 .skip_binder()
299 .iter()
300 .any(|(pred, _span)| match pred.kind().skip_binder() {
301 ty::ClauseKind::Trait(trait_predicate)
302 if trait_predicate.polarity
303 == ty::PredicatePolarity::Positive =>
304 {
305 trait_predicate.def_id() == def_id
306 }
307 _ => false,
308 }) =>
309 {
310 diag.help(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("you can box the `{0}` to coerce it to `Box<{1}>`, but you\'ll have to change the expected type as well",
values.found, values.expected))
})format!(
311 "you can box the `{}` to coerce it to `Box<{}>`, but you'll have to \
312 change the expected type as well",
313 values.found, values.expected,
314 ));
315 }
316 (ty::Dynamic(t, _), _) if let Some(def_id) = t.principal_def_id() => {
317 let mut has_matching_impl = false;
318 tcx.for_each_relevant_impl(def_id, values.found, |did| {
319 if DeepRejectCtxt::relate_rigid_infer(tcx)
320 .types_may_unify(values.found, tcx.type_of(did).skip_binder())
321 {
322 has_matching_impl = true;
323 }
324 });
325 if has_matching_impl {
326 let trait_name = tcx.item_name(def_id);
327 diag.help(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` implements `{1}` so you could box the found value and coerce it to the trait object `Box<dyn {1}>`, you will have to change the expected type as well",
values.found, trait_name))
})format!(
328 "`{}` implements `{trait_name}` so you could box the found value \
329 and coerce it to the trait object `Box<dyn {trait_name}>`, you \
330 will have to change the expected type as well",
331 values.found,
332 ));
333 }
334 }
335 (_, ty::Dynamic(t, _)) if let Some(def_id) = t.principal_def_id() => {
336 let mut has_matching_impl = false;
337 tcx.for_each_relevant_impl(def_id, values.expected, |did| {
338 if DeepRejectCtxt::relate_rigid_infer(tcx)
339 .types_may_unify(values.expected, tcx.type_of(did).skip_binder())
340 {
341 has_matching_impl = true;
342 }
343 });
344 if has_matching_impl {
345 let trait_name = tcx.item_name(def_id);
346 diag.help(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` implements `{1}` so you could change the expected type to `Box<dyn {1}>`",
values.expected, trait_name))
})format!(
347 "`{}` implements `{trait_name}` so you could change the expected \
348 type to `Box<dyn {trait_name}>`",
349 values.expected,
350 ));
351 }
352 }
353 (_, ty::Alias(ty::Opaque, opaque_ty))
354 | (ty::Alias(ty::Opaque, opaque_ty), _) => {
355 if opaque_ty.def_id.is_local()
356 && #[allow(non_exhaustive_omitted_patterns)] match tcx.def_kind(body_owner_def_id)
{
DefKind::Fn | DefKind::Static { .. } | DefKind::Const | DefKind::AssocFn |
DefKind::AssocConst => true,
_ => false,
}matches!(
357 tcx.def_kind(body_owner_def_id),
358 DefKind::Fn
359 | DefKind::Static { .. }
360 | DefKind::Const
361 | DefKind::AssocFn
362 | DefKind::AssocConst
363 )
364 && #[allow(non_exhaustive_omitted_patterns)] match tcx.opaque_ty_origin(opaque_ty.def_id)
{
hir::OpaqueTyOrigin::TyAlias { .. } => true,
_ => false,
}matches!(
365 tcx.opaque_ty_origin(opaque_ty.def_id),
366 hir::OpaqueTyOrigin::TyAlias { .. }
367 )
368 && !tcx
369 .opaque_types_defined_by(body_owner_def_id.expect_local())
370 .contains(&opaque_ty.def_id.expect_local())
371 {
372 let sp = tcx
373 .def_ident_span(body_owner_def_id)
374 .unwrap_or_else(|| tcx.def_span(body_owner_def_id));
375 let mut alias_def_id = opaque_ty.def_id;
376 while let DefKind::OpaqueTy = tcx.def_kind(alias_def_id) {
377 alias_def_id = tcx.parent(alias_def_id);
378 }
379 let opaque_path = tcx.def_path_str(alias_def_id);
380 match tcx.opaque_ty_origin(opaque_ty.def_id) {
382 rustc_hir::OpaqueTyOrigin::FnReturn { .. } => {}
383 rustc_hir::OpaqueTyOrigin::AsyncFn { .. } => {}
384 rustc_hir::OpaqueTyOrigin::TyAlias {
385 in_assoc_ty: false, ..
386 } => {
387 diag.span_note(
388 sp,
389 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("this item must have a `#[define_opaque({0})]` attribute to be able to define hidden types",
opaque_path))
})format!("this item must have a `#[define_opaque({opaque_path})]` \
390 attribute to be able to define hidden types"),
391 );
392 }
393 rustc_hir::OpaqueTyOrigin::TyAlias {
394 in_assoc_ty: true, ..
395 } => {}
396 }
397 }
398 let ObligationCauseCode::IfExpression { expr_id, .. } = cause.code() else {
401 return;
402 };
403 let hir::Node::Expr(&hir::Expr {
404 kind:
405 hir::ExprKind::If(
406 _,
407 &hir::Expr {
408 kind:
409 hir::ExprKind::Block(
410 &hir::Block { expr: Some(then), .. },
411 _,
412 ),
413 ..
414 },
415 Some(&hir::Expr {
416 kind:
417 hir::ExprKind::Block(
418 &hir::Block { expr: Some(else_), .. },
419 _,
420 ),
421 ..
422 }),
423 ),
424 ..
425 }) = self.tcx.hir_node(*expr_id)
426 else {
427 return;
428 };
429 let expected = match values.found.kind() {
430 ty::Alias(..) => values.expected,
431 _ => values.found,
432 };
433 let preds = tcx.explicit_item_self_bounds(opaque_ty.def_id);
434 for (pred, _span) in preds.skip_binder() {
435 let ty::ClauseKind::Trait(trait_predicate) = pred.kind().skip_binder()
436 else {
437 continue;
438 };
439 if trait_predicate.polarity != ty::PredicatePolarity::Positive {
440 continue;
441 }
442 let def_id = trait_predicate.def_id();
443 let mut impl_def_ids = ::alloc::vec::Vec::new()vec![];
444 tcx.for_each_relevant_impl(def_id, expected, |did| {
445 impl_def_ids.push(did)
446 });
447 if let [_] = &impl_def_ids[..] {
448 let trait_name = tcx.item_name(def_id);
449 diag.multipart_suggestion(
450 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` implements `{1}` so you can box both arms and coerce to the trait object `Box<dyn {1}>`",
expected, trait_name))
})format!(
451 "`{expected}` implements `{trait_name}` so you can box \
452 both arms and coerce to the trait object \
453 `Box<dyn {trait_name}>`",
454 ),
455 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(then.span.shrink_to_lo(), "Box::new(".to_string()),
(then.span.shrink_to_hi(),
::alloc::__export::must_use({
::alloc::fmt::format(format_args!(") as Box<dyn {0}>",
tcx.def_path_str(def_id)))
})), (else_.span.shrink_to_lo(), "Box::new(".to_string()),
(else_.span.shrink_to_hi(), ")".to_string())]))vec![
456 (then.span.shrink_to_lo(), "Box::new(".to_string()),
457 (
458 then.span.shrink_to_hi(),
459 format!(") as Box<dyn {}>", tcx.def_path_str(def_id)),
460 ),
461 (else_.span.shrink_to_lo(), "Box::new(".to_string()),
462 (else_.span.shrink_to_hi(), ")".to_string()),
463 ],
464 MachineApplicable,
465 );
466 }
467 }
468 }
469 (ty::FnPtr(_, hdr), ty::FnDef(def_id, _))
470 | (ty::FnDef(def_id, _), ty::FnPtr(_, hdr)) => {
471 if tcx.fn_sig(def_id).skip_binder().safety() < hdr.safety {
472 if !tcx.codegen_fn_attrs(def_id).safe_target_features {
473 diag.note(
474 "unsafe functions cannot be coerced into safe function pointers",
475 );
476 }
477 }
478 }
479 (ty::Adt(_, _), ty::Adt(def, args))
480 if let ObligationCauseCode::IfExpression { expr_id, .. } = cause.code()
481 && let hir::Node::Expr(if_expr) = self.tcx.hir_node(*expr_id)
482 && let hir::ExprKind::If(_, then_expr, _) = if_expr.kind
483 && let hir::ExprKind::Block(blk, _) = then_expr.kind
484 && let Some(then) = blk.expr
485 && def.is_box()
486 && let boxed_ty = args.type_at(0)
487 && let ty::Dynamic(t, _) = boxed_ty.kind()
488 && let Some(def_id) = t.principal_def_id()
489 && let mut impl_def_ids = ::alloc::vec::Vec::new()vec![]
490 && let _ =
491 tcx.for_each_relevant_impl(def_id, values.expected, |did| {
492 impl_def_ids.push(did)
493 })
494 && let [_] = &impl_def_ids[..] =>
495 {
496 diag.multipart_suggestion(
499 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` implements `{1}` so you can box it to coerce to the trait object `{2}`",
values.expected, tcx.item_name(def_id), values.found))
})format!(
500 "`{}` implements `{}` so you can box it to coerce to the trait \
501 object `{}`",
502 values.expected,
503 tcx.item_name(def_id),
504 values.found,
505 ),
506 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(then.span.shrink_to_lo(), "Box::new(".to_string()),
(then.span.shrink_to_hi(), ")".to_string())]))vec![
507 (then.span.shrink_to_lo(), "Box::new(".to_string()),
508 (then.span.shrink_to_hi(), ")".to_string()),
509 ],
510 MachineApplicable,
511 );
512 }
513 _ => {}
514 }
515 {
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/infer/note_and_explain.rs:515",
"rustc_trait_selection::error_reporting::infer::note_and_explain",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs"),
::tracing_core::__macro_support::Option::Some(515u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer::note_and_explain"),
::tracing_core::field::FieldSet::new(&["message"],
::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(&format_args!("note_and_explain_type_err expected={0:?} ({1:?}) found={2:?} ({3:?})",
values.expected, values.expected.kind(), values.found,
values.found.kind()) as &dyn Value))])
});
} else { ; }
};debug!(
516 "note_and_explain_type_err expected={:?} ({:?}) found={:?} ({:?})",
517 values.expected,
518 values.expected.kind(),
519 values.found,
520 values.found.kind(),
521 );
522 }
523 TypeError::CyclicTy(ty) => {
524 if ty.is_closure() || ty.is_coroutine() || ty.is_coroutine_closure() {
526 diag.note(
527 "closures cannot capture themselves or take themselves as argument;\n\
528 this error may be the result of a recent compiler bug-fix,\n\
529 see issue #46062 <https://github.com/rust-lang/rust/issues/46062>\n\
530 for more information",
531 );
532 }
533 }
534 TypeError::TargetFeatureCast(def_id) => {
535 let target_spans = {
#[allow(deprecated)]
{
{
'done:
{
for i in tcx.get_all_attrs(def_id) {
#[allow(unused_imports)]
use rustc_hir::attrs::AttributeKind::*;
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(TargetFeature {
attr_span: span, was_forced: false, .. }) => {
break 'done Some(*span);
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
}
}find_attr!(tcx, def_id, TargetFeature{attr_span: span, was_forced: false, ..} => *span);
536 diag.note(
537 "functions with `#[target_feature]` can only be coerced to `unsafe` function pointers"
538 );
539 diag.span_labels(target_spans, "`#[target_feature]` added here");
540 }
541 _ => {}
542 }
543 }
544
545 fn suggest_constraint(
546 &self,
547 diag: &mut Diag<'_>,
548 msg: impl Fn() -> String,
549 body_owner_def_id: DefId,
550 proj_ty: ty::AliasTy<'tcx>,
551 ty: Ty<'tcx>,
552 ) -> bool {
553 let tcx = self.tcx;
554 let assoc = tcx.associated_item(proj_ty.def_id);
555 let (trait_ref, assoc_args) = proj_ty.trait_ref_and_own_args(tcx);
556 let Some(item) = tcx.hir_get_if_local(body_owner_def_id) else {
557 return false;
558 };
559 let Some(hir_generics) = item.generics() else {
560 return false;
561 };
562 let ty::Param(param_ty) = *proj_ty.self_ty().kind() else {
565 return false;
566 };
567 let generics = tcx.generics_of(body_owner_def_id);
568 let def_id = generics.type_param(param_ty, tcx).def_id;
569 let Some(def_id) = def_id.as_local() else {
570 return false;
571 };
572
573 for pred in hir_generics.bounds_for_param(def_id) {
576 if self.constrain_generic_bound_associated_type_structured_suggestion(
577 diag,
578 trait_ref,
579 pred.bounds,
580 assoc,
581 assoc_args,
582 ty,
583 &msg,
584 false,
585 ) {
586 return true;
587 }
588 }
589 if (param_ty.index as usize) >= generics.parent_count {
590 return false;
592 }
593 let hir_id = match item {
595 hir::Node::ImplItem(item) => item.hir_id(),
596 hir::Node::TraitItem(item) => item.hir_id(),
597 _ => return false,
598 };
599 let parent = tcx.hir_get_parent_item(hir_id).def_id;
600 self.suggest_constraint(diag, msg, parent.into(), proj_ty, ty)
601 }
602
603 fn expected_projection(
617 &self,
618 diag: &mut Diag<'_>,
619 proj_ty: ty::AliasTy<'tcx>,
620 values: ExpectedFound<Ty<'tcx>>,
621 body_owner_def_id: DefId,
622 cause_code: &ObligationCauseCode<'_>,
623 ) {
624 let tcx = self.tcx;
625
626 if self
628 .tcx
629 .erase_and_anonymize_regions(values.found)
630 .contains(self.tcx.erase_and_anonymize_regions(values.expected))
631 {
632 return;
633 }
634
635 let msg = || {
636 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("consider constraining the associated type `{0}` to `{1}`",
values.expected, values.found))
})format!(
637 "consider constraining the associated type `{}` to `{}`",
638 values.expected, values.found
639 )
640 };
641
642 let body_owner = tcx.hir_get_if_local(body_owner_def_id);
643 let current_method_ident = body_owner.and_then(|n| n.ident()).map(|i| i.name);
644
645 let callable_scope = #[allow(non_exhaustive_omitted_patterns)] match body_owner {
Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn { .. }, .. }) |
hir::Node::TraitItem(hir::TraitItem {
kind: hir::TraitItemKind::Fn(..), .. }) |
hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Fn(..),
.. })) => true,
_ => false,
}matches!(
647 body_owner,
648 Some(
649 hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn { .. }, .. })
650 | hir::Node::TraitItem(hir::TraitItem { kind: hir::TraitItemKind::Fn(..), .. })
651 | hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Fn(..), .. }),
652 )
653 );
654 let impl_comparison = #[allow(non_exhaustive_omitted_patterns)] match cause_code {
ObligationCauseCode::CompareImplItem { .. } => true,
_ => false,
}matches!(cause_code, ObligationCauseCode::CompareImplItem { .. });
655 if impl_comparison {
656 } else {
659 let point_at_assoc_fn = if callable_scope
660 && self.point_at_methods_that_satisfy_associated_type(
661 diag,
662 tcx.parent(proj_ty.def_id),
663 current_method_ident,
664 proj_ty.def_id,
665 values.expected,
666 ) {
667 true
672 } else {
673 false
674 };
675 if self.suggest_constraint(diag, &msg, body_owner_def_id, proj_ty, values.found)
678 || point_at_assoc_fn
679 {
680 return;
681 }
682 }
683
684 self.suggest_constraining_opaque_associated_type(diag, &msg, proj_ty, values.found);
685
686 if self.point_at_associated_type(diag, body_owner_def_id, values.found) {
687 return;
688 }
689
690 if !impl_comparison {
691 if callable_scope {
693 diag.help(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} or calling a method that returns `{1}`",
msg(), values.expected))
})format!(
694 "{} or calling a method that returns `{}`",
695 msg(),
696 values.expected
697 ));
698 } else {
699 diag.help(msg());
700 }
701 diag.note(
702 "for more information, visit \
703 https://doc.rust-lang.org/book/ch19-03-advanced-traits.html",
704 );
705 }
706 if diag.code.is_some_and(|code| tcx.sess.teach(code)) {
707 diag.help(
708 "given an associated type `T` and a method `foo`:
709```
710trait Trait {
711type T;
712fn foo(&self) -> Self::T;
713}
714```
715the only way of implementing method `foo` is to constrain `T` with an explicit associated type:
716```
717impl Trait for X {
718type T = String;
719fn foo(&self) -> Self::T { String::new() }
720}
721```",
722 );
723 }
724 }
725
726 fn suggest_constraining_opaque_associated_type(
729 &self,
730 diag: &mut Diag<'_>,
731 msg: impl Fn() -> String,
732 proj_ty: ty::AliasTy<'tcx>,
733 ty: Ty<'tcx>,
734 ) -> bool {
735 let tcx = self.tcx;
736
737 let assoc = tcx.associated_item(proj_ty.def_id);
738 if let ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }) = *proj_ty.self_ty().kind() {
739 let opaque_local_def_id = def_id.as_local();
740 let opaque_hir_ty = if let Some(opaque_local_def_id) = opaque_local_def_id {
741 tcx.hir_expect_opaque_ty(opaque_local_def_id)
742 } else {
743 return false;
744 };
745
746 let (trait_ref, assoc_args) = proj_ty.trait_ref_and_own_args(tcx);
747
748 self.constrain_generic_bound_associated_type_structured_suggestion(
749 diag,
750 trait_ref,
751 opaque_hir_ty.bounds,
752 assoc,
753 assoc_args,
754 ty,
755 msg,
756 true,
757 )
758 } else {
759 false
760 }
761 }
762
763 fn point_at_methods_that_satisfy_associated_type(
764 &self,
765 diag: &mut Diag<'_>,
766 assoc_container_id: DefId,
767 current_method_ident: Option<Symbol>,
768 proj_ty_item_def_id: DefId,
769 expected: Ty<'tcx>,
770 ) -> bool {
771 let tcx = self.tcx;
772
773 let items = tcx.associated_items(assoc_container_id);
774 let methods: Vec<(Span, String)> = items
778 .in_definition_order()
779 .filter(|item| {
780 item.is_fn()
781 && Some(item.name()) != current_method_ident
782 && !tcx.is_doc_hidden(item.def_id)
783 })
784 .filter_map(|item| {
785 let method = tcx.fn_sig(item.def_id).instantiate_identity();
786 match *method.output().skip_binder().kind() {
787 ty::Alias(ty::Projection, ty::AliasTy { def_id: item_def_id, .. })
788 if item_def_id == proj_ty_item_def_id =>
789 {
790 Some((
791 tcx.def_span(item.def_id),
792 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("consider calling `{0}`",
tcx.def_path_str(item.def_id)))
})format!("consider calling `{}`", tcx.def_path_str(item.def_id)),
793 ))
794 }
795 _ => None,
796 }
797 })
798 .collect();
799 if !methods.is_empty() {
800 let mut span: MultiSpan =
803 methods.iter().map(|(sp, _)| *sp).collect::<Vec<Span>>().into();
804 let msg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} method{1} {2} available that return{3} `{4}`",
if methods.len() == 1 { "a" } else { "some" },
if methods.len() == 1 { "" } else { "s" },
if methods.len() == 1 { "is" } else { "are" },
if methods.len() == 1 { "s" } else { "" }, expected))
})format!(
805 "{some} method{s} {are} available that return{r} `{ty}`",
806 some = if methods.len() == 1 { "a" } else { "some" },
807 s = pluralize!(methods.len()),
808 are = pluralize!("is", methods.len()),
809 r = if methods.len() == 1 { "s" } else { "" },
810 ty = expected
811 );
812 for (sp, label) in methods.into_iter() {
813 span.push_span_label(sp, label);
814 }
815 diag.span_help(span, msg);
816 return true;
817 }
818 false
819 }
820
821 fn point_at_associated_type(
822 &self,
823 diag: &mut Diag<'_>,
824 body_owner_def_id: DefId,
825 found: Ty<'tcx>,
826 ) -> bool {
827 let tcx = self.tcx;
828
829 let Some(def_id) = body_owner_def_id.as_local() else {
830 return false;
831 };
832
833 let hir_id = tcx.local_def_id_to_hir_id(def_id);
836 let parent_id = tcx.hir_get_parent_item(hir_id);
837 let item = tcx.hir_node_by_def_id(parent_id.def_id);
838
839 {
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/infer/note_and_explain.rs:839",
"rustc_trait_selection::error_reporting::infer::note_and_explain",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs"),
::tracing_core::__macro_support::Option::Some(839u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer::note_and_explain"),
::tracing_core::field::FieldSet::new(&["message"],
::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(&format_args!("expected_projection parent item {0:?}",
item) as &dyn Value))])
});
} else { ; }
};debug!("expected_projection parent item {:?}", item);
840
841 let param_env = tcx.param_env(body_owner_def_id);
842
843 if let DefKind::Trait | DefKind::Impl { .. } = tcx.def_kind(parent_id) {
844 let assoc_items = tcx.associated_items(parent_id);
845 for assoc_item in assoc_items.in_definition_order() {
847 if assoc_item.is_type()
848 && let hir::Defaultness::Default { has_value: true } = assoc_item.defaultness(tcx)
851 && let assoc_ty = tcx.type_of(assoc_item.def_id).instantiate_identity()
852 && self.infcx.can_eq(param_env, assoc_ty, found)
853 {
854 let msg = match assoc_item.container {
855 ty::AssocContainer::Trait => {
856 "associated type defaults can't be assumed inside the \
857 trait defining them"
858 }
859 ty::AssocContainer::InherentImpl | ty::AssocContainer::TraitImpl(_) => {
860 "associated type is `default` and may be overridden"
861 }
862 };
863 diag.span_label(tcx.def_span(assoc_item.def_id), msg);
864 return true;
865 }
866 }
867 }
868
869 false
870 }
871
872 fn constrain_generic_bound_associated_type_structured_suggestion(
880 &self,
881 diag: &mut Diag<'_>,
882 trait_ref: ty::TraitRef<'tcx>,
883 bounds: hir::GenericBounds<'_>,
884 assoc: ty::AssocItem,
885 assoc_args: &[ty::GenericArg<'tcx>],
886 ty: Ty<'tcx>,
887 msg: impl Fn() -> String,
888 is_bound_surely_present: bool,
889 ) -> bool {
890 let trait_bounds = bounds.iter().filter_map(|bound| match bound {
893 hir::GenericBound::Trait(ptr) if ptr.modifiers == hir::TraitBoundModifiers::NONE => {
894 Some(ptr)
895 }
896 _ => None,
897 });
898
899 let matching_trait_bounds = trait_bounds
900 .clone()
901 .filter(|ptr| ptr.trait_ref.trait_def_id() == Some(trait_ref.def_id))
902 .collect::<Vec<_>>();
903
904 let span = match &matching_trait_bounds[..] {
905 &[ptr] => ptr.span,
906 &[] if is_bound_surely_present => match &trait_bounds.collect::<Vec<_>>()[..] {
907 &[ptr] => ptr.span,
908 _ => return false,
909 },
910 _ => return false,
911 };
912
913 self.constrain_associated_type_structured_suggestion(diag, span, assoc, assoc_args, ty, msg)
914 }
915
916 fn constrain_associated_type_structured_suggestion(
919 &self,
920 diag: &mut Diag<'_>,
921 span: Span,
922 assoc: ty::AssocItem,
923 assoc_args: &[ty::GenericArg<'tcx>],
924 ty: Ty<'tcx>,
925 msg: impl Fn() -> String,
926 ) -> bool {
927 let tcx = self.tcx;
928
929 if let Ok(has_params) =
930 tcx.sess.source_map().span_to_snippet(span).map(|snippet| snippet.ends_with('>'))
931 {
932 let (span, sugg) = if has_params {
933 let pos = span.hi() - BytePos(1);
934 let span = Span::new(pos, pos, span.ctxt(), span.parent());
935 (span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!(", {0} = {1}", assoc.ident(tcx),
ty))
})format!(", {} = {}", assoc.ident(tcx), ty))
936 } else {
937 let item_args = self.format_generic_args(assoc_args);
938 (span.shrink_to_hi(), ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("<{0}{1} = {2}>", assoc.ident(tcx),
item_args, ty))
})format!("<{}{} = {}>", assoc.ident(tcx), item_args, ty))
939 };
940 diag.span_suggestion_verbose(span, msg(), sugg, MaybeIncorrect);
941 return true;
942 }
943 false
944 }
945
946 pub fn format_generic_args(&self, args: &[ty::GenericArg<'tcx>]) -> String {
947 FmtPrinter::print_string(self.tcx, hir::def::Namespace::TypeNS, |p| {
948 p.print_path_with_generic_args(|_| Ok(()), args)
949 })
950 .expect("could not write to `String`.")
951 }
952}