-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
E0599 suggestions and elision of generic argument if no canditate is found #84221
Changes from 4 commits
c64a2ed
6efa14b
5b802ed
120691c
5d8e6ea
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 | ||||
---|---|---|---|---|---|---|
|
@@ -383,6 +383,42 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |||||
return None; | ||||||
} else { | ||||||
span = item_name.span; | ||||||
|
||||||
// Don't show generic arguments when the method can't be found in any implementation (#81576). | ||||||
let mut ty_str_reported = ty_str.clone(); | ||||||
if let ty::Adt(_, ref generics) = actual.kind() { | ||||||
if generics.len() > 0 { | ||||||
let mut autoderef = self.autoderef(span, actual); | ||||||
let candidate_found = autoderef.any(|(ty, _)| { | ||||||
if let ty::Adt(ref adt_deref, _) = ty.kind() { | ||||||
self.tcx | ||||||
.inherent_impls(adt_deref.did) | ||||||
.iter() | ||||||
.filter_map(|def_id| { | ||||||
self.associated_item( | ||||||
*def_id, | ||||||
item_name, | ||||||
Namespace::ValueNS, | ||||||
) | ||||||
}) | ||||||
.count() | ||||||
>= 1 | ||||||
} else { | ||||||
false | ||||||
} | ||||||
}); | ||||||
let has_deref = autoderef.step_count() > 0; | ||||||
if !candidate_found | ||||||
&& !has_deref | ||||||
&& unsatisfied_predicates.is_empty() | ||||||
{ | ||||||
if let Some((path_string, _)) = ty_str.split_once('<') { | ||||||
ty_str_reported = path_string.to_string(); | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
let mut err = struct_span_err!( | ||||||
tcx.sess, | ||||||
span, | ||||||
|
@@ -391,7 +427,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |||||
item_kind, | ||||||
item_name, | ||||||
actual.prefix_string(self.tcx), | ||||||
ty_str, | ||||||
ty_str_reported, | ||||||
); | ||||||
if let Mode::MethodCall = mode { | ||||||
if let SelfSource::MethodCall(call) = source { | ||||||
|
@@ -449,6 +485,55 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |||||
let mut label_span_not_found = || { | ||||||
if unsatisfied_predicates.is_empty() { | ||||||
err.span_label(span, format!("{item_kind} not found in `{ty_str}`")); | ||||||
if let ty::Adt(ref adt, _) = rcvr_ty.kind() { | ||||||
let mut inherent_impls_candidate = self | ||||||
.tcx | ||||||
.inherent_impls(adt.did) | ||||||
.iter() | ||||||
.copied() | ||||||
.filter(|def_id| { | ||||||
if let Some(assoc) = | ||||||
self.associated_item(*def_id, item_name, Namespace::ValueNS) | ||||||
{ | ||||||
// Check for both mode is the same so we avoid suggesting | ||||||
// incorrect associated item. | ||||||
match (mode, assoc.fn_has_self_parameter, source) { | ||||||
(Mode::MethodCall, true, SelfSource::MethodCall(_)) => { | ||||||
// We check that the suggest type is actually | ||||||
// different from the received one | ||||||
// So we avoid suggestion method with Box<Self> | ||||||
// for instance | ||||||
self.tcx.at(span).type_of(*def_id) != actual | ||||||
&& self.tcx.at(span).type_of(*def_id) != rcvr_ty | ||||||
} | ||||||
(Mode::Path, false, _) => true, | ||||||
_ => false, | ||||||
} | ||||||
} else { | ||||||
false | ||||||
} | ||||||
}) | ||||||
.collect::<Vec<_>>(); | ||||||
if inherent_impls_candidate.len() > 0 { | ||||||
inherent_impls_candidate.sort(); | ||||||
inherent_impls_candidate.dedup(); | ||||||
let type_candidates = inherent_impls_candidate | ||||||
.iter() | ||||||
.map(|impl_item| self.tcx.at(span).type_of(*impl_item)) | ||||||
.collect::<Vec<_>>(); | ||||||
// number of type to shows at most. | ||||||
let limit = if type_candidates.len() == 4 { 4 } else { 3 }; | ||||||
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.
Suggested change
|
||||||
for ty in type_candidates.iter().take(limit) { | ||||||
err.note(&format!("the {item_kind} was found for {}", ty)); | ||||||
} | ||||||
if type_candidates.len() > limit { | ||||||
err.note(&format!( | ||||||
"the {item_kind} was found for {} more types", | ||||||
type_candidates.len() - limit | ||||||
)); | ||||||
} | ||||||
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. Instead of emitting a note on each iteration, construct a single string to be emitted. |
||||||
} | ||||||
} | ||||||
} else { | ||||||
err.span_label(span, format!("{item_kind} cannot be called on `{ty_str}` due to unsatisfied trait bounds")); | ||||||
} | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ error[E0599]: no function or associated item named `new_undirected` found for st | |
| | ||
LL | let ug = Graph::<i32, i32>::new_undirected(); | ||
| ^^^^^^^^^^^^^^ function or associated item not found in `issue_30123_aux::Graph<i32, i32>` | ||
| | ||
= note: the function or associated item was found for issue_30123_aux::Graph<N, E, Undirected> | ||
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 would ideally talk about |
||
|
||
error: aborting due to previous error | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// Test for issue 81576 | ||
// Remove generic arguments if no method is found for all possible generic argument | ||
|
||
use std::marker::PhantomData; | ||
|
||
struct Wrapper2<'a, T, const C: usize> { | ||
x: &'a T, | ||
} | ||
|
||
impl<'a, const C: usize> Wrapper2<'a, i8, C> { | ||
fn method(&self) {} | ||
} | ||
|
||
impl<'a, const C: usize> Wrapper2<'a, i16, C> { | ||
fn method(&self) {} | ||
} | ||
|
||
impl<'a, const C: usize> Wrapper2<'a, i32, C> { | ||
fn method(&self) {} | ||
} | ||
struct Wrapper<T>(T); | ||
|
||
impl Wrapper<i8> { | ||
fn method(&self) {} | ||
} | ||
|
||
impl Wrapper<i16> { | ||
fn method(&self) {} | ||
} | ||
|
||
impl Wrapper<i32> { | ||
fn method(&self) {} | ||
} | ||
|
||
impl Wrapper<i64> { | ||
fn method(&self) {} | ||
} | ||
|
||
impl Wrapper<u8> { | ||
fn method(&self) {} | ||
} | ||
|
||
impl Wrapper<u16> { | ||
fn method(&self) {} | ||
} | ||
|
||
struct Point<T> { | ||
x: T, | ||
y: T, | ||
} | ||
|
||
impl Point<f64> { | ||
fn distance(&self) -> f64 { | ||
self.x.hypot(self.y) | ||
} | ||
} | ||
|
||
struct Other; | ||
|
||
impl Other { | ||
fn other(&self) {} | ||
} | ||
|
||
struct Struct<T>{ | ||
_phatom: PhantomData<T> | ||
} | ||
|
||
impl<T> Default for Struct<T> { | ||
fn default() -> Self { | ||
Self{ _phatom: PhantomData } | ||
} | ||
} | ||
|
||
impl<T: Clone + Copy + PartialEq + Eq + PartialOrd + Ord> Struct<T> { | ||
fn method(&self) {} | ||
} | ||
|
||
fn main() { | ||
let point_f64 = Point{ x: 1_f64, y: 1_f64}; | ||
let d = point_f64.distance(); | ||
let point_i32 = Point{ x: 1_i32, y: 1_i32}; | ||
let d = point_i32.distance(); | ||
//~^ ERROR no method named `distance` found for struct `Point<i32> | ||
let d = point_i32.other(); | ||
//~^ ERROR no method named `other` found for struct `Point | ||
let v = vec![1_i32, 2, 3]; | ||
v.iter().map(|x| x * x).extend(std::iter::once(100)); | ||
//~^ ERROR no method named `extend` found for struct `Map | ||
let wrapper = Wrapper(true); | ||
wrapper.method(); | ||
//~^ ERROR no method named `method` found for struct `Wrapper<bool> | ||
wrapper.other(); | ||
//~^ ERROR no method named `other` found for struct `Wrapper | ||
let boolean = true; | ||
let wrapper = Wrapper2::<'_, _, 3> {x: &boolean}; | ||
wrapper.method(); | ||
//~^ ERROR no method named `method` found for struct `Wrapper2<'_, bool, 3_usize> | ||
wrapper.other(); | ||
//~^ ERROR no method named `other` found for struct `Wrapper2 | ||
let a = vec![1, 2, 3]; | ||
a.not_found(); | ||
//~^ ERROR no method named `not_found` found for struct `Vec | ||
let s = Struct::<f64>::default(); | ||
s.method(); | ||
//~^ ERROR the method `method` exists for struct `Struct<f64>`, but its trait bounds were not satisfied | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
error[E0599]: no method named `distance` found for struct `Point<i32>` in the current scope | ||
--> $DIR/method-not-found-generic-arg-elision.rs:82:23 | ||
| | ||
LL | struct Point<T> { | ||
| --------------- method `distance` not found for this | ||
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. Looking at this with fresh eyes in context, it might make sense to expand this label to be 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 am not sure how to get the indent for T. Also it might become very long with multiple generic parameters. |
||
... | ||
LL | let d = point_i32.distance(); | ||
| ^^^^^^^^ method not found in `Point<i32>` | ||
| | ||
= note: the method was found for Point<f64> | ||
|
||
error[E0599]: no method named `other` found for struct `Point` in the current scope | ||
--> $DIR/method-not-found-generic-arg-elision.rs:84:23 | ||
| | ||
LL | struct Point<T> { | ||
| --------------- method `other` not found for this | ||
... | ||
LL | let d = point_i32.other(); | ||
| ^^^^^ method not found in `Point<i32>` | ||
|
||
error[E0599]: no method named `extend` found for struct `Map` in the current scope | ||
--> $DIR/method-not-found-generic-arg-elision.rs:87:29 | ||
| | ||
LL | v.iter().map(|x| x * x).extend(std::iter::once(100)); | ||
| ^^^^^^ method not found in `Map<std::slice::Iter<'_, i32>, [closure@$DIR/method-not-found-generic-arg-elision.rs:87:18: 87:27]>` | ||
|
||
error[E0599]: no method named `method` found for struct `Wrapper<bool>` in the current scope | ||
--> $DIR/method-not-found-generic-arg-elision.rs:90:13 | ||
| | ||
LL | struct Wrapper<T>(T); | ||
| --------------------- method `method` not found for this | ||
... | ||
LL | wrapper.method(); | ||
| ^^^^^^ method not found in `Wrapper<bool>` | ||
| | ||
= note: the method was found for Wrapper<i8> | ||
= note: the method was found for Wrapper<i16> | ||
= note: the method was found for Wrapper<i32> | ||
= note: the method was found for 3 more types | ||
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. Ideally this would be a single note:
|
||
|
||
error[E0599]: no method named `other` found for struct `Wrapper` in the current scope | ||
--> $DIR/method-not-found-generic-arg-elision.rs:92:13 | ||
| | ||
LL | struct Wrapper<T>(T); | ||
| --------------------- method `other` not found for this | ||
... | ||
LL | wrapper.other(); | ||
| ^^^^^ method not found in `Wrapper<bool>` | ||
|
||
error[E0599]: no method named `method` found for struct `Wrapper2<'_, bool, 3_usize>` in the current scope | ||
--> $DIR/method-not-found-generic-arg-elision.rs:96:13 | ||
| | ||
LL | struct Wrapper2<'a, T, const C: usize> { | ||
| -------------------------------------- method `method` not found for this | ||
... | ||
LL | wrapper.method(); | ||
| ^^^^^^ method not found in `Wrapper2<'_, bool, 3_usize>` | ||
| | ||
= note: the method was found for Wrapper2<'a, i8, C> | ||
= note: the method was found for Wrapper2<'a, i16, C> | ||
= note: the method was found for Wrapper2<'a, i32, C> | ||
|
||
error[E0599]: no method named `other` found for struct `Wrapper2` in the current scope | ||
--> $DIR/method-not-found-generic-arg-elision.rs:98:13 | ||
| | ||
LL | struct Wrapper2<'a, T, const C: usize> { | ||
| -------------------------------------- method `other` not found for this | ||
... | ||
LL | wrapper.other(); | ||
| ^^^^^ method not found in `Wrapper2<'_, bool, 3_usize>` | ||
|
||
error[E0599]: no method named `not_found` found for struct `Vec<{integer}>` in the current scope | ||
--> $DIR/method-not-found-generic-arg-elision.rs:101:7 | ||
| | ||
LL | a.not_found(); | ||
| ^^^^^^^^^ method not found in `Vec<{integer}>` | ||
|
||
error[E0599]: the method `method` exists for struct `Struct<f64>`, but its trait bounds were not satisfied | ||
--> $DIR/method-not-found-generic-arg-elision.rs:104:7 | ||
| | ||
LL | struct Struct<T>{ | ||
| ---------------- method `method` not found for this | ||
... | ||
LL | s.method(); | ||
| ^^^^^^ method cannot be called on `Struct<f64>` due to unsatisfied trait bounds | ||
| | ||
= note: the following trait bounds were not satisfied: | ||
`f64: Eq` | ||
`f64: Ord` | ||
|
||
error: aborting due to 9 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0599`. |
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.
Ideally we'd just take the name ident of the type and use that. I think
item_name.to_string()
would work here.String operations are brittle and can break as things change in the codebase.
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.
item_name
is the method we are looking to call. I did not find a way to print the type without the generic parameter otherwise. Do you know a way to do it ?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 is the type of
item_name
from which you getspan
from?https://github.com/rust-lang/rust/pull/84221/files/6efa14b3add08188c5322db8694a8cbbea7851e5#diff-e8458f9222609acec4270bb72dcf235e7aa575268adff217d113052c3751c53cL385
I think that might have a string representation, and if not a field that is only the name with type
Ident
, which has a string representation of only the identifier.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.
Yes it is an Ident, but this does not represent the type. This is the indent of the method
For instance it is used here:
https://github.com/ABouttefeux/rust/blob/120691c590c4309fda31994931b9a561b4249c33/compiler/rustc_typeck/src/check/method/suggest.rs#L422-L431