Skip to content
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

Add more information to error message for unconstrained_parameter E0207 #115245

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 89 additions & 3 deletions compiler/rustc_hir_analysis/src/impl_wf_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use rustc_errors::struct_span_err;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::{LocalDefId, LocalModDefId};
use rustc_middle::query::Providers;
use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt};
use rustc_middle::ty::{self, ImplSubject, Ty, TyCtxt, TypeVisitableExt};
use rustc_span::{Span, Symbol};

mod min_specialization;
Expand Down Expand Up @@ -82,6 +82,14 @@ fn enforce_impl_params_are_constrained(tcx: TyCtxt<'_>, impl_def_id: LocalDefId)
);
return;
}

let other_span = tcx.source_span(impl_def_id);
let other_span = tcx.sess.source_map().span_until_char(other_span, '{');
let prev_span = tcx.sess.source_map().span_through_char(other_span, '>');
let other_span = prev_span.between(other_span.shrink_to_hi());

let impl_subject = tcx.impl_subject(impl_def_id.into()).instantiate_identity();

let impl_generics = tcx.generics_of(impl_def_id);
let impl_predicates = tcx.predicates_of(impl_def_id);
let impl_trait_ref = tcx.impl_trait_ref(impl_def_id).map(ty::EarlyBinder::instantiate_identity);
Expand Down Expand Up @@ -119,7 +127,31 @@ fn enforce_impl_params_are_constrained(tcx: TyCtxt<'_>, impl_def_id: LocalDefId)
ty::GenericParamDefKind::Type { .. } => {
let param_ty = ty::ParamTy::for_def(param);
if !input_parameters.contains(&cgp::Parameter::from(param_ty)) {
report_unused_parameter(tcx, tcx.def_span(param.def_id), "type", param_ty.name);
let relevant_spans: Vec<_> = impl_predicates
.predicates
.iter()
.filter_map(|(clause, span)| match clause.kind().skip_binder() {
ty::ClauseKind::Trait(t) => {
if param_ty.to_ty(tcx) == t.self_ty() {
Some(Span::clone(span))
} else {
None
}
}
_ => None,
})
.collect();

report_unused_parameter(
tcx,
tcx.def_span(param.def_id),
"type",
param_ty.name,
impl_self_ty,
impl_subject,
other_span,
relevant_spans,
);
}
}
ty::GenericParamDefKind::Lifetime => {
Expand All @@ -132,6 +164,10 @@ fn enforce_impl_params_are_constrained(tcx: TyCtxt<'_>, impl_def_id: LocalDefId)
tcx.def_span(param.def_id),
"lifetime",
param.name,
impl_self_ty,
impl_subject,
other_span,
Vec::new(),
);
}
}
Expand All @@ -143,6 +179,10 @@ fn enforce_impl_params_are_constrained(tcx: TyCtxt<'_>, impl_def_id: LocalDefId)
tcx.def_span(param.def_id),
"const",
param_ct.name,
impl_self_ty,
impl_subject,
other_span,
Vec::new(),
);
}
}
Expand All @@ -169,7 +209,26 @@ fn enforce_impl_params_are_constrained(tcx: TyCtxt<'_>, impl_def_id: LocalDefId)
// used elsewhere are not projected back out.
}

