Repository navigation
Provide more context when an expected type was a type parameter #160180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
e4a18a6
b1476a0
b1cf8bd
bd34b4d
7518566
b6b1d7a
b9b3dba
2885a32
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,7 +18,7 @@ use rustc_middle::ty::{ | |
| TypeFolder, TypeSuperFoldable, TypeVisitable, TypeVisitableExt, TypeVisitor, TypingMode, | ||
| Unnormalized, Upcast, | ||
| }; | ||
| use rustc_span::{BytePos, DUMMY_SP, Span, bug, span_bug}; | ||
| use rustc_span::{BytePos, DUMMY_SP, Span, bug, kw, span_bug}; | ||
| use rustc_trait_selection::error_reporting::InferCtxtErrorExt; | ||
| use rustc_trait_selection::infer::InferCtxtExt; | ||
| use rustc_trait_selection::regions::InferCtxtRegionExt; | ||
|
|
@@ -625,12 +625,16 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>( | |
| "method `{}` has an incompatible return type for trait", | ||
| trait_m.name() | ||
| ); | ||
| diag.span_context(tcx.def_span(impl_m.container_id(tcx)).shrink_to_lo()); | ||
| infcx.err_ctxt().note_type_err( | ||
| &mut diag, | ||
| &cause, | ||
| tcx.hir_get_if_local(impl_m.def_id) | ||
| .and_then(|node| node.fn_decl()) | ||
| .map(|decl| (decl.output.span(), Cow::from("return type in trait"), false)), | ||
| tcx.hir_get_if_local(trait_m.def_id).and_then(|node| node.fn_decl()).map(|decl| { | ||
| let mut span: MultiSpan = decl.output.span().into(); | ||
| span.push_span_context(tcx.def_span(trait_m.container_id(tcx)).shrink_to_lo()); | ||
| span.push_span_context(tcx.def_span(trait_m.def_id).shrink_to_lo()); | ||
| (span, Cow::from("return type in trait"), false) | ||
| }), | ||
| Some(param_env.and(infer::ValuePairs::Terms(ExpectedFound { | ||
| expected: trait_return_ty.into(), | ||
| found: impl_return_ty.into(), | ||
|
|
@@ -1014,6 +1018,7 @@ fn report_trait_method_mismatch<'tcx>( | |
| "method `{}` has an incompatible type for trait", | ||
| trait_m.name() | ||
| ); | ||
| diag.span_context(tcx.def_span(impl_m.container_id(tcx)).shrink_to_lo()); | ||
| match &terr { | ||
| TypeError::ArgumentMutability(0) | TypeError::ArgumentSorts(_, 0) | ||
| if trait_m.is_method() => | ||
|
|
@@ -1079,10 +1084,53 @@ fn report_trait_method_mismatch<'tcx>( | |
| } | ||
|
|
||
| cause.span = impl_err_span; | ||
|
|
||
| let mut labels = vec![]; | ||
| let mut context = vec![]; | ||
| if let TypeError::ArgumentSorts(_, i) = &terr | ||
| && let Some(ty) = | ||
| tcx.fn_sig(trait_m.def_id).skip_binder().inputs_and_output().skip_binder().get(*i) | ||
| && let ty::Param(param) = ty.kind() | ||
| // The expected type corresponds to a type parameter, which migh thave a default type or | ||
| // have been explicitly set in the `impl` with a different type. | ||
| && let generics = tcx.generics_of(trait_m.def_id) | ||
| && let param = generics.type_param(*param, tcx) | ||
| && let GenericParamDefKind::Type { has_default: true, .. } = param.kind | ||
| { | ||
| context.push(tcx.def_span(trait_m.container_id(tcx)).shrink_to_lo()); | ||
|
|
||
| let param_name = param.name; | ||
| let msg = if let Some(ty) = trait_sig.inputs_and_output.get(*i) | ||
| && infcx.can_eq(param_env, *ty, tcx.type_of(param.def_id).skip_binder()) | ||
| { | ||
| format!("expected to match the default of type parameter `{param_name}`") | ||
| } else if let Some(ty) = trait_sig.inputs_and_output.get(*i) | ||
| && let ty::Param(p) = tcx.type_of(param.def_id).skip_binder().kind() | ||
| && p.name == kw::SelfUpper | ||
| && infcx.can_eq(param_env, *ty, impl_trait_ref.self_ty()) | ||
| { | ||
| format!("expected to match the `Self` default of type parameter `{param_name}`") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We seem to not have test for this, could you help to add one? |
||
| } else { | ||
| format!("expected to match type parameter `{param_name}`") | ||
| }; | ||
| labels.push((tcx.def_span(param.def_id), msg)); | ||
| } | ||
|
|
||
| infcx.err_ctxt().note_type_err( | ||
| &mut diag, | ||
| &cause, | ||
| trait_err_span.map(|sp| (sp, Cow::from("type in trait"), false)), | ||
| trait_err_span.map(|sp| { | ||
| let mut span: MultiSpan = sp.into(); | ||
| span.push_span_context(tcx.def_span(trait_m.container_id(tcx)).shrink_to_lo()); | ||
| span.push_span_context(tcx.def_span(trait_m.def_id).shrink_to_lo()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This also doesn't seem to change anything when removed. What is the intention of adding this line? |
||
| for (sp, label) in labels { | ||
| span.push_span_label(sp, label); | ||
| } | ||
| for sp in context { | ||
| span.push_span_context(sp); | ||
| } | ||
| (span, Cow::from("type in trait"), false) | ||
| }), | ||
| Some(param_env.and(infer::ValuePairs::PolySigs(ExpectedFound { | ||
| expected: ty::Binder::dummy(trait_sig), | ||
| found: ty::Binder::dummy(impl_sig), | ||
|
|
@@ -2190,7 +2238,8 @@ fn compare_const_generic_param_types<'tcx>( | |
| infcx.err_ctxt().note_type_err( | ||
| &mut diag, | ||
| &cause, | ||
| param_trait_ty_span.map(|span| (span, Cow::from("type in trait"), false)), | ||
| param_trait_ty_span | ||
| .map(|span| (span.into(), Cow::from("type in trait"), false)), | ||
| Some(param_env.and(infer::ValuePairs::Terms(ExpectedFound { | ||
| expected: trait_ty.into(), | ||
| found: impl_ty.into(), | ||
|
|
@@ -2365,6 +2414,8 @@ fn compare_const_clause_entailment<'tcx>( | |
| "implemented const `{}` has an incompatible type for trait", | ||
| trait_ct.name() | ||
| ); | ||
| diag.span_context(tcx.def_span(impl_ct.container_id(tcx)).shrink_to_lo()); | ||
| diag.span_context(tcx.def_span(impl_ct.def_id).shrink_to_lo()); | ||
|
|
||
| let trait_c_span = trait_ct.def_id.as_local().map(|trait_ct_def_id| { | ||
| // Add a label to the Span containing just the type of the const | ||
|
|
@@ -2375,7 +2426,12 @@ fn compare_const_clause_entailment<'tcx>( | |
| infcx.err_ctxt().note_type_err( | ||
| &mut diag, | ||
| &cause, | ||
| trait_c_span.map(|span| (span, Cow::from("type in trait"), false)), | ||
| trait_c_span.map(|span| { | ||
| let mut span: MultiSpan = span.into(); | ||
| span.push_span_context(tcx.def_span(trait_ct.container_id(tcx)).shrink_to_lo()); | ||
| span.push_span_context(tcx.def_span(trait_ct.def_id).shrink_to_lo()); | ||
| (span, Cow::from("type in trait"), false) | ||
| }), | ||
| Some(param_env.and(infer::ValuePairs::Terms(ExpectedFound { | ||
| expected: trait_ty.into(), | ||
| found: impl_ty.into(), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,15 @@ | ||
| error[E0053]: method `make` has an incompatible type for trait | ||
| --> $DIR/defaults-specialization.rs:18:18 | ||
| | | ||
| LL | default impl<T> Tr for A<T> { | ||
| LL | fn make() -> u8 { 0 } | ||
| | ^^ expected associated type, found `u8` | ||
| | | ||
| note: type in trait | ||
| --> $DIR/defaults-specialization.rs:8:18 | ||
| | | ||
| LL | trait Tr { | ||
| ... | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. non-blocking style nit: will it be possible to have...?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That change is something to bring up to rust-lang/annotate-snippets-rs. I don't think that rendering is necessarily ideal, because it would be hard to differentiate between the actual code being trait Tr {
...and By having it on the other side of the gutter, it makes that a bit clearer.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, makes sense. |
||
| LL | fn make() -> Self::Ty { | ||
| | ^^^^^^^^ | ||
| = note: expected signature `fn() -> <A<T> as Tr>::Ty` | ||
|
|
@@ -30,6 +33,8 @@ LL | fn make() -> bool { true } | |
| note: type in trait | ||
| --> $DIR/defaults-specialization.rs:8:18 | ||
| | | ||
| LL | trait Tr { | ||
| ... | ||
| LL | fn make() -> Self::Ty { | ||
| | ^^^^^^^^ | ||
| = note: expected signature `fn() -> <B<T> as Tr>::Ty` | ||
|
|
||
|
estebank marked this conversation as resolved.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // Regression test for https://github.com/rust-lang/rust/issues/67991 | ||
| // The weird indentation is to excercise the span context that shows the enclosing items in the | ||
| // diagnostics being emitted. | ||
| trait Trait< | ||
| T = (), | ||
| K = Self, | ||
| > { | ||
| fn foo() | ||
| -> T; | ||
| fn bar() | ||
| -> K; | ||
| } | ||
|
|
||
| struct S; | ||
|
|
||
| impl Trait for S { | ||
| fn foo() -> u32 { 0 } //~ ERROR | ||
| fn bar() -> u32 { 0 } //~ ERROR | ||
| } | ||
| impl Trait<i32> for () { | ||
| fn foo() -> u32 { 0 } //~ ERROR | ||
| fn bar() -> u32 { 0 } //~ ERROR | ||
| } | ||
|
|
||
| fn main() {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does this context do? Removing this line doesn't seem to change any test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will ensure that line that starts the containing trait will be displayed. It would only happen with weirdly formatted code or really long signatures.