Skip to content

Erase type params when suggesting fully qualified path #96347

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

Merged
merged 3 commits into from
Apr 30, 2022
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
41 changes: 31 additions & 10 deletions compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -734,22 +734,28 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
if !impl_candidates.is_empty() && e.span.contains(span)
&& let Some(expr) = exprs.first()
&& let ExprKind::Path(hir::QPath::Resolved(_, path)) = expr.kind
&& let [path_segment] = path.segments
&& let [_] = path.segments
{
let mut eraser = TypeParamEraser(self.tcx);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is overkill, but we could know exactly what generic types need to be erased if we returned the impl's DefId from find_similar_impl_candidates instead of just the TraitRef, then we might just be able to use subst instead of erasing all type params

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 maybe, but that'd end up being a relatively larger change, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, but it might allow us to erase fewer type variables when we're calling one generic method from another generic method with type variables.

but probably not worth the effort...

let candidate_len = impl_candidates.len();
let suggestions = impl_candidates.iter().map(|candidate| {
format!(
"{}::{}({})",
candidate, segment.ident, path_segment.ident
)
});
err.span_suggestions(
e.span,
let mut suggestions: Vec<_> = impl_candidates.iter().map(|candidate| {
let candidate = candidate.super_fold_with(&mut eraser);
vec![
(expr.span.shrink_to_lo(), format!("{}::{}(", candidate, segment.ident)),
if exprs.len() == 1 {
(expr.span.shrink_to_hi().with_hi(e.span.hi()), ")".to_string())
} else {
(expr.span.shrink_to_hi().with_hi(exprs[1].span.lo()), ", ".to_string())
},
]
}).collect();
suggestions.sort_by(|a, b| a[0].1.cmp(&b[0].1));
err.multipart_suggestions(
&format!(
"use the fully qualified path for the potential candidate{}",
pluralize!(candidate_len),
),
suggestions,
suggestions.into_iter(),
Applicability::MaybeIncorrect,
);
}
Expand Down Expand Up @@ -1037,3 +1043,18 @@ impl<'tcx> TypeFolder<'tcx> for ErrTypeParamEraser<'tcx> {
}
}
}

/// Replace type parameters with `ty::Infer(ty::Var)` to display `_`.
struct TypeParamEraser<'tcx>(TyCtxt<'tcx>);

impl<'tcx> TypeFolder<'tcx> for TypeParamEraser<'tcx> {
fn tcx<'a>(&'a self) -> TyCtxt<'tcx> {
self.0
}
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
match t.kind() {
ty::Param(_) | ty::Error(_) => self.tcx().mk_ty_var(ty::TyVid::from_u32(0)),
_ => t.super_fold_with(self),
}
}
}
5 changes: 4 additions & 1 deletion src/test/ui/error-codes/E0283.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ LL | let bar = foo_impl.into() * 1u32;
| | |
| | cannot infer type for type parameter `T` declared on the trait `Into`
| this method call resolves to `T`
| help: use the fully qualified path for the potential candidate: `<Impl as Into<u32>>::into(foo_impl)`
|
note: multiple `impl`s satisfying `Impl: Into<_>` found
--> $DIR/E0283.rs:17:1
Expand All @@ -24,6 +23,10 @@ LL | impl Into<u32> for Impl {
= note: and another `impl` found in the `core` crate:
- impl<T, U> Into<U> for T
where U: From<T>;
help: use the fully qualified path for the potential candidate
|
LL | let bar = <Impl as Into<u32>>::into(foo_impl) * 1u32;
| ++++++++++++++++++++++++++ ~

error: aborting due to 2 previous errors

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
struct Thing<X>(X);

trait Method<T> {
fn method(self, _: i32) -> T;
}

impl<X> Method<i32> for Thing<X> {
fn method(self, _: i32) -> i32 { 0 }
}

impl<X> Method<u32> for Thing<X> {
fn method(self, _: i32) -> u32 { 0 }
}

fn main() {
let thing = Thing(true);
thing.method(42);
//~^ ERROR type annotations needed
//~| ERROR type annotations needed
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
error[E0282]: type annotations needed
--> $DIR/do-not-mention-type-params-by-name-in-suggestion-issue-96292.rs:17:11
|
LL | thing.method(42);
| ------^^^^^^----
| | |
| | cannot infer type for type parameter `T` declared on the trait `Method`
| this method call resolves to `T`

error[E0283]: type annotations needed
--> $DIR/do-not-mention-type-params-by-name-in-suggestion-issue-96292.rs:17:11
|
LL | thing.method(42);
| ------^^^^^^----
| | |
| | cannot infer type for type parameter `T` declared on the trait `Method`
| this method call resolves to `T`
|
note: multiple `impl`s satisfying `Thing<bool>: Method<_>` found
--> $DIR/do-not-mention-type-params-by-name-in-suggestion-issue-96292.rs:7:1
|
LL | impl<X> Method<i32> for Thing<X> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
LL | impl<X> Method<u32> for Thing<X> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: use the fully qualified path for the potential candidates
|
LL | <Thing<_> as Method<i32>>::method(thing, 42);
| ++++++++++++++++++++++++++++++++++ ~
LL | <Thing<_> as Method<u32>>::method(thing, 42);
| ++++++++++++++++++++++++++++++++++ ~

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0282, E0283.
For more information about an error, try `rustc --explain E0282`.
8 changes: 4 additions & 4 deletions src/test/ui/traits/issue-77982.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ LL | opts.get(opt.as_ref());
help: use the fully qualified path for the potential candidates
|
LL | opts.get(<String as AsRef<OsStr>>::as_ref(opt));
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| +++++++++++++++++++++++++++++++++ ~
LL | opts.get(<String as AsRef<Path>>::as_ref(opt));
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| ++++++++++++++++++++++++++++++++ ~
LL | opts.get(<String as AsRef<[u8]>>::as_ref(opt));
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| ++++++++++++++++++++++++++++++++ ~
LL | opts.get(<String as AsRef<str>>::as_ref(opt));
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| +++++++++++++++++++++++++++++++ ~
and 4 other candidates

error[E0283]: type annotations needed
Expand Down