Skip to content

rustc suggest adding an import of compiler_builtins #142676

Closed

Description

@jendrikw

Code

fn f() {
    u8::cast_from_lossy(9);
}

Current output

Compiling playground v0.0.1 (/playground)
error[E0599]: no function or associated item named `cast_from_lossy` found for type `u8` in the current scope
 --> src/lib.rs:2:9
  |
2 |     u8::cast_from_lossy(9);
  |         ^^^^^^^^^^^^^^^ function or associated item not found in `u8`
  |
  = help: items from traits can only be used if the trait is in scope
help: trait `CastFrom` which provides `cast_from_lossy` is implemented but not in scope; perhaps you want to import it
  |
1 + use compiler_builtins::math::libm::support::int_traits::CastFrom;
  |

For more information about this error, try `rustc --explain E0599`.
error: could not compile `playground` (lib) due to 1 previous error

Desired output

Compiling playground v0.0.1 (/playground)
error[E0599]: no function or associated item named `cast_from_lossy` found for type `u8` in the current scope
 --> src/lib.rs:2:9
  |
2 |     u8::cast_from_lossy(9);
  |         ^^^^^^^^^^^^^^^ function or associated item not found in `u8`
  |

For more information about this error, try `rustc --explain E0599`.
error: could not compile `playground` (lib) due to 1 previous error

Rationale and extra context

The suggested import does not work. Additionally, the import will be suggested even if it's already there.

Other cases

A previously reported issue #71090 was supposed to be fixed, but maybe it regressed?

Rust Version

rustc 1.87.0 (17067e9ac 2025-05-09)
binary: rustc
commit-hash: 17067e9ac6d7ecb70e50f92c1944e545188d2359
commit-date: 2025-05-09
host: x86_64-unknown-linux-gnu
release: 1.87.0
LLVM version: 20.1.1

Anything else?

