Skip to content

Commit eb0d42d

Browse files
Rollup merge of #157305 - oli-obk:relaxed-bounds-maybe, r=fmease
Eagerly decide whether relaxed bounds are allowed or not r? @davidtwco or @fmease Previously introduced in #142693 by @fmease I am trying to resolve https://github.com/rust-lang/rust/blob/d7986943e496ccb07987f4ad83c6f7033d725861/compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs#L196, but then got into a rabbit hole somehwere. I think this PR further unblocks resolving that FIXME, because it makes it easier to reason about whether we should be reporting an error about duplicate bounds or about relaxed bounds in general
2 parents 3ac83ce + 876d5f3 commit eb0d42d

2 files changed

Lines changed: 19 additions & 21 deletions

File tree

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1992,7 +1992,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
19921992
bounds: &[GenericBound],
19931993
colon_span: Option<Span>,
19941994
parent_span: Span,
1995-
rbp: RelaxedBoundPolicy<'_>,
1995+
rbp: RelaxedBoundPolicy,
19961996
itctx: ImplTraitContext,
19971997
origin: PredicateOrigin,
19981998
) -> Option<hir::WherePredicate<'hir>> {
@@ -2067,10 +2067,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
20672067
bounded_ty,
20682068
bounds,
20692069
}) => {
2070-
let rbp = if bound_generic_params.is_empty() {
2071-
RelaxedBoundPolicy::AllowedIfOnTyParam(bounded_ty.id, params)
2070+
let rbp = if bound_generic_params.is_empty()
2071+
&& let Some(res) =
2072+
self.get_partial_res(bounded_ty.id).and_then(|r| r.full_res())
2073+
&& let Res::Def(DefKind::TyParam, def_id) = res
2074+
&& params.iter().any(|p| def_id == self.local_def_id(p.id).to_def_id())
2075+
{
2076+
RelaxedBoundPolicy::Allowed
20722077
} else {
2073-
RelaxedBoundPolicy::Forbidden(RelaxedBoundForbiddenReason::LateBoundVarsInScope)
2078+
RelaxedBoundPolicy::Forbidden(RelaxedBoundForbiddenReason::WhereBound)
20742079
};
20752080
hir::WherePredicateKind::BoundPredicate(hir::WhereBoundPredicate {
20762081
bound_generic_params: self.lower_generic_params(

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,8 @@ impl<'tcx> ResolverAstLowering<'tcx> {
313313
/// Relaxed bounds should only be allowed in places where we later
314314
/// (namely during HIR ty lowering) perform *sized elaboration*.
315315
#[derive(Clone, Copy, Debug)]
316-
enum RelaxedBoundPolicy<'a> {
316+
enum RelaxedBoundPolicy {
317317
Allowed,
318-
AllowedIfOnTyParam(NodeId, &'a [ast::GenericParam]),
319318
Forbidden(RelaxedBoundForbiddenReason),
320319
}
321320

@@ -325,7 +324,9 @@ enum RelaxedBoundForbiddenReason {
325324
SuperTrait,
326325
TraitAlias,
327326
AssocTyBounds,
328-
LateBoundVarsInScope,
327+
/// We do not allow where bounds doing relaxed bounds,
328+
/// except if it's for generic parameters of the current item.
329+
WhereBound,
329330
}
330331

331332
/// Context of `impl Trait` in code, which determines whether it is allowed in an HIR subtree,
@@ -1950,7 +1951,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
19501951
fn lower_param_bound(
19511952
&mut self,
19521953
tpb: &GenericBound,
1953-
rbp: RelaxedBoundPolicy<'_>,
1954+
rbp: RelaxedBoundPolicy,
19541955
itctx: ImplTraitContext,
19551956
) -> hir::GenericBound<'hir> {
19561957
match tpb {
@@ -2188,7 +2189,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
21882189
fn lower_poly_trait_ref(
21892190
&mut self,
21902191
PolyTraitRef { bound_generic_params, modifiers, trait_ref, span, parens: _ }: &PolyTraitRef,
2191-
rbp: RelaxedBoundPolicy<'_>,
2192+
rbp: RelaxedBoundPolicy,
21922193
itctx: ImplTraitContext,
21932194
) -> hir::PolyTraitRef<'hir> {
21942195
let bound_generic_params =
@@ -2212,7 +2213,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
22122213
&self,
22132214
trait_ref: hir::TraitRef<'_>,
22142215
span: Span,
2215-
rbp: RelaxedBoundPolicy<'_>,
2216+
rbp: RelaxedBoundPolicy,
22162217
) {
22172218
// Even though feature `more_maybe_bounds` enables the user to relax all default bounds
22182219
// other than `Sized` in a lot more positions (thereby bypassing the given policy), we don't
@@ -2225,14 +2226,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
22252226

22262227
match rbp {
22272228
RelaxedBoundPolicy::Allowed => return,
2228-
RelaxedBoundPolicy::AllowedIfOnTyParam(id, params) => {
2229-
if let Some(res) = self.get_partial_res(id).and_then(|r| r.full_res())
2230-
&& let Res::Def(DefKind::TyParam, def_id) = res
2231-
&& params.iter().any(|p| def_id == self.local_def_id(p.id).to_def_id())
2232-
{
2233-
return;
2234-
}
2235-
}
22362229
RelaxedBoundPolicy::Forbidden(reason) => {
22372230
let gate = |context, subject| {
22382231
let extended = self.tcx.features().more_maybe_bounds();
@@ -2272,7 +2265,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
22722265
return;
22732266
}
22742267
RelaxedBoundForbiddenReason::AssocTyBounds
2275-
| RelaxedBoundForbiddenReason::LateBoundVarsInScope => {}
2268+
| RelaxedBoundForbiddenReason::WhereBound => {}
22762269
};
22772270
}
22782271
}
@@ -2294,7 +2287,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
22942287
fn lower_param_bounds(
22952288
&mut self,
22962289
bounds: &[GenericBound],
2297-
rbp: RelaxedBoundPolicy<'_>,
2290+
rbp: RelaxedBoundPolicy,
22982291
itctx: ImplTraitContext,
22992292
) -> hir::GenericBounds<'hir> {
23002293
self.arena.alloc_from_iter(self.lower_param_bounds_mut(bounds, rbp, itctx))
@@ -2303,7 +2296,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
23032296
fn lower_param_bounds_mut(
23042297
&mut self,
23052298
bounds: &[GenericBound],
2306-
rbp: RelaxedBoundPolicy<'_>,
2299+
rbp: RelaxedBoundPolicy,
23072300
itctx: ImplTraitContext,
23082301
) -> impl Iterator<Item = hir::GenericBound<'hir>> {
23092302
bounds.iter().map(move |bound| self.lower_param_bound(bound, rbp, itctx))

0 commit comments

Comments
 (0)