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

Associated types expansion issue #70647

Open
Popog opened this issue Apr 1, 2020 · 0 comments
Open

Associated types expansion issue #70647

Popog opened this issue Apr 1, 2020 · 0 comments
Labels
A-associated-items Area: Associated items such as associated types and consts. A-lazy-normalization Area: lazy normalization (tracking issue: #60471) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Popog
Copy link

Popog commented Apr 1, 2020

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>
   |                                   ^^^^^^^^^^^^^^^^^^
@jonas-schievink jonas-schievink added A-associated-items Area: Associated items such as associated types and consts. A-lazy-normalization Area: lazy normalization (tracking issue: #60471) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items such as associated types and consts. A-lazy-normalization Area: lazy normalization (tracking issue: #60471) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants