Skip to content

handle nested generics in Generics::type_param/region_param #44959

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

Merged
merged 3 commits into from
Oct 2, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/librustc/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -794,8 +794,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
let generics = self.tcx.generics_of(did);
// Account for the case where `did` corresponds to `Self`, which doesn't have
// the expected type argument.
if generics.types.len() > 0 {
let type_param = generics.type_param(param);
if let Some(type_param) = generics.type_param(param, self.tcx) {
let hir = &self.tcx.hir;
hir.as_local_node_id(type_param.def_id).map(|id| {
// Get the `hir::TyParam` to verify wether it already has any bounds.
Expand Down
36 changes: 29 additions & 7 deletions src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ pub struct Generics {
pub has_late_bound_regions: Option<Span>,
}

impl Generics {
impl<'a, 'gcx, 'tcx> Generics {
pub fn parent_count(&self) -> usize {
self.parent_regions as usize + self.parent_types as usize
}
Expand All @@ -742,14 +742,36 @@ impl Generics {
self.parent_count() + self.own_count()
}

pub fn region_param(&self, param: &EarlyBoundRegion) -> &RegionParameterDef {
assert_eq!(self.parent_count(), 0);
&self.regions[param.index as usize - self.has_self as usize]
pub fn region_param(&'tcx self,
param: &EarlyBoundRegion,
tcx: TyCtxt<'a, 'gcx, 'tcx>)
-> &'tcx RegionParameterDef
{
if let Some(index) = param.index.checked_sub(self.parent_count() as u32) {
&self.regions[index as usize - self.has_self as usize]
} else {
tcx.generics_of(self.parent.expect("parent_count>0 but no parent?"))
.region_param(param, tcx)
}
}

pub fn type_param(&self, param: &ParamTy) -> &TypeParameterDef {
assert_eq!(self.parent_count(), 0);
&self.types[param.idx as usize - self.has_self as usize - self.regions.len()]
/// Returns the `TypeParameterDef` associated with this `ParamTy`, or `None`
/// if `param` is `self`.
pub fn type_param(&'tcx self,
param: &ParamTy,
tcx: TyCtxt<'a, 'gcx, 'tcx>)
-> Option<&TypeParameterDef> {
if let Some(idx) = param.idx.checked_sub(self.parent_count() as u32) {
let type_param_start = (self.has_self as usize) + self.regions.len();
if let Some(idx) = (idx as usize).checked_sub(type_param_start) {
Some(&self.types[idx])
} else {
None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not assert_eq!((idx, self.has_self), (0, true)); followed by &self.types[0]?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idx of 1 is also mapped to 1 - 1 = 0, so I don't think &self.types[0] is correct here and Self doesn't have a TypeParameterDef.

}
} else {
tcx.generics_of(self.parent.expect("parent_count>0 but no parent?"))
.type_param(param, tcx)
}
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/librustc/ty/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,11 +515,13 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
let result = item_substs.iter().zip(impl_substs.iter())
.filter(|&(_, &k)| {
if let Some(&ty::RegionKind::ReEarlyBound(ref ebr)) = k.as_region() {
!impl_generics.region_param(ebr).pure_wrt_drop
!impl_generics.region_param(ebr, self).pure_wrt_drop
} else if let Some(&ty::TyS {
sty: ty::TypeVariants::TyParam(ref pt), ..
}) = k.as_type() {
!impl_generics.type_param(pt).pure_wrt_drop
!impl_generics.type_param(pt, self)
.expect("drop impl param doesn't have a ParameterDef?")
.pure_wrt_drop
} else {
// not a type or region param - this should be reported
// as an error.
Expand Down
10 changes: 10 additions & 0 deletions src/test/ui/lifetimes/lifetime-doesnt-live-long-enough.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,14 @@ struct Foo<T> {
foo: &'static T
}

trait X<T> {}

struct Nested<K>(K);
impl<K> Nested<K> {
fn generic_in_parent<'a, L: X<&'a Nested<K>>>() {
}
fn generic_in_child<'a, 'b, L: X<&'a Nested<M>>, M: 'b>() {
}
}

fn main() {}
35 changes: 34 additions & 1 deletion src/test/ui/lifetimes/lifetime-doesnt-live-long-enough.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,38 @@ note: ...so that the reference type `&'static T` does not outlive the data it po
28 | foo: &'static T
| ^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors
error[E0309]: the parameter type `K` may not live long enough
--> $DIR/lifetime-doesnt-live-long-enough.rs:35:5
|
34 | impl<K> Nested<K> {
| - help: consider adding an explicit lifetime bound `K: 'a`...
35 | / fn generic_in_parent<'a, L: X<&'a Nested<K>>>() {
36 | | }
| |_____^
|
note: ...so that the reference type `&'a Nested<K>` does not outlive the data it points at
--> $DIR/lifetime-doesnt-live-long-enough.rs:35:5
|
35 | / fn generic_in_parent<'a, L: X<&'a Nested<K>>>() {
36 | | }
| |_____^

error[E0309]: the parameter type `M` may not live long enough
--> $DIR/lifetime-doesnt-live-long-enough.rs:37:5
|
37 | fn generic_in_child<'a, 'b, L: X<&'a Nested<M>>, M: 'b>() {
| ^ -- help: consider adding an explicit lifetime bound `M: 'a`...
| _____|
| |
38 | | }
| |_____^
|
note: ...so that the reference type `&'a Nested<M>` does not outlive the data it points at
--> $DIR/lifetime-doesnt-live-long-enough.rs:37:5
|
37 | / fn generic_in_child<'a, 'b, L: X<&'a Nested<M>>, M: 'b>() {
38 | | }
| |_____^

error: aborting due to 4 previous errors