Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ use rustc_middle::hir::nested_filter;
use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow, DerefAdjustKind};
use rustc_middle::ty::print::{FmtPrinter, PrettyPrinter, Print, Printer};
use rustc_middle::ty::{
self, GenericArg, GenericArgKind, GenericArgsRef, InferConst, IsSuggestable, Term, TermKind,
Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt, TypeckResults,
self, GenericArg, GenericArgKind, GenericArgsRef, GenericParamDefKind, InferConst,
IsSuggestable, Term, TermKind, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable,
TypeVisitableExt, TypeckResults,
};
use rustc_span::{BytePos, DUMMY_SP, Ident, Span, sym};
use tracing::{debug, instrument, warn};
Expand Down Expand Up @@ -592,15 +593,19 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
(true, parent.prefix.to_string(), parent.name)
});

let param = &generics.own_params[argument_index];
let param_name = param.name.to_string();

infer_subdiags.push(SourceKindSubdiag::GenericLabel {
span,
is_type,
param_name: generics.own_params[argument_index].name.to_string(),
param_name: param_name.clone(),
parent_exists,
parent_prefix,
parent_name,
});

let mut used_fallback = false;
let args = if self.tcx.get_diagnostic_item(sym::iterator_collect_fn)
== Some(generics_def_id)
{
Expand Down Expand Up @@ -634,9 +639,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
let mut p = fmt_printer(self, Namespace::TypeNS);
p.comma_sep(generic_args.iter().copied().map(|arg| {
if arg.is_suggestable(self.tcx, true) {
used_fallback = true;
return arg;
}

match arg.kind() {
GenericArgKind::Lifetime(_) => bug!("unexpected lifetime"),
GenericArgKind::Type(_) => self.next_ty_var(DUMMY_SP).into(),
Expand All @@ -648,11 +653,31 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
};

if !have_turbofish {
infer_subdiags.push(SourceKindSubdiag::GenericSuggestion {
span: insert_span,
arg_count: generic_args.len(),
args,
});
if generic_args.len() == 1 && used_fallback {
match param.kind {
GenericParamDefKind::Type { .. } => {
infer_subdiags.push(SourceKindSubdiag::GenericTypeSuggestion {
span: insert_span,
param: param_name,
});
}
GenericParamDefKind::Const { .. } => {
infer_subdiags.push(SourceKindSubdiag::ConstGenericSuggestion {
span: insert_span,
param: param_name,
});
}
GenericParamDefKind::Lifetime => {
bug!("unexpected lifetime")
}
}
} else {
infer_subdiags.push(SourceKindSubdiag::GenericSuggestion {
span: insert_span,
arg_count: generic_args.len(),
args,
});
}
}
}
InferSourceKind::FullyQualifiedMethodCall { receiver, successor, args, def_id } => {
Expand Down
22 changes: 22 additions & 0 deletions compiler/rustc_trait_selection/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,28 @@ pub(crate) enum SourceKindSubdiag<'a> {
arg_count: usize,
args: String,
},
#[suggestion(
"consider specifying a concrete type for the type parameter `{$param}`",
style = "verbose",
code = "::</* Type */>",
applicability = "has-placeholders"
)]
GenericTypeSuggestion {
#[primary_span]
span: Span,
param: String,
},
#[suggestion(
"consider specifying a const for the const parameter `{$param}`",
style = "verbose",
code = "::</* CONST */>",
applicability = "has-placeholders"
)]
ConstGenericSuggestion {
#[primary_span]
span: Span,
param: String,
},
}

#[derive(Subdiagnostic)]
Expand Down
18 changes: 9 additions & 9 deletions tests/ui/associated-type-bounds/duplicate-bound-err.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,32 @@ error[E0282]: type annotations needed
LL | iter::empty()
| ^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the function `empty`
|
help: consider specifying the generic argument
help: consider specifying a concrete type for the type parameter `T`
|
LL | iter::empty::<T>()
| +++++
LL | iter::empty::</* Type */>()
| ++++++++++++++

error[E0282]: type annotations needed
--> $DIR/duplicate-bound-err.rs:18:5
|
LL | iter::empty()
| ^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the function `empty`
|
help: consider specifying the generic argument
help: consider specifying a concrete type for the type parameter `T`
|
LL | iter::empty::<T>()
| +++++
LL | iter::empty::</* Type */>()
| ++++++++++++++

error[E0282]: type annotations needed
--> $DIR/duplicate-bound-err.rs:22:5
|
LL | iter::empty()
| ^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the function `empty`
|
help: consider specifying the generic argument
help: consider specifying a concrete type for the type parameter `T`
|
LL | iter::empty::<T>()
| +++++
LL | iter::empty::</* Type */>()
| ++++++++++++++

