Skip to content

Commit b3fba5e

Browse files
committed
Remove unnecessary code and account for turbofish suggestion
Remove previously existing fallback that tried to give a good turbofish suggestion, `need_type_info` is already good enough. Special case `::<Vec<_>` suggestion for `Iterator::collect`.
1 parent 9d5e7d3 commit b3fba5e

File tree

5 files changed

+43
-87
lines changed

5 files changed

+43
-87
lines changed

compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -461,33 +461,39 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
461461
parent_name,
462462
});
463463

464-
let args = fmt_printer(self, Namespace::TypeNS)
465-
.comma_sep(generic_args.iter().copied().map(|arg| {
466-
if arg.is_suggestable(self.tcx, true) {
467-
return arg;
468-
}
464+
let args = if self.infcx.tcx.get_diagnostic_item(sym::iterator_collect_fn)
465+
== Some(generics_def_id)
466+
{
467+
"Vec<_>".to_string()
468+
} else {
469+
fmt_printer(self, Namespace::TypeNS)
470+
.comma_sep(generic_args.iter().copied().map(|arg| {
471+
if arg.is_suggestable(self.tcx, true) {
472+
return arg;
473+
}
469474

470-
match arg.unpack() {
471-
GenericArgKind::Lifetime(_) => bug!("unexpected lifetime"),
472-
GenericArgKind::Type(_) => self
473-
.next_ty_var(TypeVariableOrigin {
474-
span: rustc_span::DUMMY_SP,
475-
kind: TypeVariableOriginKind::MiscVariable,
476-
})
477-
.into(),
478-
GenericArgKind::Const(arg) => self
479-
.next_const_var(
480-
arg.ty(),
481-
ConstVariableOrigin {
475+
match arg.unpack() {
476+
GenericArgKind::Lifetime(_) => bug!("unexpected lifetime"),
477+
GenericArgKind::Type(_) => self
478+
.next_ty_var(TypeVariableOrigin {
482479
span: rustc_span::DUMMY_SP,
483-
kind: ConstVariableOriginKind::MiscVariable,
484-
},
485-
)
486-
.into(),
487-
}
488-
}))
489-
.unwrap()
490-
.into_buffer();
480+
kind: TypeVariableOriginKind::MiscVariable,
481+
})
482+
.into(),
483+
GenericArgKind::Const(arg) => self
484+
.next_const_var(
485+
arg.ty(),
486+
ConstVariableOrigin {
487+
span: rustc_span::DUMMY_SP,
488+
kind: ConstVariableOriginKind::MiscVariable,
489+
},
490+
)
491+
.into(),
492+
}
493+
}))
494+
.unwrap()
495+
.into_buffer()
496+
};
491497

