Skip to content

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

Closed
@moonheart08

Description

@moonheart08

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.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