-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Lifetime Resolution for Generic Associated Types #46706
Changes from all commits
8cfaf1b
4f90eac
b90e9c9
f701b4c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -624,30 +624,77 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> { | |
} | ||
|
||
fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem) { | ||
let tcx = self.tcx; | ||
if let hir::TraitItemKind::Method(ref sig, _) = trait_item.node { | ||
self.visit_early_late( | ||
Some(tcx.hir.get_parent(trait_item.id)), | ||
&sig.decl, | ||
&trait_item.generics, | ||
|this| intravisit::walk_trait_item(this, trait_item), | ||
) | ||
} else { | ||
intravisit::walk_trait_item(self, trait_item); | ||
use self::hir::TraitItemKind::*; | ||
match trait_item.node { | ||
Method(ref sig, _) => { | ||
let tcx = self.tcx; | ||
self.visit_early_late( | ||
Some(tcx.hir.get_parent(trait_item.id)), | ||
&sig.decl, | ||
&trait_item.generics, | ||
|this| intravisit::walk_trait_item(this, trait_item), | ||
); | ||
}, | ||
Type(ref bounds, ref ty) => { | ||
let generics = &trait_item.generics; | ||
let mut index = self.next_early_index(); | ||
debug!("visit_ty: index = {}", index); | ||
let lifetimes = generics.lifetimes.iter() | ||
.map(|lt_def| Region::early(&self.tcx.hir, &mut index, lt_def)) | ||
.collect(); | ||
|
||
let next_early_index = index + generics.ty_params.len() as u32; | ||
let scope = Scope::Binder { lifetimes, next_early_index, s: self.scope }; | ||
self.with(scope, |_old_scope, this| { | ||
this.visit_generics(generics); | ||
for bound in bounds { | ||
this.visit_ty_param_bound(bound); | ||
} | ||
if let Some(ty) = ty { | ||
this.visit_ty(ty); | ||
} | ||
}); | ||
}, | ||
Const(_, _) => { | ||
// Only methods and types support generics. | ||
assert!(!trait_item.generics.is_parameterized()); | ||
intravisit::walk_trait_item(self, trait_item); | ||
}, | ||
} | ||
} | ||
|
||
fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem) { | ||
let tcx = self.tcx; | ||
if let hir::ImplItemKind::Method(ref sig, _) = impl_item.node { | ||
self.visit_early_late( | ||
Some(tcx.hir.get_parent(impl_item.id)), | ||
&sig.decl, | ||
&impl_item.generics, | ||
|this| intravisit::walk_impl_item(this, impl_item), | ||
) | ||
} else { | ||
intravisit::walk_impl_item(self, impl_item); | ||
use self::hir::ImplItemKind::*; | ||
match impl_item.node { | ||
Method(ref sig, _) => { | ||
let tcx = self.tcx; | ||
self.visit_early_late( | ||
Some(tcx.hir.get_parent(impl_item.id)), | ||
&sig.decl, | ||
&impl_item.generics, | ||
|this| intravisit::walk_impl_item(this, impl_item), | ||
) | ||
}, | ||
Type(ref ty) => { | ||
let generics = &impl_item.generics; | ||
let mut index = self.next_early_index(); | ||
debug!("visit_ty: index = {}", index); | ||
let lifetimes = generics.lifetimes.iter() | ||
.map(|lt_def| Region::early(&self.tcx.hir, &mut index, lt_def)) | ||
.collect(); | ||
|
||
let next_early_index = index + generics.ty_params.len() as u32; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not necessarily something we should fix in this PR, but it does feel like we have a lot of code duplication here, don't it? Feels like we want a "introduce scope w/ early-bound generics" helper function or something, but I'd have to look around to see how many cases could readily use such a thing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes I was thinking about that too. I'll need to look into how many cases actually use a similar pattern. If it's just these two, it may not be worth it. Even these two cases do subtly different things (e.g. no bounds checking in impl items) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I checked and didn't see a lot of places that are using this pattern. Let me know if you would like me to do anything about the small bit of repetition here. =) |
||
let scope = Scope::Binder { lifetimes, next_early_index, s: self.scope }; | ||
self.with(scope, |_old_scope, this| { | ||
this.visit_generics(generics); | ||
this.visit_ty(ty); | ||
}); | ||
}, | ||
Const(_, _) => { | ||
// Only methods and types support generics. | ||
assert!(!impl_item.generics.is_parameterized()); | ||
intravisit::walk_impl_item(self, impl_item); | ||
}, | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,8 @@ | ||
error[E0261]: use of undeclared lifetime name `'a` | ||
--> $DIR/construct_with_other_type.rs:24:37 | ||
| | ||
24 | type Quux<'a> = <T as Foo>::Bar<'a, 'static>; | ||
| ^^ undeclared lifetime | ||
|
||
error[E0110]: lifetime parameters are not allowed on this type | ||
--> $DIR/construct_with_other_type.rs:24:37 | ||
--> $DIR/construct_with_other_type.rs:25:37 | ||
| | ||
24 | type Quux<'a> = <T as Foo>::Bar<'a, 'static>; | ||
25 | type Quux<'a> = <T as Foo>::Bar<'a, 'static>; | ||
| ^^ lifetime parameter not allowed on this type | ||
|
||
error: aborting due to 2 previous errors | ||
error: aborting due to previous error | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,2 @@ | ||
error[E0261]: use of undeclared lifetime name `'a` | ||
--> $DIR/generic-associated-types-where.rs:34:32 | ||
| | ||
34 | type WithDefault<'a, T> = &'a Iterator<T>; | ||
| ^^ undeclared lifetime | ||
|
||
error: cannot continue compilation due to previous error | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![feature(generic_associated_types)] | ||
|
||
use std::ops::Deref; | ||
|
||
//FIXME(#44265): "lifetime parameters are not allowed on this type" errors will be addressed in a | ||
//follow-up PR | ||
|
||
trait Iterable { | ||
type Item<'a>; | ||
type Iter<'a>: Iterator<Item = Self::Item<'a>> | ||
//~^ ERROR lifetime parameters are not allowed on this type [E0110] | ||
+ Deref<Target = Self::Item<'b>>; | ||
//~^ ERROR undeclared lifetime | ||
//~| ERROR lifetime parameters are not allowed on this type [E0110] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: I intentionally split up this line like this so that it was clear which line each |
||
|
||
fn iter<'a>(&'a self) -> Self::Iter<'undeclared>; | ||
//~^ ERROR undeclared lifetime | ||
//~| ERROR lifetime parameters are not allowed on this type [E0110] | ||
} | ||
|
||
fn main() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
error[E0261]: use of undeclared lifetime name `'b` | ||
--> $DIR/generic_associated_type_undeclared_lifetimes.rs:22:37 | ||
| | ||
22 | + Deref<Target = Self::Item<'b>>; | ||
| ^^ undeclared lifetime | ||
|
||
error[E0261]: use of undeclared lifetime name `'undeclared` | ||
--> $DIR/generic_associated_type_undeclared_lifetimes.rs:26:41 | ||
| | ||
26 | fn iter<'a>(&'a self) -> Self::Iter<'undeclared>; | ||
| ^^^^^^^^^^^ undeclared lifetime | ||
|
||
error[E0110]: lifetime parameters are not allowed on this type | ||
--> $DIR/generic_associated_type_undeclared_lifetimes.rs:20:47 | ||
| | ||
20 | type Iter<'a>: Iterator<Item = Self::Item<'a>> | ||
| ^^ lifetime parameter not allowed on this type | ||
|
||
error[E0110]: lifetime parameters are not allowed on this type | ||
--> $DIR/generic_associated_type_undeclared_lifetimes.rs:22:37 | ||
| | ||
22 | + Deref<Target = Self::Item<'b>>; | ||
| ^^ lifetime parameter not allowed on this type | ||
|
||
error[E0110]: lifetime parameters are not allowed on this type | ||
--> $DIR/generic_associated_type_undeclared_lifetimes.rs:26:41 | ||
| | ||
26 | fn iter<'a>(&'a self) -> Self::Iter<'undeclared>; | ||
| ^^^^^^^^^^^ lifetime parameter not allowed on this type | ||
|
||
error: aborting due to 5 previous errors | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good to see the assert =)