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

f32 seen as ambiguous associated type #26760

Closed
pfalabella opened this issue Jul 3, 2015 · 11 comments · Fixed by #60045
Closed

f32 seen as ambiguous associated type #26760

pfalabella opened this issue Jul 3, 2015 · 11 comments · Fixed by #60045
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. WG-diagnostics Working group: Diagnostics

Comments

@pfalabella
Copy link
Contributor

see http://stackoverflow.com/questions/31208465/pi-constant-is-ambiguous

   fn main() {
        let _ = f32::consts::PI;
    }

fails to compile with:

$ rustc -
<anon>:2:13: 2:28 error: ambiguous associated type; specify the type using the syntax `<f32 as Trait>::consts` [E0223]
<anon>:2     let i = f32::consts::PI;
                     ^~~~~~~~~~~~~~~
error: aborting due to previous error

on rust 1.0 stable and 1.1 stable

using the full path std::f32::consts::PI; works. The compilation error looks wrong (why is f32 an associated type)?

@jonas-schievink
Copy link
Contributor

You're using the type f32, not the module, so rustc tries to resolve consts as an associated type. If you put a use std::f32 somewhere, it works.

@pfalabella
Copy link
Contributor Author

Yes, I probably haven't made myself very clear in the description above.
I've noted that fully specifying the path works, but I still think "ambiguous associated type" referring to f32 is a confusing error message (I can't blame the person asking the SO question for interpreting it as "f32 is an associated type and defined in two or more traits" none of which is true).

@jonas-schievink
Copy link
Contributor

That's true. The message should read something like "no associated type consts found" (at least this seems to be the case here). It would also help if rustc would output a list of implemented Traits that have the ambiguous associated type whenever this happens.

@bluss
Copy link
Member

bluss commented Jul 3, 2015

Rustc is underlining all of f32::consts::PI, and it should call it an associated item, not just type.

@arielb1 arielb1 added the A-diagnostics Area: Messages for errors, warnings, and lints label Jul 4, 2015
@steveklabnik
Copy link
Member

Triage, with the new errors:

error[E0223]: ambiguous associated type
 --> <anon>:2:17
  |
2 |         let _ = f32::consts::PI;
  |                 ^^^^^^^^^^^^^^^ ambiguous associated type
  |
  = note: specify the type using the syntax `<f32 as Trait>::consts`

ouch 😦

@steveklabnik steveklabnik removed the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
@Mark-Simulacrum
Copy link
Member

The consts in the number type modules should probably be moved to associated constants. I believe we're mostly on board with this, but there's not currently an issue to my knowledge.

@Mark-Simulacrum Mark-Simulacrum added C-enhancement Category: An issue proposing an enhancement or a PR with one. C-bug Category: This is a bug. and removed C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Jul 22, 2017
@abique
Copy link

abique commented Dec 25, 2017

I'm running into this error, this is quite ridiculous to have so much troubles just to use PI.

@agrif
Copy link

agrif commented Sep 14, 2018

I ran into this today. I was reading the docs for f32 and all the examples use f32::consts::PI, so color me surprised when attempting to use that results in an associated type error (???).

Of course, the examples also use std::f32; at the top, but that's a pretty dang non-local effect and not something you are looking for right away, and of course the error doesn't point you in anywhere near that direction.

It would probably be best if there were not two different things named f32 here, but I suppose that ship has sailed.

@eminence
Copy link
Contributor

At a minimum, the ambiguous associated type, specify the type using the syntax <f32 as Trait>::consts error message is totally bogus and should be improved

@estebank estebank added the WG-diagnostics Working group: Diagnostics label Apr 12, 2019
Centril added a commit to Centril/rust that referenced this issue Apr 19, 2019
Suggest appropriate path when calling associated item on bare types

When looking at the documentation for `std::f32` or `std::str`, for
example, it is easy to get confused and assume `std::f32` and `f32`
are the same thing. Because of this, it is not uncommon to attempt
writing `f32::consts::PI` instead of the correct
`std::f32::consts::PI`. When encountering the former, which results
in an access error due to it being an inexistent path, try to access
the same path under `std`. If this succeeds, this information is
stored for later tweaking of the final E0599 to provide an
appropriate suggestion.

Fix rust-lang#26760, fix rust-lang#46660.
Centril added a commit to Centril/rust that referenced this issue Apr 19, 2019
Suggest appropriate path when calling associated item on bare types

When looking at the documentation for `std::f32` or `std::str`, for
example, it is easy to get confused and assume `std::f32` and `f32`
are the same thing. Because of this, it is not uncommon to attempt
writing `f32::consts::PI` instead of the correct
`std::f32::consts::PI`. When encountering the former, which results
in an access error due to it being an inexistent path, try to access
the same path under `std`. If this succeeds, this information is
stored for later tweaking of the final E0599 to provide an
appropriate suggestion.

Fix rust-lang#26760, fix rust-lang#46660.
Centril added a commit to Centril/rust that referenced this issue Apr 19, 2019
Suggest appropriate path when calling associated item on bare types

When looking at the documentation for `std::f32` or `std::str`, for
example, it is easy to get confused and assume `std::f32` and `f32`
are the same thing. Because of this, it is not uncommon to attempt
writing `f32::consts::PI` instead of the correct
`std::f32::consts::PI`. When encountering the former, which results
in an access error due to it being an inexistent path, try to access
the same path under `std`. If this succeeds, this information is
stored for later tweaking of the final E0599 to provide an
appropriate suggestion.

Fix rust-lang#26760, fix rust-lang#46660.
@AliAbdullah1999
Copy link

can anyone guide me in this

@AliAbdullah1999
Copy link

105 | let v_02 = ::create_vehicle{
| ^ expected one of ., ::, ;, ?, or an operator here
//i talked about this error above

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. WG-diagnostics Working group: Diagnostics
Projects
None yet
Development

Successfully merging a pull request may close this issue.