error: unconstrained opaque type
--> $DIR/duplicate-bound-err.rs:26:51
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/async-await/unresolved_type_param.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ error[E0282]: type annotations needed
LL | bar().await;
| ^^^ cannot infer type of the type parameter `T` declared on the function `bar`
|
help: consider specifying the generic argument
help: consider specifying a concrete type for the type parameter `T`
|
LL | bar::<T>().await;
| +++++
LL | bar::</* Type */>().await;
| ++++++++++++++

error: aborting due to 1 previous error

Expand Down
6 changes: 3 additions & 3 deletions tests/ui/const-generics/defaults/rp_impl_trait_fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ note: required by a const generic parameter in `uwu`
|
LL | fn uwu<const N: u8>() -> impl Traitor<N> {
| ^^^^^^^^^^^ required by this const generic parameter in `uwu`
help: consider specifying the generic argument
help: consider specifying a const for the const parameter `N`
|
LL | uwu::<N>();
| +++++
LL | uwu::</* CONST */>();
| +++++++++++++++

error: aborting due to 4 previous errors

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ error[E0282]: type annotations needed
LL | let _ = Checked::<generic>;
| ^^^^^^^ cannot infer type of the type parameter `T` declared on the function `generic`
|
help: consider specifying the generic argument
help: consider specifying a concrete type for the type parameter `T`
|
LL | let _ = Checked::<generic::<T>>;
| +++++
LL | let _ = Checked::<generic::</* Type */>>;
| ++++++++++++++

error: aborting due to 3 previous errors

Expand Down
6 changes: 3 additions & 3 deletions tests/ui/const-generics/fn-const-param-infer.full.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ error[E0282]: type annotations needed
LL | let _ = Checked::<generic>;
| ^^^^^^^ cannot infer type of the type parameter `T` declared on the function `generic`
|
help: consider specifying the generic argument
help: consider specifying a concrete type for the type parameter `T`
|
LL | let _ = Checked::<generic::<T>>;
| +++++
LL | let _ = Checked::<generic::</* Type */>>;
| ++++++++++++++

error: aborting due to 3 previous errors

Expand Down
6 changes: 3 additions & 3 deletions tests/ui/const-generics/fn-const-param-infer.min.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ error[E0282]: type annotations needed
LL | let _ = Checked::<generic>;
| ^^^^^^^ cannot infer type of the type parameter `T` declared on the function `generic`
|
help: consider specifying the generic argument
help: consider specifying a concrete type for the type parameter `T`
|
LL | let _ = Checked::<generic::<T>>;
| +++++
LL | let _ = Checked::<generic::</* Type */>>;
| ++++++++++++++

error: aborting due to 3 previous errors

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ note: required by a const generic parameter in `use_dyn`
|
LL | fn use_dyn<const N: usize>(v: &dyn Foo<N>) where [u8; N + 1]: Sized {
| ^^^^^^^^^^^^^^ required by this const generic parameter in `use_dyn`
help: consider specifying the generic argument
help: consider specifying a const for the const parameter `N`
|
LL | use_dyn::<N>(&());
| +++++
LL | use_dyn::</* CONST */>(&());
| +++++++++++++++

error[E0284]: type annotations needed
--> $DIR/dyn-compatibility-ok-infer-err.rs:19:5
Expand All @@ -30,10 +30,10 @@ LL | impl<const N: usize> Foo<N> for () {
| |
| unsatisfied trait bound introduced here
= note: required for the cast from `&()` to `&dyn Foo<_>`
help: consider specifying the generic argument
help: consider specifying a const for the const parameter `N`
|
LL | use_dyn::<N>(&());
| +++++
LL | use_dyn::</* CONST */>(&());
| +++++++++++++++

error: aborting due to 2 previous errors

Expand Down
6 changes: 3 additions & 3 deletions tests/ui/const-generics/infer/cannot-infer-const-args.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ note: required by a const generic parameter in `foo`
|
LL | fn foo<const X: usize>() -> usize {
| ^^^^^^^^^^^^^^ required by this const generic parameter in `foo`
help: consider specifying the generic argument
help: consider specifying a const for the const parameter `X`
|
LL | foo::<X>();
| +++++
LL | foo::</* CONST */>();
| +++++++++++++++

error: aborting due to 1 previous error

Expand Down
6 changes: 3 additions & 3 deletions tests/ui/const-generics/infer/method-chain.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ note: required by a const generic parameter in `Foo::baz`
|
LL | fn baz<const N: usize>(self) -> Foo {
| ^^^^^^^^^^^^^^ required by this const generic parameter in `Foo::baz`
help: consider specifying the generic argument
help: consider specifying a const for the const parameter `N`
|
LL | Foo.bar().bar().bar().bar().baz::<N>();
| +++++
LL | Foo.bar().bar().bar().bar().baz::</* CONST */>();
| +++++++++++++++

error: aborting due to 1 previous error

Expand Down
6 changes: 3 additions & 3 deletions tests/ui/const-generics/unify_with_nested_expr.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ note: required by a const generic parameter in `bar`
|
LL | fn bar<const N: usize>()
| ^^^^^^^^^^^^^^ required by this const generic parameter in `bar`
help: consider specifying the generic argument
help: consider specifying a const for the const parameter `N`
|
LL | bar::<N>();
| +++++
LL | bar::</* CONST */>();
| +++++++++++++++

error: aborting due to 1 previous error

Expand Down
12 changes: 6 additions & 6 deletions tests/ui/consts/issue-64662.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ error[E0282]: type annotations needed
LL | A = foo(),
| ^^^ cannot infer type of the type parameter `T` declared on the function `foo`
|
help: consider specifying the generic argument
help: consider specifying a concrete type for the type parameter `T`
|
LL | A = foo::<T>(),
| +++++
LL | A = foo::</* Type */>(),
| ++++++++++++++

error[E0282]: type annotations needed
--> $DIR/issue-64662.rs:3:9
|
LL | B = foo(),
| ^^^ cannot infer type of the type parameter `T` declared on the function `foo`
|
help: consider specifying the generic argument
help: consider specifying a concrete type for the type parameter `T`
|
LL | B = foo::<T>(),
| +++++
LL | B = foo::</* Type */>(),
| ++++++++++++++

error: aborting due to 2 previous errors

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ error[E0282]: type annotations needed
LL | for i in Vec::new() {}
| ^^^^^^^^ cannot infer type of the type parameter `T` declared on the struct `Vec`
|
help: consider specifying the generic argument
help: consider specifying a concrete type for the type parameter `T`
|
LL | for i in Vec::<T>::new() {}
| +++++
LL | for i in Vec::</* Type */>::new() {}
| ++++++++++++++

error: aborting due to 1 previous error

Expand Down
6 changes: 3 additions & 3 deletions tests/ui/generic-associated-types/bugs/issue-88382.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ note: required by a bound in `test`
|
LL | fn test<'a, I: Iterable>(_: &mut I::Iterator<'a>) {}
| ^^^^^^^^ required by this bound in `test`
help: consider specifying the generic argument
help: consider specifying a concrete type for the type parameter `I`
|
LL | do_something(SomeImplementation(), test::<I>);
| +++++
LL | do_something(SomeImplementation(), test::</* Type */>);
| ++++++++++++++

error: aborting due to 1 previous error

Expand Down
6 changes: 3 additions & 3 deletions tests/ui/impl-trait/fallback_inference.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ error[E0282]: type annotations needed
LL | PhantomData
| ^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the struct `PhantomData`
|
help: consider specifying the generic argument
help: consider specifying a concrete type for the type parameter `T`
|
LL | PhantomData::<T>
| +++++
LL | PhantomData::</* Type */>
| ++++++++++++++

error: aborting due to 1 previous error

Expand Down
6 changes: 3 additions & 3 deletions tests/ui/impl-trait/in-trait/not-inferred-generic.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ note: required by a bound in `TypedClient::publish_typed::{anon_assoc#0}`
|
LL | F: Clone;
| ^^^^^ required by this bound in `TypedClient::publish_typed::{anon_assoc#0}`
help: consider specifying the generic argument
help: consider specifying a concrete type for the type parameter `F`
|
LL | ().publish_typed::<F>();
| +++++
LL | ().publish_typed::</* Type */>();
| ++++++++++++++

error: aborting due to 1 previous error

Expand Down
6 changes: 3 additions & 3 deletions tests/ui/inference/dont-collect-stmts-from-parent-body.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ error[E0282]: type annotations needed
LL | Type
| ^^^^ cannot infer type of the type parameter `T` declared on the struct `Type`
|
help: consider specifying the generic argument
help: consider specifying a concrete type for the type parameter `T`
|
LL | Type::<T>
| +++++
LL | Type::</* Type */>
| ++++++++++++++

error: aborting due to 2 previous errors

Expand Down
Loading
Loading