Invalid suggestion for ambiguous associated type #130166
Open
Description
opened on Sep 9, 2024
I tried this code:
Playground link
trait Parent {
type Assoc;
}
trait Child: Parent {
// send bound is not necessary for the error, but that's why this associated
// type exists at all
type Assoc: Send;
fn get_associated() -> <Self as Child>::Assoc;
}
pub struct Implementor;
pub struct AssociatedNamedFields {
field: i32,
}
impl Parent for Implementor {
type Assoc = AssociatedNamedFields;
}
impl Child for Implementor {
type Assoc = <Self as Parent>::Assoc;
fn get_associated() -> <Self as Child>::Assoc {
// this line produces the issue:
Self::Assoc { field: 1 }
// this is what rustc suggests, which is incorrect:
// <Self as Child>::Assoc { field: 1 }
}
}
I expected to see this happen:
rustc
wouldn't suggest unstable features - either wouldn't suggest anything or suggest a different fix.
Not sure what the best fix in this case would be - maybe renaming one of the associated types? Or ideally not using it at all.
Instead, this happened:
rustc
(and rust-analyzer
) suggest the following replacement:
<Self as Child>::Assoc { field: 1 }
which is not supported syntax:
error[E0658]: usage of qualified paths in this context is experimental
--> src/main.rs:31:9
|
31 | <Self as Child>::Assoc { field: 1 }
| ^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #86935 <https://github.com/rust-lang/rust/issues/86935> for more information
Side note - interestingly, this only happens for structs with named fields - for tuple or unit structs, rustc
just says:
error[E0599]: no associated item named `Assoc` found for struct `Implementor` in the current scope
--> src/main.rs:33:15
|
12 | pub struct Implementor;
| ---------------------- associated item `Assoc` not found for this struct
...
33 | Self::Assoc
| ^^^^^ associated item not found in `Implementor`
which is a confusing message by itself.
Meta
rustc --version --verbose
:
rustc 1.81.0 (eeb90cda1 2024-09-04)
binary: rustc
commit-hash: eeb90cda1969383f56a2637cbd3037bdf598841c
commit-date: 2024-09-04
host: x86_64-unknown-linux-gnu
release: 1.81.0
LLVM version: 18.1.7
I do see the same issue on nightly and beta. I also checked the last several versions and all of them had the same issue.
Backtrace
➜ dev/rust/assoc (main) ✗ RUST_BACKTRACE=1 cargo build
Compiling assoc v0.1.0 (/home/erhodes/dev/rust/assoc)
error[E0221]: ambiguous associated type `Assoc` in bounds of `Self`
--> src/main.rs:30:28
|
2 | type Assoc;
| ---------- ambiguous `Assoc` from `Parent`
...
7 | type Assoc: Send;
| ---------------- ambiguous `Assoc` from `Child`
...
30 | fn get_associated() -> Self::Assoc {
| ^^^^^^^^^^^ ambiguous associated type `Assoc`
|
help: use fully-qualified syntax to disambiguate
|
30 | fn get_associated() -> <Self as Child>::Assoc {
| ~~~~~~~~~~~~~~~~~
help: use fully-qualified syntax to disambiguate
|
30 | fn get_associated() -> <Self as Parent>::Assoc {
| ~~~~~~~~~~~~~~~~~~
error[E0221]: ambiguous associated type `Assoc` in bounds of `Self`
--> src/main.rs:31:9
|
2 | type Assoc;
| ---------- ambiguous `Assoc` from `Parent`
...
7 | type Assoc: Send;
| ---------------- ambiguous `Assoc` from `Child`
...
31 | Self::Assoc { field: 1 }
| ^^^^^^^^^^^ ambiguous associated type `Assoc`
|
help: use fully-qualified syntax to disambiguate
|
31 | <Self as Child>::Assoc { field: 1 }
| ~~~~~~~~~~~~~~~~~
help: use fully-qualified syntax to disambiguate
|
31 | <Self as Parent>::Assoc { field: 1 }
| ~~~~~~~~~~~~~~~~~~
For more information about this error, try `rustc --explain E0221`.
error: could not compile `assoc` (bin "assoc") due to 2 previous errors
Activity