Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1986,7 +1986,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
bounds: &[GenericBound],
colon_span: Option<Span>,
parent_span: Span,
rbp: RelaxedBoundPolicy<'_>,
rbp: RelaxedBoundPolicy,
itctx: ImplTraitContext,
origin: PredicateOrigin,
) -> Option<hir::WherePredicate<'hir>> {
Expand Down Expand Up @@ -2061,10 +2061,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
bounded_ty,
bounds,
}) => {
let rbp = if bound_generic_params.is_empty() {
RelaxedBoundPolicy::AllowedIfOnTyParam(bounded_ty.id, params)
let rbp = if bound_generic_params.is_empty()
&& let Some(res) =
self.get_partial_res(bounded_ty.id).and_then(|r| r.full_res())
&& let Res::Def(DefKind::TyParam, def_id) = res
&& params.iter().any(|p| def_id == self.local_def_id(p.id).to_def_id())
{
RelaxedBoundPolicy::Allowed
} else {
RelaxedBoundPolicy::Forbidden(RelaxedBoundForbiddenReason::LateBoundVarsInScope)
RelaxedBoundPolicy::Forbidden(RelaxedBoundForbiddenReason::WhereBound)
};
hir::WherePredicateKind::BoundPredicate(hir::WhereBoundPredicate {
bound_generic_params: self.lower_generic_params(
Expand Down
27 changes: 10 additions & 17 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,8 @@ impl<'tcx> ResolverAstLowering<'tcx> {
/// Relaxed bounds should only be allowed in places where we later
/// (namely during HIR ty lowering) perform *sized elaboration*.
#[derive(Clone, Copy, Debug)]
enum RelaxedBoundPolicy<'a> {
enum RelaxedBoundPolicy {
Allowed,
AllowedIfOnTyParam(NodeId, &'a [ast::GenericParam]),
Forbidden(RelaxedBoundForbiddenReason),
}

Expand All @@ -330,7 +329,9 @@ enum RelaxedBoundForbiddenReason {
SuperTrait,
TraitAlias,
AssocTyBounds,
LateBoundVarsInScope,
/// We do not allow where bounds doing relaxed bounds,
/// except if it's for generic parameters of the current item.
WhereBound,
}

/// Context of `impl Trait` in code, which determines whether it is allowed in an HIR subtree,
Expand Down Expand Up @@ -1955,7 +1956,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
fn lower_param_bound(
&mut self,
tpb: &GenericBound,
rbp: RelaxedBoundPolicy<'_>,
rbp: RelaxedBoundPolicy,
itctx: ImplTraitContext,
) -> hir::GenericBound<'hir> {
match tpb {
Expand Down Expand Up @@ -2193,7 +2194,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
fn lower_poly_trait_ref(
&mut self,
PolyTraitRef { bound_generic_params, modifiers, trait_ref, span, parens: _ }: &PolyTraitRef,
rbp: RelaxedBoundPolicy<'_>,
rbp: RelaxedBoundPolicy,
itctx: ImplTraitContext,
) -> hir::PolyTraitRef<'hir> {
let bound_generic_params =
Expand All @@ -2217,7 +2218,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
&self,
trait_ref: hir::TraitRef<'_>,
span: Span,
rbp: RelaxedBoundPolicy<'_>,
rbp: RelaxedBoundPolicy,
) {
// Even though feature `more_maybe_bounds` enables the user to relax all default bounds
// other than `Sized` in a lot more positions (thereby bypassing the given policy), we don't
Expand All @@ -2230,14 +2231,6 @@ impl<'hir> LoweringContext<'_, 'hir> {

match rbp {
RelaxedBoundPolicy::Allowed => return,
RelaxedBoundPolicy::AllowedIfOnTyParam(id, params) => {
if let Some(res) = self.get_partial_res(id).and_then(|r| r.full_res())
&& let Res::Def(DefKind::TyParam, def_id) = res
&& params.iter().any(|p| def_id == self.local_def_id(p.id).to_def_id())
{
return;
}
}
RelaxedBoundPolicy::Forbidden(reason) => {
let gate = |context, subject| {
let extended = self.tcx.features().more_maybe_bounds();
Expand Down Expand Up @@ -2277,7 +2270,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
return;
}
RelaxedBoundForbiddenReason::AssocTyBounds
| RelaxedBoundForbiddenReason::LateBoundVarsInScope => {}
| RelaxedBoundForbiddenReason::WhereBound => {}
};
}
}
Expand All @@ -2299,7 +2292,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
fn lower_param_bounds(
&mut self,
bounds: &[GenericBound],
rbp: RelaxedBoundPolicy<'_>,
rbp: RelaxedBoundPolicy,
itctx: ImplTraitContext,
) -> hir::GenericBounds<'hir> {
self.arena.alloc_from_iter(self.lower_param_bounds_mut(bounds, rbp, itctx))
Expand All @@ -2308,7 +2301,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
fn lower_param_bounds_mut(
&mut self,
bounds: &[GenericBound],
rbp: RelaxedBoundPolicy<'_>,
rbp: RelaxedBoundPolicy,
itctx: ImplTraitContext,
) -> impl Iterator<Item = hir::GenericBound<'hir>> {
bounds.iter().map(move |bound| self.lower_param_bound(bound, rbp, itctx))
Expand Down
Loading