From 9155f672bf98c49ea7f31cc052488640eb3eb00e Mon Sep 17 00:00:00 2001 From: Ben Reeves Date: Sat, 27 Nov 2021 23:10:18 -0600 Subject: [PATCH 1/2] typeck: Ensure proper bound vars passed to `add_bounds`. Fixes the ICE in #88586. --- compiler/rustc_typeck/src/collect.rs | 16 +++++++--------- ...sue-88586-hr-self-outlives-in-trait-def.rs | 11 +++++++++++ ...88586-hr-self-outlives-in-trait-def.stderr | 19 +++++++++++++++++++ 3 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 src/test/ui/higher-rank-trait-bounds/issue-88586-hr-self-outlives-in-trait-def.rs create mode 100644 src/test/ui/higher-rank-trait-bounds/issue-88586-hr-self-outlives-in-trait-def.stderr diff --git a/compiler/rustc_typeck/src/collect.rs b/compiler/rustc_typeck/src/collect.rs index b9db8a6be5916..856c4972700b8 100644 --- a/compiler/rustc_typeck/src/collect.rs +++ b/compiler/rustc_typeck/src/collect.rs @@ -666,7 +666,7 @@ impl ItemCtxt<'tcx> { Some(assoc_name) => self.bound_defines_assoc_item(b, assoc_name), None => true, }) - .flat_map(|b| predicates_from_bound(self, ty, b)); + .flat_map(|b| predicates_from_bound(self, ty, b, ty::List::empty())); let param_def_id = self.tcx.hir().local_def_id(param_id).to_def_id(); let from_where_clauses = ast_generics @@ -685,15 +685,17 @@ impl ItemCtxt<'tcx> { } else { None }; + let bvars = self.tcx.late_bound_vars(bp.bounded_ty.hir_id); + bp.bounds .iter() .filter(|b| match assoc_name { Some(assoc_name) => self.bound_defines_assoc_item(b, assoc_name), None => true, }) - .filter_map(move |b| bt.map(|bt| (bt, b))) + .filter_map(move |b| bt.map(|bt| (bt, b, bvars))) }) - .flat_map(|(bt, b)| predicates_from_bound(self, bt, b)); + .flat_map(|(bt, b, bvars)| predicates_from_bound(self, bt, b, bvars)); from_ty_params.chain(from_where_clauses).collect() } @@ -2433,14 +2435,10 @@ fn predicates_from_bound<'tcx>( astconv: &dyn AstConv<'tcx>, param_ty: Ty<'tcx>, bound: &'tcx hir::GenericBound<'tcx>, + bound_vars: &'tcx ty::List, ) -> Vec<(ty::Predicate<'tcx>, Span)> { let mut bounds = Bounds::default(); - astconv.add_bounds( - param_ty, - std::array::IntoIter::new([bound]), - &mut bounds, - ty::List::empty(), - ); + astconv.add_bounds(param_ty, std::array::IntoIter::new([bound]), &mut bounds, bound_vars); bounds.predicates(astconv.tcx(), param_ty) } diff --git a/src/test/ui/higher-rank-trait-bounds/issue-88586-hr-self-outlives-in-trait-def.rs b/src/test/ui/higher-rank-trait-bounds/issue-88586-hr-self-outlives-in-trait-def.rs new file mode 100644 index 0000000000000..3f5202827ad5e --- /dev/null +++ b/src/test/ui/higher-rank-trait-bounds/issue-88586-hr-self-outlives-in-trait-def.rs @@ -0,0 +1,11 @@ +// Regression test for #88586: a higher-ranked outlives bound on Self in a trait +// definition caused an ICE when debug_assertions were enabled. +// +// The error output is incidentally unhelpful; this should be improved. + +trait A where for<'a> Self: 'a +//~^ ERROR the parameter type `Self` may not live long enough +{ +} + +fn main() {} diff --git a/src/test/ui/higher-rank-trait-bounds/issue-88586-hr-self-outlives-in-trait-def.stderr b/src/test/ui/higher-rank-trait-bounds/issue-88586-hr-self-outlives-in-trait-def.stderr new file mode 100644 index 0000000000000..18618ffcc86dc --- /dev/null +++ b/src/test/ui/higher-rank-trait-bounds/issue-88586-hr-self-outlives-in-trait-def.stderr @@ -0,0 +1,19 @@ +error[E0311]: the parameter type `Self` may not live long enough + --> $DIR/issue-88586-hr-self-outlives-in-trait-def.rs:6:1 + | +LL | / trait A where for<'a> Self: 'a +LL | | +LL | | { +LL | | } + | |_^ + | + = help: consider adding an explicit lifetime bound `Self: 'a`... + = note: ...so that the type `Self` will meet its required lifetime bounds... +note: ...that is required by this bound + --> $DIR/issue-88586-hr-self-outlives-in-trait-def.rs:6:29 + | +LL | trait A where for<'a> Self: 'a + | ^^ + +error: aborting due to previous error + From 6df2c78e1c35e5dac6589f55db1e0bea6e443700 Mon Sep 17 00:00:00 2001 From: Ben Reeves Date: Sun, 28 Nov 2021 07:05:23 -0600 Subject: [PATCH 2/2] Address PR feedback --- compiler/rustc_typeck/src/collect.rs | 2 +- .../issue-88586-hr-self-outlives-in-trait-def.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_typeck/src/collect.rs b/compiler/rustc_typeck/src/collect.rs index 856c4972700b8..39fac12e29743 100644 --- a/compiler/rustc_typeck/src/collect.rs +++ b/compiler/rustc_typeck/src/collect.rs @@ -2438,7 +2438,7 @@ fn predicates_from_bound<'tcx>( bound_vars: &'tcx ty::List, ) -> Vec<(ty::Predicate<'tcx>, Span)> { let mut bounds = Bounds::default(); - astconv.add_bounds(param_ty, std::array::IntoIter::new([bound]), &mut bounds, bound_vars); + astconv.add_bounds(param_ty, [bound].into_iter(), &mut bounds, bound_vars); bounds.predicates(astconv.tcx(), param_ty) } diff --git a/src/test/ui/higher-rank-trait-bounds/issue-88586-hr-self-outlives-in-trait-def.rs b/src/test/ui/higher-rank-trait-bounds/issue-88586-hr-self-outlives-in-trait-def.rs index 3f5202827ad5e..b50f56b03d9cd 100644 --- a/src/test/ui/higher-rank-trait-bounds/issue-88586-hr-self-outlives-in-trait-def.rs +++ b/src/test/ui/higher-rank-trait-bounds/issue-88586-hr-self-outlives-in-trait-def.rs @@ -1,7 +1,7 @@ // Regression test for #88586: a higher-ranked outlives bound on Self in a trait // definition caused an ICE when debug_assertions were enabled. // -// The error output is incidentally unhelpful; this should be improved. +// FIXME: The error output in the absence of the ICE is unhelpful; this should be improved. trait A where for<'a> Self: 'a //~^ ERROR the parameter type `Self` may not live long enough