No response

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Jun 18, 2025
  2. ChrisDenton commented on Jun 19, 2025

    @ChrisDenton
    Member

    This regressed in version 1.86.

    F:\scratch> cat test.rs
    fn f() {
        u8::cast_from(9);
    }
    
    F:\scratch> rustc +1.86 test.rs --crate-type lib
    error[E0599]: no function or associated item named `cast_from` found for type `u8` in the current scope
     --> test.rs:2:9
      |
    2 |     u8::cast_from(9);
      |         ^^^^^^^^^ function or associated item not found in `u8`
      |
      = help: items from traits can only be used if the trait is in scope
    help: trait `CastFrom` which provides `cast_from` is implemented but not in scope; perhaps you want to import it
      |
    1 + use compiler_builtins::math::libm::support::int_traits::CastFrom;
      |
    
    error: aborting due to 1 previous error
    
    F:\scratch> rustc +1.85 test.rs --crate-type lib
    error[E0599]: no function or associated item named `cast_from` found for type `u8` in the current scope
     --> test.rs:2:9
      |
    2 |     u8::cast_from(9);
      |         ^^^^^^^^^ function or associated item not found in `u8`
    
    error: aborting due to 1 previous error
    
    For more information about this error, try `rustc --explain E0599`.
    

    As noted in the OP, this error was previously seen in #71090 but had been fixed years ago.

  3. workingjubilee commented on Jun 19, 2025

    @workingjubilee
    Member
  4. ChrisDenton commented on Jun 19, 2025

    @ChrisDenton
    Member

    Bisection:

    searched nightlies: from nightly-2025-01-27 to nightly-2025-06-19
    regressed nightly: nightly-2025-02-26
    searched commit range: 617aad8...85abb27
    regressed commit: 4ecd70d

    Most obvious candidate is #137515 which leads to rust-lang/compiler-builtins#766

  5. tgross35 commented on Jun 19, 2025

    @tgross35
    Member

    You're kidding me, still? This was supposed to be gone forever with #135501 🙃. Either compiler_builtins is getting marked public somehow, or diagnostics are suggesting from private crates when they shouldn't be.

  6. tgross35 commented on Jun 19, 2025

    @tgross35
    Member

    With RUSTC_LOG=rustc_metadata=info https://rust.godbolt.org/z/qcG85vvr3 I see:

       name: compiler_builtins
       cnum: 4
       hash: 9c1a86ef5a39ee579d9f9d59f9e50b8b
       reqd: Explicit
       priv: true
        rlib: /opt/compiler-explorer/rust-1.87.0/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-b91655ac28a1a56c.rlib
    

    So it is still private at the end of resolution, meaning that the diagnostic is making suggestions from crates it shouldn't. At least resolution doesn't seem borked.

  7. estebank commented on Jun 20, 2025

    @estebank
    Contributor

    or diagnostics are suggesting from private crates when they shouldn't be.

    This has happened in the past...

  8. tgross35 commented on Jun 24, 2025

    @tgross35
    Member

    I probably won't be able to look at this for a while and this isn't the highest priority, so I'll label for someone to pick it up if they get the chance. The steps are most likely:

    1. The error comes from here
      fn suggest_valid_traits(
      &self,
      err: &mut Diag<'_>,
      item_name: Ident,
      valid_out_of_scope_traits: Vec<DefId>,
      explain: bool,
      ) -> bool {
      if !valid_out_of_scope_traits.is_empty() {
      let mut candidates = valid_out_of_scope_traits;
      candidates.sort_by_key(|id| self.tcx.def_path_str(id));
      candidates.dedup();
      // `TryFrom` and `FromIterator` have no methods
      let edition_fix = candidates
      .iter()
      .find(|did| self.tcx.is_diagnostic_item(sym::TryInto, **did))
      .copied();
      if explain {
      err.help("items from traits can only be used if the trait is in scope");
      }
      let msg = format!(
      "{this_trait_is} implemented but not in scope",
      this_trait_is = if candidates.len() == 1 {
      format!(
      "trait `{}` which provides `{item_name}` is",
      self.tcx.item_name(candidates[0]),
      )
      } else {
      format!("the following traits which provide `{item_name}` are")
      }
      );
      . Figure out which time it is called is relevant (probably just with info! statements and RUSTC_LOG https://rustc-dev-guide.rust-lang.org/tracing.html)
    2. Figure out where that valid_out_of_scope_traits comes from and why it's including private dependencies. Or maybe just filter it within suggest_valid_traits.
    3. Write a test that reproduces the error without the fix (see RFC3516)

    Medium-hard?

  9. added
    E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.
    E-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.
    on Jun 24, 2025
  10. cyrgani commented on Jun 25, 2025

    @cyrgani
    Contributor

    A very similar issue with object instead of compiler_builtins is #138191.

  11. Qelxiros commented on Jun 25, 2025

    @Qelxiros
    Contributor

    @rustbot claim

  12. 1 remaining item

  13. tgross35 commented on Jun 25, 2025

    @tgross35
    Member

    Most obvious candidate is #137515 which leads to rust-lang/compiler-builtins#766

    Looking at the issue @cyrgani linked, the diagnostic has been an issue back to 1.56 https://rust.godbolt.org/z/cE9YeoqYr. The relevant bit here is just that the relevant trait isn't gated at the AST level starting with that PR. It's still gated by a feature, but apparently being in a private crate behind a disabled cfg is no match for our enthusiastic diagnostics.

    Linking some zulip discussion: #t-compiler/help > compiler_builtins isn't a private dependency

  14. added 3 commits that reference this issue on Jul 1, 2025
    2f39595
    71b32e8
    6d5f88d
  15. added a commit that references this issue on Jul 2, 2025
    f916bb2
  16. added a commit that references this issue on Jul 3, 2025
    9508371
  17. added 2 commits that reference this issue on Jul 3, 2025
    7e90008
    0aaac88
  18. added a commit that references this issue on Jul 3, 2025
    db10318
  19. added a commit that references this issue on Jul 4, 2025
    dc28179
  20. added a commit that references this issue on Jul 4, 2025
    14b3793
  21. added a commit that references this issue on Jul 11, 2025
    fa70f6b
  22. suprohub commented on Oct 17, 2025

    @suprohub

    Can someone reopen? I get now similar issue.
    If i add in random place of my crate f64::PI, it suggests me:

    error[E0599]: no associated item named `PI` found for type `f64` in the current scope
      --> src/num.rs:77:14
       |
    77 |         f64::PI;
       |              ^^ associated item not found in `f64`
       |
       = help: items from traits can only be used if the trait is in scope
    help: trait `Float` which provides `PI` is implemented but not in scope; perhaps you want to import it
       |
     1 + use compiler_builtins::math::libm_math::support::float_traits::Float;
       |
    

    No one of my crates use compiler builtins, and my Cargo.lock dont have compiler_builtins too.
    My crate: https://github.com/suprohub/cos

  23. tgross35 commented on Oct 20, 2025

    @tgross35
    Member

    @suprohub could you open a new issue for this? Please include the rustc version you used and steps/code to reproduce as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.E-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions