Skip to content

Commit 82c1c87

Browse files
committed
Eagerly decide whether relaxed bounds are allowed or not
1 parent 4530eac commit 82c1c87

2 files changed

Lines changed: 14 additions & 18 deletions

File tree

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,7 +1986,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
19861986
bounds: &[GenericBound],
19871987
colon_span: Option<Span>,
19881988
parent_span: Span,
1989-
rbp: RelaxedBoundPolicy<'_>,
1989+
rbp: RelaxedBoundPolicy,
19901990
itctx: ImplTraitContext,
19911991
origin: PredicateOrigin,
19921992
) -> Option<hir::WherePredicate<'hir>> {
@@ -2061,8 +2061,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
20612061
bounded_ty,
20622062
bounds,
20632063
}) => {
2064-
let rbp = if bound_generic_params.is_empty() {
2065-
RelaxedBoundPolicy::AllowedIfOnTyParam(bounded_ty.id, params)
2064+
let rbp = if bound_generic_params.is_empty()
2065+
&& let Some(res) =
2066+
self.get_partial_res(bounded_ty.id).and_then(|r| r.full_res())
2067+
&& let Res::Def(DefKind::TyParam, def_id) = res
2068+
&& params.iter().any(|p| def_id == self.local_def_id(p.id).to_def_id())
2069+
{
2070+
RelaxedBoundPolicy::Allowed
20662071
} else {
20672072
RelaxedBoundPolicy::Forbidden(RelaxedBoundForbiddenReason::LateBoundVarsInScope)
20682073
};

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,8 @@ impl<'tcx> ResolverAstLowering<'tcx> {
318318
/// Relaxed bounds should only be allowed in places where we later
319319
/// (namely during HIR ty lowering) perform *sized elaboration*.
320320
#[derive(Clone, Copy, Debug)]
321-
enum RelaxedBoundPolicy<'a> {
321+
enum RelaxedBoundPolicy {
322322
Allowed,
323-
AllowedIfOnTyParam(NodeId, &'a [ast::GenericParam]),
324323
Forbidden(RelaxedBoundForbiddenReason),
325324
}
326325

@@ -1955,7 +1954,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
19551954
fn lower_param_bound(
19561955
&mut self,
19571956
tpb: &GenericBound,
1958-
rbp: RelaxedBoundPolicy<'_>,
1957+
rbp: RelaxedBoundPolicy,
19591958
itctx: ImplTraitContext,
19601959
) -> hir::GenericBound<'hir> {
19611960
match tpb {
@@ -2193,7 +2192,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
21932192
fn lower_poly_trait_ref(
21942193
&mut self,
21952194
PolyTraitRef { bound_generic_params, modifiers, trait_ref, span, parens: _ }: &PolyTraitRef,
2196-
rbp: RelaxedBoundPolicy<'_>,
2195+
rbp: RelaxedBoundPolicy,
21972196
itctx: ImplTraitContext,
21982197
) -> hir::PolyTraitRef<'hir> {
21992198
let bound_generic_params =
@@ -2217,7 +2216,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
22172216
&self,
22182217
trait_ref: hir::TraitRef<'_>,
22192218
span: Span,
2220-
rbp: RelaxedBoundPolicy<'_>,
2219+
rbp: RelaxedBoundPolicy,
22212220
) {
22222221
// Even though feature `more_maybe_bounds` enables the user to relax all default bounds
22232222
// other than `Sized` in a lot more positions (thereby bypassing the given policy), we don't
@@ -2230,14 +2229,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
22302229

22312230
match rbp {
22322231
RelaxedBoundPolicy::Allowed => return,
2233-
RelaxedBoundPolicy::AllowedIfOnTyParam(id, params) => {
2234-
if let Some(res) = self.get_partial_res(id).and_then(|r| r.full_res())
2235-
&& let Res::Def(DefKind::TyParam, def_id) = res
2236-
&& params.iter().any(|p| def_id == self.local_def_id(p.id).to_def_id())
2237-
{
2238-
return;
2239-
}
2240-
}
22412232
RelaxedBoundPolicy::Forbidden(reason) => {
22422233
let gate = |context, subject| {
22432234
let extended = self.tcx.features().more_maybe_bounds();
@@ -2299,7 +2290,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
22992290
fn lower_param_bounds(
23002291
&mut self,
23012292
bounds: &[GenericBound],
2302-
rbp: RelaxedBoundPolicy<'_>,
2293+
rbp: RelaxedBoundPolicy,
23032294
itctx: ImplTraitContext,
23042295
) -> hir::GenericBounds<'hir> {
23052296
self.arena.alloc_from_iter(self.lower_param_bounds_mut(bounds, rbp, itctx))
@@ -2308,7 +2299,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
23082299
fn lower_param_bounds_mut(
23092300
&mut self,
23102301
bounds: &[GenericBound],
2311-
rbp: RelaxedBoundPolicy<'_>,
2302+
rbp: RelaxedBoundPolicy,
23122303
itctx: ImplTraitContext,
23132304
) -> impl Iterator<Item = hir::GenericBound<'hir>> {
23142305
bounds.iter().map(move |bound| self.lower_param_bound(bound, rbp, itctx))

0 commit comments

Comments
 (0)