Skip to content
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

Merged
merged 1 commit into from
Nov 9, 2016

Conversation

estebank
Copy link
Contributor

Given the following code:

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:

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 #35987.

@rust-highfive
Copy link
Collaborator

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> {
Copy link
Contributor Author

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?

Copy link
Member

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?

Copy link
Contributor

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.

@estebank estebank force-pushed the generic-type-error-span branch 5 times, most recently from 870fe6a to 169d580 Compare October 27, 2016 21:25
@estebank
Copy link
Contributor Author

estebank commented Nov 7, 2016

r? @jonathandturner

@rust-highfive rust-highfive assigned sophiajt and unassigned pnkfelix Nov 7, 2016
use std::ops::Add;

impl<T: Clone, Add> Add for Foo<T> {
type Output = usize;
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

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 {
Copy link
Member

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;
Copy link
Member

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.

Copy link
Contributor

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.

@sophiajt
Copy link
Contributor

sophiajt commented Nov 8, 2016

A nit or two to fix, but otherwise seems okay to me.

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
```
@estebank
Copy link
Contributor Author

estebank commented Nov 9, 2016

@jonathandturner fixed.

@sanxiyn
Copy link
Member

sanxiyn commented Nov 9, 2016

@bors r+

@bors
Copy link
Contributor

bors commented Nov 9, 2016

📌 Commit 3edb4fc has been approved by sanxiyn

eddyb added a commit to eddyb/rust that referenced this pull request Nov 9, 2016
…=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.
bors added a commit that referenced this pull request Nov 9, 2016
Rollup of 15 pull requests

- Successful merges: #36868, #37134, #37229, #37250, #37370, #37428, #37432, #37472, #37524, #37614, #37622, #37627, #37636, #37644, #37654
- Failed merges: #37463, #37542, #37645
@bors bors merged commit 3edb4fc into rust-lang:master Nov 9, 2016
@estebank estebank deleted the generic-type-error-span branch November 9, 2023 05:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants