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

If two traits provide a method with the same name, and said method is used in an "unambiguous" way, the compiler picks the "wrong" trait #16949

Closed
japaric opened this issue Sep 2, 2014 · 3 comments
Labels
P-medium Medium priority

Comments

@japaric
Copy link
Member

japaric commented Sep 2, 2014

Worth noting, that the traits are implemented for "non-overlapping" types. (Hence, I said "unambiguous" in the title)

STR

use std::iter::AdditiveIterator;

// I can't reuse the trait defined in the stdlib, create a new one
trait MyAdditiveIterator<T> {
    fn sum(self) -> Option<Row<Vec<T>>>;
}

// newtype to represent the row of a matrix
struct Row<D> {
    data: D,
}

// consume a "row-by-row" iterator, and return the sum of the rows
impl<T, D, I: Iterator<Row<D>>> MyAdditiveIterator<T> for I {
    fn sum(self) -> Option<Row<Vec<T>>> {
        unimplemented!();
    }
}

fn main() {
    // Clearly `range` is *not* an iterator over rows (`u8` != `Row<D>` for any `D`),
    // so the compiler should *not* pick `MyAdditiveIterator` but it does
    range(0u8, 3).sum();
}

Output

rows.rs:23:5: 23:24 error: expected core::iter::Iterator<Row<<generic #519>>>, found core::iter::Iterator<u8> (expected struct Row, found u8) [E0095]
rows.rs:23     range(0u8, 3).sum();
               ^~~~~~~~~~~~~~~~~~~
rows.rs:23:5: 23:24 error: expected core::iter::Iterator<Row<<generic #519>>>, found core::iter::Iterator<u8> (expected struct Row, found u8) [E0095]
rows.rs:23     range(0u8, 3).sum();
               ^~~~~~~~~~~~~~~~~~~
rows.rs:23:5: 23:24 error: expected core::iter::Iterator<Row<<generic #519>>>, found core::iter::Iterator<u8> (expected struct Row, found u8) [E0095]
rows.rs:23     range(0u8, 3).sum();
               ^~~~~~~~~~~~~~~~~~~
rows.rs:23:5: 23:24 error: expected core::iter::Iterator<Row<<generic #519>>>, found core::iter::Iterator<u8> (expected struct Row, found u8) [E0095]
rows.rs:23     range(0u8, 3).sum();
               ^~~~~~~~~~~~~~~~~~~
error: aborting due to 4 previous errors

(Off-topic: Same error 4 times?)

Version

rustc 0.12.0-pre (dfbd4669c 2014-09-02 10:31:04 +0000)

For completeness sake, here's the definition/impl of std::iter::AdditiveIterator:

pub trait AdditiveIterator<A> {
    fn sum(&mut self) -> A;
}

impl<A: Zero + Add<A, A>, T: Iterator<A>> AdditiveIterator<A> for T {}

Altough I can work around this issue, I get the feeling that the compiler should be able to handle this case (but I could be wrong).

cc @nikomatsakis

EDIT: Added rustc version

@pcwalton
Copy link
Contributor

Recommend closing as a dupe of trait reform.

@pnkfelix
Copy link
Member

  1. It is backwards compatible to fix this and,
  2. It should be handled by trait reform, e..g Improve method lookup auto-deref behavior (subissue of trait reform) #12825.

Let's leave this open (to ensure we add a separate test for it in case its not a true dupe of #12825), but its just P-high, not 1.0 blocker.

@steveklabnik
Copy link
Member

This code now compiles correctly.

lnicola pushed a commit to lnicola/rust that referenced this issue Apr 7, 2024
Fix tasks in tasks.json

rust-lang#16839 refactored the representation of tasks inside the VS Code extension. However, this data type is exposed to users, who can define their own tasks in the same format in `tasks.json` or `.code-workspace`.

Revert the data type to have a `command` field rather than a `program` field, and document the different fields. This code is also a little complex, so split out a `cargoToExecution` to handle the Task to Execution conversion logic.

After this change, any tasks.json with a `command` field works again. For example, the following tasks.json works as expected:

```
{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "cargo",
			"command": "build",
			"problemMatcher": [
			  "$rustc"
			],
			"group": "build",
			"label": "my example cargo build task"
		}
	]
}
```

Fixes rust-lang#16943 rust-lang#16949
lnicola pushed a commit to lnicola/rust that referenced this issue Apr 20, 2024
Fix tasks in tasks.json

rust-lang#16839 refactored the representation of tasks inside the VS Code extension. However, this data type is exposed to users, who can define their own tasks in the same format in `tasks.json` or `.code-workspace`.

Revert the data type to have a `command` field rather than a `program` field, and document the different fields. This code is also a little complex, so split out a `cargoToExecution` to handle the Task to Execution conversion logic.

After this change, any tasks.json with a `command` field works again. For example, the following tasks.json works as expected:

```
{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "cargo",
			"command": "build",
			"problemMatcher": [
			  "$rustc"
			],
			"group": "build",
			"label": "my example cargo build task"
		}
	]
}
```

Fixes rust-lang#16943 rust-lang#16949
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P-medium Medium priority
Projects
None yet
Development

No branches or pull requests

5 participants