Skip to content

rustc recommends a compiler builtin to solve a trait bound issue. #71532

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

Closed
moonheart08 opened this issue Apr 24, 2020 · 4 comments
Closed

rustc recommends a compiler builtin to solve a trait bound issue. #71532

moonheart08 opened this issue Apr 24, 2020 · 4 comments
Labels
A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@moonheart08
Copy link
Contributor

While working on an emulator project, I ran into the following compiler error:

error[E0599]: no method named `wrapping_sub` found for type parameter `T` in the current scope
   --> emutk-vax/src/cpu/instrs/operands.rs:142:72
    |
142 |                 UnresolvedOperandRead::Value(regs.read_gpr_ext::<T>(v).wrapping_sub(1))
    |                                                                        ^^^^^^^^^^^^ method not found in `T`
    |
    = help: items from traits can only be used if the type parameter is bounded by the trait
help: the following traits define an item `wrapping_sub`, perhaps you need to restrict type parameter `T` with one of them:
    |
121 |     pub fn resolve_read_operand<T: compiler_builtins::int::Int>(self, regs: &mut VAXRegisterFile) -> UnresolvedOperandRead<T>
    |                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
121 |     pub fn resolve_read_operand<T: cpu::_IMPL_NUM_ToPrimitive_FOR_PrivilegeMode::_num_traits::WrappingSub>(self, regs: &mut VAXRegisterFile) -> UnresolvedOperandRead<T>
    |                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Minimal example:

pub fn foo<T: std::convert::From<u32>>() {
    let v: T = 0_u32.try_into().unwrap();
    v.wrapping_sub(1);
}

which will give the following error and recommended fix:

error[E0599]: no method named `wrapping_sub` found for type parameter `T` in the current scope
 --> rustcpls.rs:3:7
  |
3 |     v.wrapping_sub(1);
  |       ^^^^^^^^^^^^ method not found in `T`
  |
  = help: items from traits can only be used if the type parameter is bounded by the trait
help: the following trait defines an item `wrapping_sub`, perhaps you need to restrict type parameter `T` with it:
  |
1 | pub fn foo<T: compiler_builtins::int::Int + std::convert::From<u32>>() {
  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Meta

rustc --version --verbose:

rustc 1.42.0
binary: rustc
commit-hash: unknown
commit-date: unknown
host: x86_64-unknown-linux-gnu
release: 1.42.0
LLVM version: 9.0

Also occurs on:
rustc 1.43.0 (4fb7144ed 2020-04-20)
rustc 1.44.0-nightly (b2e36e6c2 2020-04-22)

@moonheart08 moonheart08 added the C-bug Category: This is a bug. label Apr 24, 2020
@jonas-schievink jonas-schievink added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed C-bug Category: This is a bug. labels Apr 24, 2020
@moonheart08
Copy link
Contributor Author

moonheart08 commented Apr 24, 2020

@jonas-schievink how does the compiler recommending a thing you can't even use not count as a bug in suggestion diagnostics?
compiler_builtins is an internal part of the compiler, not a module one can simply use.

@jonas-schievink
Copy link
Contributor

You can use it via

#![feature(rustc_private)]
extern crate compiler_builtins;

And some libraries do legitimately use rustc_private crates (such as Clippy and custom codegen backends), so it isn't totally obvious that this shouldn't be suggested, but I see your point.

@moonheart08
Copy link
Contributor Author

It was also being recommended on stable, where you /can't/ use it, as well.

@alexcrichton
Copy link
Member

Fixed since #72629 was included with #72759

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants