Skip to content

Commit 668ed9c

Browse files
authored
Unrolled build for #157305
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 b3f7e32 + 876d5f3 commit 668ed9c

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
@@ -320,9 +320,8 @@ impl<'tcx> ResolverAstLowering<'tcx> {
320320
/// Relaxed bounds should only be allowed in places where we later
321321
/// (namely during HIR ty lowering) perform *sized elaboration*.
322322
#[derive(Clone, Copy, Debug)]
323-
enum RelaxedBoundPolicy<'a> {
323+
enum RelaxedBoundPolicy {
324324
Allowed,
325-
AllowedIfOnTyParam(NodeId, &'a [ast::GenericParam]),
326325
Forbidden(RelaxedBoundForbiddenReason),
327326
}
328327

@@ -332,7 +331,9 @@ enum RelaxedBoundForbiddenReason {
332331
SuperTrait,
333332
TraitAlias,
334333
AssocTyBounds,
335-
LateBoundVarsInScope,
334+
/// We do not allow where bounds doing relaxed bounds,
335+
/// except if it's for generic parameters of the current item.
336+
WhereBound,
336337
}
337338

338339
/// Context of `impl Trait` in code, which determines whether it is allowed in an HIR subtree,
@@ -1965,7 +1966,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
19651966
fn lower_param_bound(
19661967
&mut self,
19671968
tpb: &GenericBound,
1968-
rbp: RelaxedBoundPolicy<'_>,
1969+
rbp: RelaxedBoundPolicy,
19691970
itctx: ImplTraitContext,
19701971
) -> hir::GenericBound<'hir> {
19711972
match tpb {
@@ -2203,7 +2204,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
22032204
fn lower_poly_trait_ref(
22042205
&mut self,
22052206
PolyTraitRef { bound_generic_params, modifiers, trait_ref, span, parens: _ }: &PolyTraitRef,
2206-
rbp: RelaxedBoundPolicy<'_>,
2207+
rbp: RelaxedBoundPolicy,
22072208
itctx: ImplTraitContext,
22082209
) -> hir::PolyTraitRef<'hir> {
22092210
let bound_generic_params =
@@ -2227,7 +2228,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
22272228
&self,
22282229
trait_ref: hir::TraitRef<'_>,
22292230
span: Span,
2230-
rbp: RelaxedBoundPolicy<'_>,
2231+
rbp: RelaxedBoundPolicy,
22312232
) {
22322233
// Even though feature `more_maybe_bounds` enables the user to relax all default bounds
22332234
// other than `Sized` in a lot more positions (thereby bypassing the given policy), we don't
@@ -2240,14 +2241,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
22402241

22412242
match rbp {
22422243
RelaxedBoundPolicy::Allowed => return,
2243-
RelaxedBoundPolicy::AllowedIfOnTyParam(id, params) => {
2244-
if let Some(res) = self.get_partial_res(id).and_then(|r| r.full_res())
2245-
&& let Res::Def(DefKind::TyParam, def_id) = res
2246-
&& params.iter().any(|p| def_id == self.local_def_id(p.id).to_def_id())
2247-
{
2248-
return;
2249-
}
2250-
}
22512244
RelaxedBoundPolicy::Forbidden(reason) => {
22522245
let gate = |context, subject| {
22532246
let extended = self.tcx.features().more_maybe_bounds();
@@ -2287,7 +2280,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
22872280
return;
22882281
}
22892282
RelaxedBoundForbiddenReason::AssocTyBounds
2290-
| RelaxedBoundForbiddenReason::LateBoundVarsInScope => {}
2283+
| RelaxedBoundForbiddenReason::WhereBound => {}
22912284
};
22922285
}
22932286
}
@@ -2309,7 +2302,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
23092302
fn lower_param_bounds(
23102303
&mut self,
23112304
bounds: &[GenericBound],
2312-
rbp: RelaxedBoundPolicy<'_>,
2305+
rbp: RelaxedBoundPolicy,
23132306
itctx: ImplTraitContext,
23142307
) -> hir::GenericBounds<'hir> {
23152308
self.arena.alloc_from_iter(self.lower_param_bounds_mut(bounds, rbp, itctx))
@@ -2318,7 +2311,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
23182311
fn lower_param_bounds_mut(
23192312
&mut self,
23202313
bounds: &[GenericBound],
2321-
rbp: RelaxedBoundPolicy<'_>,
2314+
rbp: RelaxedBoundPolicy,
23222315
itctx: ImplTraitContext,
23232316
) -> impl Iterator<Item = hir::GenericBound<'hir>> {
23242317
bounds.iter().map(move |bound| self.lower_param_bound(bound, rbp, itctx))

0 commit comments

Comments
 (0)