492498
if !have_turbofish {
493499
infer_subdiags.push(SourceKindSubdiag::GenericSuggestion {

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

Lines changed: 4 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use rustc_middle::ty::{
4242
};
4343
use rustc_session::Limit;
4444
use rustc_span::def_id::LOCAL_CRATE;
45-
use rustc_span::symbol::{kw, sym};
45+
use rustc_span::symbol::sym;
4646
use rustc_span::{ExpnKind, Span, DUMMY_SP};
4747
use std::fmt;
4848
use std::iter;
@@ -2198,60 +2198,10 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
21982198
}
21992199
}
22002200

2201-
if let ObligationCauseCode::ItemObligation(def_id) | ObligationCauseCode::ExprItemObligation(def_id, ..) = *obligation.cause.code() {
2202-
self.suggest_fully_qualified_path(&mut err, def_id, span, trait_ref.def_id());
2203-
} else if let Ok(snippet) = &self.tcx.sess.source_map().span_to_snippet(span)
2204-
&& let ObligationCauseCode::BindingObligation(def_id, _) | ObligationCauseCode::ExprBindingObligation(def_id, ..)
2205-
= *obligation.cause.code()
2201+
if let ObligationCauseCode::ItemObligation(def_id)
2202+
| ObligationCauseCode::ExprItemObligation(def_id, ..) = *obligation.cause.code()
22062203
{
2207-
let generics = self.tcx.generics_of(def_id);
2208-
if generics.params.iter().any(|p| p.name != kw::SelfUpper)
2209-
&& !snippet.ends_with('>')
2210-
&& !generics.has_impl_trait()
2211-
&& !self.tcx.is_fn_trait(def_id)
2212-
{
2213-
// FIXME: To avoid spurious suggestions in functions where type arguments
2214-
// where already supplied, we check the snippet to make sure it doesn't
2215-
// end with a turbofish. Ideally we would have access to a `PathSegment`
2216-
// instead. Otherwise we would produce the following output:
2217-
//
2218-
// error[E0283]: type annotations needed
2219-
// --> $DIR/issue-54954.rs:3:24
2220-
// |
2221-
// LL | const ARR_LEN: usize = Tt::const_val::<[i8; 123]>();
2222-
// | ^^^^^^^^^^^^^^^^^^^^^^^^^^
2223-
// | |
2224-
// | cannot infer type
2225-
// | help: consider specifying the type argument
2226-
// | in the function call:
2227-
// | `Tt::const_val::<[i8; 123]>::<T>`
2228-
// ...
2229-
// LL | const fn const_val<T: Sized>() -> usize {
2230-
// | - required by this bound in `Tt::const_val`
2231-
// |
2232-
// = note: cannot satisfy `_: Tt`
2233-
2234-
// Clear any more general suggestions in favor of our specific one
2235-
err.clear_suggestions();
2236-
2237-
err.span_suggestion_verbose(
2238-
span.shrink_to_hi(),
2239-
&format!(
2240-
"consider specifying the type argument{} in the function call",
2241-
pluralize!(generics.params.len()),
2242-
),
2243-
format!(
2244-
"::<{}>",
2245-
generics
2246-
.params
2247-
.iter()
2248-
.map(|p| p.name.to_string())
2249-
.collect::<Vec<String>>()
2250-
.join(", ")
2251-
),
2252-
Applicability::HasPlaceholders,
2253-
);
2254-
}
2204+
self.suggest_fully_qualified_path(&mut err, def_id, span, trait_ref.def_id());
22552205
}
22562206

22572207
if let (Some(body_id), Some(ty::subst::GenericArgKind::Type(_))) =

src/test/ui/error-codes/E0401.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ note: required by a bound in `bfnr`
5959
|
6060
LL | fn bfnr<U, V: Baz<U>, W: Fn()>(y: T) {
6161
| ^^^^ required by this bound in `bfnr`
62-
help: consider specifying the type arguments in the function call
62+
help: consider specifying the generic arguments
6363
|
6464
LL | bfnr::<U, V, W>(x);
6565
| +++++++++++

src/test/ui/inference/erase-type-params-in-label.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ note: required by a bound in `foo`
1010
|
1111
LL | fn foo<T, K, W: Default, Z: Default>(t: T, k: K) -> Foo<T, K, W, Z> {
1212
| ^^^^^^^ required by this bound in `foo`
13-
help: consider specifying the type arguments in the function call
13+
help: consider giving `foo` an explicit type, where the type for type parameter `W` is specified
1414
|
15-
LL | let foo = foo::<T, K, W, Z>(1, "");
16-
| ++++++++++++++
15+
LL | let foo: Foo<i32, &str, W, Z> = foo(1, "");
16+
| ++++++++++++++++++++++
1717

1818
error[E0283]: type annotations needed for `Bar<i32, &str, Z>`
1919
--> $DIR/erase-type-params-in-label.rs:5:9
@@ -27,10 +27,10 @@ note: required by a bound in `bar`
2727
|
2828
LL | fn bar<T, K, Z: Default>(t: T, k: K) -> Bar<T, K, Z> {
2929
| ^^^^^^^ required by this bound in `bar`
30-
help: consider specifying the type arguments in the function call
30+
help: consider giving `bar` an explicit type, where the type for type parameter `Z` is specified
3131
|
32-
LL | let bar = bar::<T, K, Z>(1, "");
33-
| +++++++++++
32+
LL | let bar: Bar<i32, &str, Z> = bar(1, "");
33+
| +++++++++++++++++++
3434

3535
error: aborting due to 2 previous errors
3636

src/test/ui/type/type-annotation-needed.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ note: required by a bound in `foo`
1010
|
1111
LL | fn foo<T: Into<String>>(x: i32) {}
1212
| ^^^^^^^^^^^^ required by this bound in `foo`
13-
help: consider specifying the type argument in the function call
13+
help: consider specifying the generic argument
1414
|
1515
LL | foo::<T>(42);
1616
| +++++

0 commit comments

Comments
 (0)