fn report_unused_parameter(tcx: TyCtxt<'_>, span: Span, kind: &str, name: Symbol) {
fn report_unused_parameter(
tcx: TyCtxt<'_>,
span: Span,
kind: &str,
name: Symbol,
ty: Ty<'_>,
impl_subject: ImplSubject<'_>,
impl_subject_span: Span,
_extra_spans: Vec<Span>,
) {
let raw_ty = format!("{ty}");
let ty_using_param = if raw_ty.contains("<") {
let mut tmp = raw_ty.clone();
let end_idx = raw_ty.rfind(">").unwrap();
tmp.insert_str(end_idx, &format!(", {name}"));
tmp
} else {
format!("{raw_ty}<{name}>")
};

let mut err = struct_span_err!(
tcx.sess,
span,
Expand All @@ -180,6 +239,33 @@ fn report_unused_parameter(tcx: TyCtxt<'_>, span: Span, kind: &str, name: Symbol
name
);
err.span_label(span, format!("unconstrained {kind} parameter"));

let param_span =
tcx.sess.source_map().span_extend_while(span, |tmp| tmp != '>' && tmp != ',').unwrap();

err.multipart_suggestion(
format!("consider using the Parameter like {ty_using_param}"),
core::iter::once((impl_subject_span, ty_using_param)).collect(),
rustc_lint_defs::Applicability::Unspecified,
);

match impl_subject {
ImplSubject::Trait(_) => {
// err.note(format!("Use the {kind} paramter `{name}` on an associated type"));
}
ImplSubject::Inherent(_) => {
err.multipart_suggestion(
format!(
"consider moving the {kind} paramter `{name}` to one of the inner functions"
),
core::iter::once((param_span, String::new()))
// .chain(extra_spans.iter().map(|span| (span.to_owned(), String::new())))
.collect(),
rustc_lint_defs::Applicability::Unspecified,
);
}
};

if kind == "const" {
err.note(
"expressions using a const parameter must map each value to a distinct output value",
Expand Down
1 change: 1 addition & 0 deletions tests/ui/async-await/issues/issue-78654.full.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ error[E0207]: the const parameter `H` is not constrained by the impl trait, self
LL | impl<const H: feature> Foo {
| ^^^^^^^^^^^^^^^^ unconstrained const parameter
|
= note: consider using const parameter `H` in the impl on `Foo` or otherwise constrain `H`
= note: expressions using a const parameter must map each value to a distinct output value
= note: proving the result of expressions other than the parameter are unique is not supported

Expand Down
1 change: 1 addition & 0 deletions tests/ui/async-await/issues/issue-78654.min.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ error[E0207]: the const parameter `H` is not constrained by the impl trait, self
LL | impl<const H: feature> Foo {
| ^^^^^^^^^^^^^^^^ unconstrained const parameter
|
= note: consider using const parameter `H` in the impl on `Foo` or otherwise constrain `H`
= note: expressions using a const parameter must map each value to a distinct output value
= note: proving the result of expressions other than the parameter are unique is not supported

Expand Down
2 changes: 2 additions & 0 deletions tests/ui/const-generics/issues/issue-68366.full.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ error[E0207]: the const parameter `N` is not constrained by the impl trait, self
LL | impl <const N: usize> Collatz<{Some(N)}> {}
| ^^^^^^^^^^^^^^ unconstrained const parameter
|
= note: consider using const parameter `N` in the impl on `Collatz<{Some(N)}>` or otherwise constrain `N`
= note: expressions using a const parameter must map each value to a distinct output value
= note: proving the result of expressions other than the parameter are unique is not supported

Expand All @@ -13,6 +14,7 @@ error[E0207]: the const parameter `N` is not constrained by the impl trait, self
LL | impl<const N: usize> Foo {}
| ^^^^^^^^^^^^^^ unconstrained const parameter
|
= note: consider using const parameter `N` in the impl on `Foo` or otherwise constrain `N`
= note: expressions using a const parameter must map each value to a distinct output value
= note: proving the result of expressions other than the parameter are unique is not supported

Expand Down
2 changes: 2 additions & 0 deletions tests/ui/const-generics/issues/issue-68366.min.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ error[E0207]: the const parameter `N` is not constrained by the impl trait, self
LL | impl <const N: usize> Collatz<{Some(N)}> {}
| ^^^^^^^^^^^^^^ unconstrained const parameter
|
= note: consider using const parameter `N` in the impl on `Collatz<{Some(N)}>` or otherwise constrain `N`
= note: expressions using a const parameter must map each value to a distinct output value
= note: proving the result of expressions other than the parameter are unique is not supported

Expand All @@ -22,6 +23,7 @@ error[E0207]: the const parameter `N` is not constrained by the impl trait, self
LL | impl<const N: usize> Foo {}
| ^^^^^^^^^^^^^^ unconstrained const parameter
|
= note: consider using const parameter `N` in the impl on `Foo` or otherwise constrain `N`
= note: expressions using a const parameter must map each value to a distinct output value
= note: proving the result of expressions other than the parameter are unique is not supported

Expand Down
1 change: 1 addition & 0 deletions tests/ui/consts/rustc-impl-const-stability.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ error[E0207]: the const parameter `host` is not constrained by the impl trait, s
LL | impl const Default for Data {
| ^^^^^ unconstrained const parameter
|
= note: consider using const parameter `host` in the impl on `Data` or otherwise constrain `host`
= note: expressions using a const parameter must map each value to a distinct output value
= note: proving the result of expressions other than the parameter are unique is not supported

Expand Down
2 changes: 2 additions & 0 deletions tests/ui/duplicate/duplicate-type-parameter.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ error[E0207]: the type parameter `T` is not constrained by the impl trait, self
|
LL | impl<T,T> Qux<T,T> for Option<T> {}
| ^ unconstrained type parameter
|
= note: consider using type parameter `T` in the impl on `Option<T>` or otherwise constrain `T`

error: aborting due to 8 previous errors

Expand Down
2 changes: 2 additions & 0 deletions tests/ui/error-codes/E0207.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ error[E0207]: the type parameter `T` is not constrained by the impl trait, self
|
LL | impl<T: Default> Foo {
| ^ unconstrained type parameter
|
= note: consider using type parameter `T` in the impl on `Foo` or otherwise constrain `T`

error: aborting due to previous error

Expand Down
2 changes: 2 additions & 0 deletions tests/ui/generic-associated-types/bugs/issue-87735.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ error[E0207]: the type parameter `U` is not constrained by the impl trait, self
|
LL | impl<'b, T, U> AsRef2 for Foo<T>
| ^ unconstrained type parameter
|
= note: consider using type parameter `U` in the impl on `Foo<T>` or otherwise constrain `U`

error: aborting due to previous error

Expand Down
2 changes: 2 additions & 0 deletions tests/ui/generic-associated-types/bugs/issue-88526.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ error[E0207]: the type parameter `I` is not constrained by the impl trait, self
|
LL | impl<'q, Q, I, F> A for TestB<Q, F>
| ^ unconstrained type parameter
|
= note: consider using type parameter `I` in the impl on `TestB<Q, F>` or otherwise constrain `I`

error: aborting due to previous error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ error[E0207]: the type parameter `T1` is not constrained by the impl trait, self
|
LL | impl <T, T1> Foo for T {
| ^^ unconstrained type parameter
|
= note: consider using type parameter `T1` in the impl on `T` or otherwise constrain `T1`

error: aborting due to 3 previous errors

Expand Down
2 changes: 2 additions & 0 deletions tests/ui/impl-trait/in-trait/unconstrained-lt.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait,
|
LL | impl<'a, T> Foo for T {
| ^^ unconstrained lifetime parameter
|
= note: consider using lifetime parameter `'a` in the impl on `T` or otherwise constrain `'a`

error: aborting due to previous error

Expand Down
2 changes: 2 additions & 0 deletions tests/ui/impl-trait/issues/issue-87340.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ error[E0207]: the type parameter `T` is not constrained by the impl trait, self
|
LL | impl<T> X for () {
| ^ unconstrained type parameter
|
= note: consider using type parameter `T` in the impl on `()` or otherwise constrain `T`

error: aborting due to previous error

Expand Down
2 changes: 2 additions & 0 deletions tests/ui/impl-unused-rps-in-assoc-type.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait,
|
LL | impl<'a> Fun for Holder {
| ^^ unconstrained lifetime parameter
|
= note: consider using lifetime parameter `'a` in the impl on `Holder` or otherwise constrain `'a`

error: aborting due to previous error

Expand Down
4 changes: 4 additions & 0 deletions tests/ui/impl-unused-tps-inherent.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ error[E0207]: the type parameter `T` is not constrained by the impl trait, self
|
LL | impl<T> MyType {
| ^ unconstrained type parameter
|
= note: consider using type parameter `T` in the impl on `MyType` or otherwise constrain `T`

error[E0207]: the type parameter `U` is not constrained by the impl trait, self type, or predicates
--> $DIR/impl-unused-tps-inherent.rs:17:8
|
LL | impl<T,U> MyType1<T> {
| ^ unconstrained type parameter
|
= note: consider using type parameter `U` in the impl on `MyType1<T>` or otherwise constrain `U`

error: aborting due to 2 previous errors

Expand Down
10 changes: 10 additions & 0 deletions tests/ui/impl-unused-tps.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,40 @@ error[E0207]: the type parameter `U` is not constrained by the impl trait, self
|
LL | impl<T,U> Foo<T> for [isize;1] {
| ^ unconstrained type parameter
|
= note: consider using type parameter `U` in the impl on `[isize; 1]` or otherwise constrain `U`

error[E0207]: the type parameter `U` is not constrained by the impl trait, self type, or predicates
--> $DIR/impl-unused-tps.rs:30:8
|
LL | impl<T,U> Bar for T {
| ^ unconstrained type parameter
|
= note: consider using type parameter `U` in the impl on `T` or otherwise constrain `U`

error[E0207]: the type parameter `U` is not constrained by the impl trait, self type, or predicates
--> $DIR/impl-unused-tps.rs:38:8
|
LL | impl<T,U> Bar for T
| ^ unconstrained type parameter
|
= note: consider using type parameter `U` in the impl on `T` or otherwise constrain `U`

error[E0207]: the type parameter `U` is not constrained by the impl trait, self type, or predicates
--> $DIR/impl-unused-tps.rs:46:8
|
LL | impl<T,U,V> Foo<T> for T
| ^ unconstrained type parameter
|
= note: consider using type parameter `U` in the impl on `T` or otherwise constrain `U`

error[E0207]: the type parameter `V` is not constrained by the impl trait, self type, or predicates
--> $DIR/impl-unused-tps.rs:46:10
|
LL | impl<T,U,V> Foo<T> for T
| ^ unconstrained type parameter
|
= note: consider using type parameter `V` in the impl on `T` or otherwise constrain `V`

error: aborting due to 5 previous errors

Expand Down
2 changes: 2 additions & 0 deletions tests/ui/issues/issue-16562.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ error[E0207]: the type parameter `T` is not constrained by the impl trait, self
|
LL | impl<T, M: MatrixShape> Collection for Col<M, usize> {
| ^ unconstrained type parameter
|
= note: consider using type parameter `T` in the impl on `Col<M, usize>` or otherwise constrain `T`

error: aborting due to previous error

Expand Down
2 changes: 2 additions & 0 deletions tests/ui/issues/issue-22886.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait,
|
LL | impl<'a> Iterator for Newtype {
| ^^ unconstrained lifetime parameter
|
= note: consider using lifetime parameter `'a` in the impl on `Newtype` or otherwise constrain `'a`

error: aborting due to previous error

Expand Down
4 changes: 4 additions & 0 deletions tests/ui/issues/issue-26262.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ error[E0207]: the type parameter `T` is not constrained by the impl trait, self
|
LL | impl<T: Tr> S<T::Assoc> {
| ^ unconstrained type parameter
|
= note: consider using type parameter `T` in the impl on `S<<T as Tr>::Assoc>` or otherwise constrain `T`

error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates
--> $DIR/issue-26262.rs:17:6
|
LL | impl<'a,T: Trait2<'a>> Trait1<<T as Trait2<'a>>::Foo> for T {
| ^^ unconstrained lifetime parameter
|
= note: consider using lifetime parameter `'a` in the impl on `T` or otherwise constrain `'a`

error: aborting due to 2 previous errors

Expand Down
2 changes: 2 additions & 0 deletions tests/ui/issues/issue-29861.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait,
|
LL | impl<'a, T: 'a> MakeRef2 for T {
| ^^ unconstrained lifetime parameter
|
= note: consider using lifetime parameter `'a` in the impl on `T` or otherwise constrain `'a`

error: aborting due to previous error

Expand Down
2 changes: 2 additions & 0 deletions tests/ui/issues/issue-35139.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait,
|
LL | impl<'a> MethodType for MTFn {
| ^^ unconstrained lifetime parameter
|
= note: consider using lifetime parameter `'a` in the impl on `MTFn` or otherwise constrain `'a`

error: aborting due to previous error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait,
|
LL | impl<'a, I> UnwrapItemsExt for I {
| ^^ unconstrained lifetime parameter
|
= note: consider using lifetime parameter `'a` in the impl on `I` or otherwise constrain `'a`

error: aborting due to previous error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ error[E0207]: the type parameter `T` is not constrained by the impl trait, self
|
LL | impl<T> X for () {
| ^ unconstrained type parameter
|
= note: consider using type parameter `T` in the impl on `()` or otherwise constrain `T`

error: aborting due to previous error

Expand Down
Loading