From b922ae07b3e62c7b5ead3b9c8d379e8ae2794004 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sun, 20 Oct 2024 03:01:49 +0000 Subject: [PATCH 1/2] Make LowerPolyBounds take an IntoIterator --- compiler/rustc_hir_analysis/src/collect/predicates_of.rs | 4 ++-- compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/collect/predicates_of.rs b/compiler/rustc_hir_analysis/src/collect/predicates_of.rs index 421ba40aa88ce..cdf3cb53e4f4d 100644 --- a/compiler/rustc_hir_analysis/src/collect/predicates_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/predicates_of.rs @@ -267,7 +267,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen let mut bounds = Bounds::default(); icx.lowerer().lower_poly_bounds( ty, - bound_pred.bounds.iter(), + bound_pred.bounds, &mut bounds, bound_vars, PredicateFilter::All, @@ -836,7 +836,7 @@ impl<'tcx> ItemCtxt<'tcx> { let bound_vars = self.tcx.late_bound_vars(predicate.hir_id); self.lowerer().lower_poly_bounds( bound_ty, - predicate.bounds.iter(), + predicate.bounds, &mut bounds, bound_vars, filter, diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs index 310f648b980e6..8254e6c7a037c 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs @@ -142,7 +142,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { /// There is an implied binder around `param_ty` and `hir_bounds`. /// See `lower_poly_trait_ref` for more details. #[instrument(level = "debug", skip(self, hir_bounds, bounds))] - pub(crate) fn lower_poly_bounds<'hir, I: Iterator>>( + pub(crate) fn lower_poly_bounds<'hir, I: IntoIterator>>( &self, param_ty: Ty<'tcx>, hir_bounds: I, @@ -231,7 +231,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { self.lower_poly_bounds( param_ty, - hir_bounds.iter(), + hir_bounds, &mut bounds, ty::List::empty(), predicate_filter, @@ -446,7 +446,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { Ty::new_alias(tcx, ty::Projection, projection_ty.skip_binder()); self.lower_poly_bounds( param_ty, - hir_bounds.iter(), + hir_bounds, bounds, projection_ty.bound_vars(), predicate_filter, From 10b07961a2289a83018c63e70698bd563d75ac00 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sun, 20 Oct 2024 13:39:35 +0000 Subject: [PATCH 2/2] Inline lower_mono_bounds into lower_poly_bounds --- .../src/collect/item_bounds.rs | 7 ++-- .../src/collect/predicates_of.rs | 16 +++++---- .../src/hir_ty_lowering/bounds.rs | 33 ++----------------- 3 files changed, 17 insertions(+), 39 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/collect/item_bounds.rs b/compiler/rustc_hir_analysis/src/collect/item_bounds.rs index 4346504450d0b..69ebd3a928a3a 100644 --- a/compiler/rustc_hir_analysis/src/collect/item_bounds.rs +++ b/compiler/rustc_hir_analysis/src/collect/item_bounds.rs @@ -13,6 +13,7 @@ use tracing::{debug, instrument}; use super::ItemCtxt; use super::predicates_of::assert_only_contains_predicates_from; +use crate::bounds::Bounds; use crate::hir_ty_lowering::{HirTyLowerer, PredicateFilter}; /// For associated types we include both bounds written on the type @@ -36,7 +37,8 @@ fn associated_type_bounds<'tcx>( ); let icx = ItemCtxt::new(tcx, assoc_item_def_id); - let mut bounds = icx.lowerer().lower_mono_bounds(item_ty, hir_bounds, filter); + let mut bounds = Bounds::default(); + icx.lowerer().lower_bounds(item_ty, hir_bounds, &mut bounds, ty::List::empty(), filter); // Associated types are implicitly sized unless a `?Sized` bound is found icx.lowerer().add_sized_bound(&mut bounds, item_ty, hir_bounds, None, span); @@ -303,7 +305,8 @@ fn opaque_type_bounds<'tcx>( ) -> &'tcx [(ty::Clause<'tcx>, Span)] { ty::print::with_reduced_queries!({ let icx = ItemCtxt::new(tcx, opaque_def_id); - let mut bounds = icx.lowerer().lower_mono_bounds(item_ty, hir_bounds, filter); + let mut bounds = Bounds::default(); + icx.lowerer().lower_bounds(item_ty, hir_bounds, &mut bounds, ty::List::empty(), filter); // Opaque types are implicitly sized unless a `?Sized` bound is found icx.lowerer().add_sized_bound(&mut bounds, item_ty, hir_bounds, None, span); debug!(?bounds); diff --git a/compiler/rustc_hir_analysis/src/collect/predicates_of.rs b/compiler/rustc_hir_analysis/src/collect/predicates_of.rs index cdf3cb53e4f4d..9bd8c70dcfe79 100644 --- a/compiler/rustc_hir_analysis/src/collect/predicates_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/predicates_of.rs @@ -181,9 +181,12 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen // on a trait we must also consider the bounds that follow the trait's name, // like `trait Foo: A + B + C`. if let Some(self_bounds) = is_trait { - let bounds = icx.lowerer().lower_mono_bounds( + let mut bounds = Bounds::default(); + icx.lowerer().lower_bounds( tcx.types.self_param, self_bounds, + &mut bounds, + ty::List::empty(), PredicateFilter::All, ); predicates.extend(bounds.clauses(tcx)); @@ -265,7 +268,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen } let mut bounds = Bounds::default(); - icx.lowerer().lower_poly_bounds( + icx.lowerer().lower_bounds( ty, bound_pred.bounds, &mut bounds, @@ -626,7 +629,7 @@ pub(super) fn implied_predicates_with_filter<'tcx>( bug!("trait_def_id {trait_def_id:?} is not an item"); }; - let (generics, bounds) = match item.kind { + let (generics, superbounds) = match item.kind { hir::ItemKind::Trait(.., generics, supertraits, _) => (generics, supertraits), hir::ItemKind::TraitAlias(generics, supertraits) => (generics, supertraits), _ => span_bug!(item.span, "super_predicates invoked on non-trait"), @@ -635,7 +638,8 @@ pub(super) fn implied_predicates_with_filter<'tcx>( let icx = ItemCtxt::new(tcx, trait_def_id); let self_param_ty = tcx.types.self_param; - let superbounds = icx.lowerer().lower_mono_bounds(self_param_ty, bounds, filter); + let mut bounds = Bounds::default(); + icx.lowerer().lower_bounds(self_param_ty, superbounds, &mut bounds, ty::List::empty(), filter); let where_bounds_that_match = icx.probe_ty_param_bounds_in_generics( generics, @@ -646,7 +650,7 @@ pub(super) fn implied_predicates_with_filter<'tcx>( // Combine the two lists to form the complete set of superbounds: let implied_bounds = - &*tcx.arena.alloc_from_iter(superbounds.clauses(tcx).chain(where_bounds_that_match)); + &*tcx.arena.alloc_from_iter(bounds.clauses(tcx).chain(where_bounds_that_match)); debug!(?implied_bounds); // Now require that immediate supertraits are lowered, which will, in @@ -834,7 +838,7 @@ impl<'tcx> ItemCtxt<'tcx> { }; let bound_vars = self.tcx.late_bound_vars(predicate.hir_id); - self.lowerer().lower_poly_bounds( + self.lowerer().lower_bounds( bound_ty, predicate.bounds, &mut bounds, diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs index 8254e6c7a037c..8605553f7978e 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs @@ -142,7 +142,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { /// There is an implied binder around `param_ty` and `hir_bounds`. /// See `lower_poly_trait_ref` for more details. #[instrument(level = "debug", skip(self, hir_bounds, bounds))] - pub(crate) fn lower_poly_bounds<'hir, I: IntoIterator>>( + pub(crate) fn lower_bounds<'hir, I: IntoIterator>>( &self, param_ty: Ty<'tcx>, hir_bounds: I, @@ -212,35 +212,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { } } - /// Lower HIR bounds into `bounds` given the self type `param_ty` and *no* overarching late-bound vars. - /// - /// ### Example - /// - /// ```ignore (illustrative) - /// fn foo() { } - /// // ^ ^^^^^^^^^ hir_bounds - /// // param_ty - /// ``` - pub(crate) fn lower_mono_bounds( - &self, - param_ty: Ty<'tcx>, - hir_bounds: &[hir::GenericBound<'tcx>], - predicate_filter: PredicateFilter, - ) -> Bounds<'tcx> { - let mut bounds = Bounds::default(); - - self.lower_poly_bounds( - param_ty, - hir_bounds, - &mut bounds, - ty::List::empty(), - predicate_filter, - ); - debug!(?bounds); - - bounds - } - /// Lower an associated item constraint from the HIR into `bounds`. /// /// ### A Note on Binders @@ -444,7 +415,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { // parameter to have a skipped binder. let param_ty = Ty::new_alias(tcx, ty::Projection, projection_ty.skip_binder()); - self.lower_poly_bounds( + self.lower_bounds( param_ty, hir_bounds, bounds,