Skip to content

f32 seen as ambiguous associated type #26760

Closed

Description

@pfalabella

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)?

Activity

  1. jonas-schievink commented on Jul 3, 2015

    @jonas-schievink
    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.

  2. pfalabella commented on Jul 3, 2015

    @pfalabella
    ContributorAuthor

    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).

  3. jonas-schievink commented on Jul 3, 2015

    @jonas-schievink
    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.

  4. bluss commented on Jul 3, 2015

    @bluss
    Contributor

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

  5. steveklabnik commented on Nov 29, 2016

    @steveklabnik
    Contributor

    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 😦

  6. removed
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Mar 9, 2017
  7. Mark-Simulacrum commented on Jun 1, 2017

    @Mark-Simulacrum
    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.

  8. added
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    C-bugCategory: This is a bug.
    and removed
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    on Jul 22, 2017
  9. abique commented on Dec 25, 2017

    @abique

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

  10. agrif commented on Sep 14, 2018

    @agrif

    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.

  11. eminence commented on Sep 14, 2018

    @eminence
    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

  12. added 3 commits that reference this issue on Apr 19, 2019
    e343271
    2a9d97c
    bd0d097
  13. AliAbdullah1999 commented on Dec 9, 2019

    @AliAbdullah1999

    can anyone guide me in this

  14. AliAbdullah1999 commented on Dec 9, 2019

    @AliAbdullah1999

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.WG-diagnosticsWorking group: Diagnostics

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions