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

GAT + enum: incorrect unused lifetime error #69184

Closed
dhardy opened this issue Feb 15, 2020 · 6 comments · Fixed by #80558
Closed

GAT + enum: incorrect unused lifetime error #69184

dhardy opened this issue Feb 15, 2020 · 6 comments · Fixed by #80558
Labels
A-lifetimes Area: lifetime related A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@dhardy
Copy link
Contributor

dhardy commented Feb 15, 2020

This is a follow up on #67089, which now works. The following doesn't (play link):

#![feature(generic_associated_types)]

trait A {
    type B<'a>;
    
    fn make_b<'a>(&'a self) -> Self::B<'a>;
}

struct S {}

impl A for S {
    type B<'a> = &'a S;
    fn make_b<'a>(&'a self) -> &'a Self {
        self
    }
}

enum E<'a> {
    S(<S as A>::B<'a>),
}

Error is:

18 | enum E<'a> {
   |        ^^ unused parameter

however, this lifetime clearly is used and is required.

@jonas-schievink jonas-schievink added A-lifetimes Area: lifetime related A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 15, 2020
@Centril
Copy link
Contributor

Centril commented Feb 17, 2020

The variance code in src/librustc_typeck/variance/constraints.rs is:

            ty::Projection(ref data) => {
                let tcx = self.tcx();
                self.add_constraints_from_trait_ref(current, data.trait_ref(tcx), variance);
            }

It seems to me that this is disregarding the substitutions for ::B<'a> if I'm understanding what .trait_ref(tcx) does here wrt. the substitutions (this pretty poorly documented right now).

cc @matthewjasper

@dhardy
Copy link
Contributor Author

dhardy commented Feb 17, 2020

BTW how long do we need to keep this warning around? I understand that this is basically a given when using nightly features. (It puts me off using it in my normal build path — but presumably that's exactly what is wanted, in order to detect bugs like the one reported here.)

warning: the feature `generic_associated_types` is incomplete and may cause the compiler to crash

@Centril
Copy link
Contributor

Centril commented Feb 17, 2020

We only use INCOMPLETE_FEATURES for those that have been ICE-prone in the past or are generally not in a "considered implemented modulo bugs" state. It's quite a small set of features that are incomplete like this. GATs specifically are to my knowledge nowhere near ready to be "implemented modulo bugs".

@dhardy
Copy link
Contributor Author

dhardy commented Feb 17, 2020

On this topic, the following builds,

trait Foo {}
impl<'a, T: A> Foo for T
where <T as A>::B<'a>: std::fmt::Debug
{}

but the bound <T as A>::B<'_>: std::fmt::Debug fails with:

error[E0637]: `'_` cannot be used here

I *think* this should work?

@dhardy
Copy link
Contributor Author

dhardy commented Feb 17, 2020

Also, the following fails:

trait Foo {}
impl<'a, T: A> Foo for T
where <T as A>::B<'a>: 'static
{}

with:

error[E0310]: the associated type `<T as A>::B<'_>` may not live long enough

This bound may not make much sense (since there is another lifetime parameter), but I believe it should be valid to specify here.

@Dirbaio
Copy link
Contributor

Dirbaio commented Jan 2, 2021

I'm running into a similar issue which I believe is the same as this: (play)

trait Bar{ 
    type Foo<'a>: 'a;
}

struct What<'a, T: Bar> {
    foo: T::Foo<'a>,
}

@bors bors closed this as completed in bbc01bb Jan 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: lifetime related A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants