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 lifetime mismatch with type parametrised over dyn trait object with restricted lifetime #78113

Closed
dhardy opened this issue Oct 19, 2020 · 7 comments · Fixed by #87244
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. 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 Oct 19, 2020

#![feature(generic_associated_types)]

pub trait A {}
impl A for &dyn A {}
impl A for Box<dyn A> {}

pub struct P<'a, T: ?Sized>(&'a T);
impl<'a, T: ?Sized> A for P<'a, T> {}

pub struct Q<T: ?Sized> {
    _pd: std::marker::PhantomData<T>,
}
impl A for Q<dyn A> {}

pub trait B {
    type T<'a>: A;
}

impl B for () {
    // this is ok:
    // type T<'a> = &'a dyn A;
    
    // so is this:
    // type T<'a> = P<'a, dyn A + 'a>;
    
    // this is not:
    type T<'a> = Box<dyn A + 'a>;
    
    // nor is this:
    // type T<'a> = Q<dyn A + 'a>;
}

// Note that this is also fine:
fn _f<'a>(_x: &'a ()) -> Box<dyn A + 'a> { todo!() }
// and so is this:
fn _g<'a>(_x: &'a ()) -> Q<dyn A + 'a> { todo!() }

Error:

error[E0308]: mismatched types
  --> src/lib.rs:27:5
   |
27 |     type T<'a> = Box<dyn A + 'a>;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
   |
   = note: expected trait `A`
              found trait `A`
note: the lifetime `'a` as defined on the associated item at 27:12...
  --> src/lib.rs:27:12
   |
27 |     type T<'a> = Box<dyn A + 'a>;
   |            ^^
   = note: ...does not necessarily outlive the static lifetime

Note also that a few months back, the nightly available did accept this type of code.

@camelid camelid added A-lifetimes Area: Lifetimes / regions F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs I-prioritize Issue: Indicates that prioritization has been requested for this issue. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Oct 19, 2020
@jonas-schievink jonas-schievink added the requires-nightly This issue requires a nightly compiler in some way. label Oct 19, 2020
@b-naber
Copy link
Contributor

b-naber commented Jan 12, 2021

I don't think this is a bug. What happens here is that the implementation of A for Box<dyn A> is used as a candidate for the trait object A in the Type alias type T<'a> = Box<dyn A + 'a>. Since impl A for Box<dyn A> resolves to impl A for Box<dyn A + 'static> the lifetime 'a in the TyAlias is equalized to 'static. If you parameterize over any lifetime (impl<'a> A for Box<dyn A + 'a>) then your first example compiles.

@dhardy
Copy link
Contributor Author

dhardy commented Jan 13, 2021

The second example can be "fixed" in the same way: impl<'a> A for Q<dyn A + 'a> {}

But, by that logic, _f and _g should also fail to compile, so I'm not convinced.

@b-naber
Copy link
Contributor

b-naber commented Jan 13, 2021

In the functions the lifetime in the trait object is resolved to the lifetime supplied by the function _f<'a>.

What behaviour would you expect here, i.e. what lifetime would you expect 'a to be in the associated type?

I'd agree that it's inconsistent though that the lifetimes in type T<'a> = &'a dyn A and type T<'a> = P<'a, dyn A + 'a> are not resolved to 'static.

Edit: No actually I think type T<'a> = &'a dyn A and type T<'a> = P<'a, dyn A + 'a> are correct, in impl A for &dyn A the lifetime is elided, so not necessarily resolved to static and in impl<'a, T: ?Sized> A for P<'a, T> {} you use a lifetime parameter so that 'a in P<'a, dyn A + 'a> is also an arbitrary lifetime and not 'static. Still don't see that this is a bug.

@dhardy
Copy link
Contributor Author

dhardy commented Jan 13, 2021

Well then I have two questions:

  1. Why is Box<dyn A> resolved to 'static instead of an elided lifetime? I suppose since it is simpler and usually works.
  2. Why does fn _f ... work — simply because there is no :A bound as in the trait? (Indeed, removing this bound from the assoc type fixes both failures.)

Then you may be right and this is no more than unclear documentation error message.

@b-naber
Copy link
Contributor

b-naber commented Jan 13, 2021

I'm not completely sure, but to me it seems most plausible that in impl A for Box<dyn A> the lifetimes for the trait object A are taken from the surrounding impl, which resolves to 'static, since its declared in global scope and no lifetime parameter is supplied.

I think _f works for the reason I explained in the previous comment. I don't believe it has anything to do with the trait bound. The reason that impl B works without the bound on the associated type could be that the compiler doesn't have to use impl A for Box<dyn A> as a candidate trait implementation and therefore possibly chooses impl A for &dyn A as the candidate for the trait object A in type T<'a> = Box<dyn A>, and since that implementation has an elided lifetime we don't get the 'static lifetime error.

But I could be wrong since I'm not that familiar with the trait system, the right person to answer this would be @matthewjasper

@dhardy
Copy link
Contributor Author

dhardy commented Jan 13, 2021

In the functions the lifetime in the trait object is resolved to the lifetime supplied by the function _f<'a>.

This argument doesn't make sense to me since the associated types have a lifetime supplied in the same way — the key difference, assuming that associated types are resolved in the same way as other types, is the lack of a bound. Note that this also works (no bound, not associated):

type X<'a> = Box<dyn A + 'a>;

dhardy added a commit to kas-gui/kas that referenced this issue Jan 20, 2021
@matthewjasper matthewjasper added the D-confusing Diagnostics: Confusing error or lint that should be reworked. label Feb 6, 2021
@jackh726 jackh726 added the A-diagnostics Area: Messages for errors, warnings, and lints label May 11, 2021
@dhardy
Copy link
Contributor Author

dhardy commented May 17, 2021

BTW I wrote these test cases which may/may not be of use here.

issue-78113.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. 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.

6 participants