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

Overflow evaluating a trait requirement #111313

Open
taiheioki opened this issue May 7, 2023 · 3 comments
Open

Overflow evaluating a trait requirement #111313

taiheioki opened this issue May 7, 2023 · 3 comments
Labels
A-trait-system Area: Trait system C-bug Category: This is a bug. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@taiheioki
Copy link

taiheioki commented May 7, 2023

I tried this code (playground):

use std::ops::Add;

struct A<T>(T);

impl<T> Add<A<T>> for i32
where
    i32: Add<T, Output = T>,
{
    type Output = A<T>;

    fn add(self, rhs: A<T>) -> Self::Output {
        A(Add::add(self, rhs.0))
    }
}

fn main() {}

I think this code should compile. However, I got the following compile error:

error[E0275]: overflow evaluating the requirement `i32: Add<A<_>>`
  --> src/lib.rs:12:11
   |
12 | ...   A(Add::add(self, rhs.0))
   |         ^^^^^^^^
   |
   = help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`geometry`)
note: required for `i32` to implement `Add<A<A<_>>>`
  --> src/lib.rs:5:9
   |
5  | impl<T> Add<A<T>> for i32
   |         ^^^^^^^^^     ^^^
6  | where
7  |     i32: Add<T, Output = T>,
   |                 ---------- unsatisfied trait bound introduced here
   = note: 127 redundant requirements hidden
   = note: required for `i32` to implement `Add<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<A<_>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>`

Changing A(Add::add(self, rhs.0)) to A(self + rhs.0) or A(Add::<T>::add(self, rhs.0)) results in a successful compilation. Maybe related to #39959?

Meta

rustc --version --verbose:

rustc 1.68.2 (9eb3afe9e 2023-03-27)
binary: rustc
commit-hash: 9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0
commit-date: 2023-03-27
host: aarch64-apple-darwin
release: 1.68.2
LLVM version: 15.0.6
@taiheioki taiheioki added the C-bug Category: This is a bug. label May 7, 2023
@compiler-errors
Copy link
Member

For what it's worth, this does compile with the new trait solver (-Ztrait-solver=next).

@jyn514 jyn514 added A-trait-system Area: Trait system T-types Relevant to the types team, which will review and decide on the PR/issue. labels May 26, 2023
@npuichigo
Copy link

@compiler-errors

Cannot find the trait-solver flag in nightly.

Available unstable (nightly-only) flags:

    -Z allow-features          -- Allow *only* the listed unstable features
    -Z avoid-dev-deps          -- Avoid installing dev-dependencies if possible
    -Z binary-dep-depinfo      -- Track changes to dependency artifacts
    -Z bindeps                 -- Allow Cargo packages to depend on bin, cdylib, and staticlib crates, and use the artifacts built by those crates
    -Z build-std               -- Enable Cargo to compile the standard library itself as part of a crate graph compilation
    -Z build-std-features      -- Configure features enabled for the standard library itself when building the standard library
    -Z check-cfg               -- Specify scope of compile-time checking of `cfg` names/values
    -Z codegen-backend         -- Enable the `codegen-backend` option in profiles in .cargo/config.toml file
    -Z config-include          -- Enable the `include` key in config files
    -Z credential-process      -- Add a config setting to fetch registry authentication tokens by calling an external process
    -Z direct-minimal-versions -- Resolve minimal dependency versions instead of maximum (direct dependencies only)
    -Z doctest-xcompile        -- Compile and run doctests for non-host target using runner config
    -Z dual-proc-macros        -- Build proc-macros for both the host and the target
    -Z gitoxide                -- Use gitoxide for the given git interactions, or all of them if no argument is given
    -Z host-config             -- Enable the [host] section in the .cargo/config.toml file
    -Z lints                   -- Pass `[lints]` to the linting tools
    -Z minimal-versions        -- Resolve minimal dependency versions instead of maximum
    -Z msrv-policy             -- Enable rust-version aware policy within cargo
    -Z mtime-on-use            -- Configure Cargo to update the mtime of used files
    -Z no-index-update         -- Do not update the registry index even if the cache is outdated
    -Z panic-abort-tests       -- Enable support to run tests with -Cpanic=abort
    -Z profile-rustflags       -- Enable the `rustflags` option in profiles in .cargo/config.toml file
    -Z publish-timeout         -- Enable the `publish.timeout` key in .cargo/config.toml file
    -Z registry-auth           -- Authentication for alternative registries, and generate registry authentication tokens using asymmetric cryptography
    -Z rustdoc-map             -- Allow passing external documentation mappings to rustdoc
    -Z rustdoc-scrape-examples -- Allows Rustdoc to scrape code examples from reverse-dependencies
    -Z script                  -- Enable support for single-file, `.rs` packages
    -Z target-applies-to-host  -- Enable the `target-applies-to-host` key in the .cargo/config.toml file
    -Z unstable-options        -- Allow the usage of unstable options

Run with 'cargo -Z [FLAG] [COMMAND]'

@compiler-errors
Copy link
Member

@npuichigo: Those are cargo's flags, but also I don't recommend using the new trait solver yet. It will probably cause you a lot more problems in other code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-trait-system Area: Trait system C-bug Category: This is a bug. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants