Skip to content

Associated types expansion issue #70647

Open
@Popog

Description

@Popog

Adding an bound on an associated type (where T::BarOutput: TraitFoo) causes the compiler to erroneously complain about the original bound:

the trait bound ``T: TraitBar<u64>`` is not satisfied

The compiler is able to expand the associated type BazOutput enough to generate that error, but not enough to realize it would satisfy that bound.

pub trait TraitFoo {}

pub trait TraitBar<T> {
    type BarOutput;
}

pub trait TraitBaz {
    type BazOutput;
}
pub trait TraitFum<T> {}

pub struct StructZot;

impl TraitBaz for StructZot {
    type BazOutput = u64;
}

macro_rules! test {
    (1) => {
        impl<T: TraitBar<<Self as TraitBaz>::BazOutput>> TraitFum<T> for StructZot {}
    };
    (2) => {
        impl<T: TraitBar<<StructZot as TraitBaz>::BazOutput>> TraitFum<T> for StructZot {}
    };
    (3) => {
        impl<T: TraitBar<u64>> TraitFum<T> for StructZot {}
    };
    (4) => {
        impl<T: TraitBar<<Self as TraitBaz>::BazOutput>> TraitFum<T> for StructZot where
            T::BarOutput: TraitFoo
        {
        }
    };
    (5) => {
        impl<T: TraitBar<<StructZot as TraitBaz>::BazOutput>> TraitFum<T> for StructZot where
            T::BarOutput: TraitFoo
        {
        }
    };
    (6) => {
        impl<T: TraitBar<u64>> TraitFum<T> for StructZot where T::BarOutput: TraitFoo {}
    };
}

//test!{1} // Works
//test!{2} // Works
//test!{3} // Works
//test!{4} // Fails
test!{5} // Fails
//test!{6} // Works

(Playground)

I expected to see all 6 versions compile cleanly.

Instead, this happened:

error[E0277]: the trait bound `T: TraitBar<u64>` is not satisfied
  --> src/lib.rs:35:9
   |
35 | /         impl<T: TraitBar<<StructZot as TraitBaz>::BazOutput>> TraitFum<T> for StructZot where
36 | |             T::BarOutput: TraitFoo
37 | |         {
38 | |         }
   | |_________^ the trait `TraitBar<u64>` is not implemented for `T`
...
49 |   test!{5} // Fails
   |   -------- in this macro invocation
   |
   = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider further restricting type parameter `T`
   |
36 |             T::BarOutput: TraitFoo, T: TraitBar<u64>
   |                                   ^^^^^^^^^^^^^^^^^^

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-lazy-normalizationArea: Lazy normalization (tracking issue: #60471)A-trait-systemArea: Trait systemC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.fixed-by-next-solverFixed by the next-generation trait solver, `-Znext-solver`.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions