Skip to content

Redundant Bound breaks const-generics Builders #151788

@EsotericPyramid

Description

@EsotericPyramid

The following code produces 3 errors (at the bottom in the impl of Foo):

pub trait Builder {
    type Wrapped;

    // in my project this wraps something but that isn't needed for the bug
    fn wrap(&self, inner: ()) -> Self::Wrapped;
}
pub trait BuilderUnion<T: Builder>: Builder {
    type Union: Builder;

    fn union(self, other: T) -> Self::Union;
}

pub struct WrapperBuilder<const D: usize>;

impl<const D: usize> Builder for WrapperBuilder<D> {
    type Wrapped = Wrapper<D>;

    fn wrap(&self, _: ()) -> Self::Wrapped {
        Wrapper()
    }
}

impl<const D: usize> BuilderUnion<WrapperBuilder<D>> for WrapperBuilder<D> {
    type Union = Self;

    fn union(self, _: WrapperBuilder<D>) -> Self::Union {
        self
    }
}

// in my project this wraps something but that isn't needed for the bug
pub struct Wrapper<const D: usize>();

pub trait Bar {
    fn is_bar(&self) -> () {}
}

impl<const D: usize> Bar for Wrapper<D> {}

pub trait Foo<T> {
    fn foo(self, rhs: T);
}

impl<const D: usize> Foo<WrapperBuilder<D>> for WrapperBuilder<D>
where
    Self: BuilderUnion<Self>,
{
    // the error is here, each `is_bar` produces an error because Rust doesn't think Bar is impl'd 
    fn foo(self, rhs: WrapperBuilder<D>) {
        (self).wrap(()).is_bar();
        (rhs).wrap(()).is_bar();
        (self.union(rhs)).wrap(()).is_bar();
    }
}

I expected to see this happen: No errors produced

Instead, this happened: 1 error produced for each is_bar call because the type doesn't implement Bar (even though it does because it is ultimately just Wrapper<D>)

Extra Notes:

  • removing the Self: BuilderUnion<Self> bound causes it to function correctly (even though its true in both cases and irrelevent to the first 2 calls)
  • removing the const generics causes it to function correctly

Meta

rustc --version --verbose:

rustc 1.93.0 (254b59607 2026-01-19)
binary: rustc
commit-hash: 254b59607d4417e9dffbc307138ae5c86280fe4c
commit-date: 2026-01-19
host: aarch64-apple-darwin
release: 1.93.0
LLVM version: 21.1.8

(also fails on rustc 1.95.0-nightly (e96bb7e44 2026-01-27))

Backtrace

   Compiling bug_tests v0.1.0 ([...]/bug_tests)
error[E0599]: no method named `is_bar` found for associated type `<WrapperBuilder<D> as Builder>::Wrapped` in the current scope
  --> src/lib.rs:49:25
   |
49 |         (self).wrap(()).is_bar();
   |                         ^^^^^^ method not found in `<WrapperBuilder<D> as Builder>::Wrapped`
   |
   = help: items from traits can only be used if the trait is implemented and in scope
note: `Bar` defines an item `is_bar`, perhaps you need to implement it
  --> src/lib.rs:34:1
   |
34 | pub trait Bar {
   | ^^^^^^^^^^^^^

error[E0599]: no method named `is_bar` found for associated type `<WrapperBuilder<D> as Builder>::Wrapped` in the current scope
  --> src/lib.rs:50:24
   |
50 |         (rhs).wrap(()).is_bar();
   |                        ^^^^^^ method not found in `<WrapperBuilder<D> as Builder>::Wrapped`
   |
   = help: items from traits can only be used if the trait is implemented and in scope
note: `Bar` defines an item `is_bar`, perhaps you need to implement it
  --> src/lib.rs:34:1
   |
34 | pub trait Bar {
   | ^^^^^^^^^^^^^

error[E0599]: no method named `is_bar` found for associated type `<<WrapperBuilder<D> as BuilderUnion<WrapperBuilder<D>>>::Union as Builder>::Wrapped` in the current scope
  --> src/lib.rs:51:36
   |
51 |         (self.union(rhs)).wrap(()).is_bar();
   |                                    ^^^^^^ method not found in `<<WrapperBuilder<D> as BuilderUnion<WrapperBuilder<D>>>::Union as Builder>::Wrapped`
   |
   = help: items from traits can only be used if the trait is implemented and in scope
note: `Bar` defines an item `is_bar`, perhaps you need to implement it
  --> src/lib.rs:34:1
   |
34 | pub trait Bar {
   | ^^^^^^^^^^^^^

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions