-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Point to type argument span when used as trait #37428
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @pnkfelix (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
@@ -399,6 +399,16 @@ impl Generics { | |||
pub fn is_parameterized(&self) -> bool { | |||
self.is_lt_parameterized() || self.is_type_parameterized() | |||
} | |||
pub fn span_for_name(&self, name: &str) -> Option<Span> { |
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.
I'm not convinced that this is the best way of getting the type argument's span. Does anyone have a pointer as to an alternative?
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.
I remember doing such a thing but can't find the PR. From my point of view, this is correct. However I can totally overpass a better solution.
Anything in mind @eddyb?
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.
Seems okay. If there are duplicate type parameter names, you'll get a different error for that.
870fe6a
to
169d580
Compare
use std::ops::Add; | ||
|
||
impl<T: Clone, Add> Add for Foo<T> { | ||
type Output = usize; |
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.
Rust uses 4 spaces indent.
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.
Fixed.
169d580
to
c56cc5f
Compare
pub fn span_for_name(&self, name: &str) -> Option<Span> { | ||
let mut span = None; | ||
for t in &self.ty_params { | ||
if &format!("{}", t.ident.name.as_str()) == name { |
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.
This is redundant. You can do &t.ident.name.as_str() == name
instead.
for t in &self.ty_params { | ||
if &format!("{}", t.ident.name.as_str()) == name { | ||
span = Some(t.span); | ||
break; |
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.
You can return Some(t.span);
instead of breaking, and get rid of the local variable span
.
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.
Yeah, returning is probably slightly cleaner/easier to read.
A nit or two to fix, but otherwise seems okay to me. |
c56cc5f
to
0037756
Compare
Given the following code: ```rust struct Foo<T: Clone>(T); use std::ops::Add; impl<T: Clone, Add> Add for Foo<T> { type Output = usize; fn add(self, rhs: Self) -> Self::Output { unimplemented!(); } } ``` present the following output: ```nocode error[E0404]: `Add` is not a trait --> file3.rs:5:21 | 5 | impl<T: Clone, Add> Add for Okok<T> { | --- ^^^ expected trait, found type parameter | | | type parameter defined here ```
0037756
to
3edb4fc
Compare
@jonathandturner fixed. |
@bors r+ |
📌 Commit 3edb4fc has been approved by |
…=sanxiyn Point to type argument span when used as trait Given the following code: ``` rust struct Foo<T: Clone>(T); use std::ops::Add; impl<T: Clone, Add> Add for Foo<T> { type Output = usize; fn add(self, rhs: Self) -> Self::Output { unimplemented!(); } } ``` present the following output: ``` nocode error[E0404]: `Add` is not a trait --> file3.rs:5:21 | 5 | impl<T: Clone, Add> Add for Okok<T> { | --- ^^^ expected trait, found type parameter | | | type parameter defined here ``` Fixes rust-lang#35987.
Given the following code:
present the following output:
Fixes #35987.