Skip to content

[WIP] Support param bounds on non-lifetime binders #115362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1818,6 +1818,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
bounded_ty: self.arena.alloc(bounded_ty),
bounds,
bound_generic_params: &[],
bound_assumptions: &[],
origin,
})
}
Expand Down Expand Up @@ -1846,6 +1847,20 @@ impl<'hir> LoweringContext<'_, 'hir> {
}) => hir::WherePredicateKind::BoundPredicate(hir::WhereBoundPredicate {
bound_generic_params: self
.lower_generic_params(bound_generic_params, hir::GenericParamSource::Binder),
bound_assumptions: self.arena.alloc_from_iter(
bound_generic_params.iter().filter_map(|param| {
self.lower_generic_bound_predicate(
param.ident,
param.id,
&param.kind,
&param.bounds,
param.colon_span,
span,
ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
PredicateOrigin::GenericParam,
)
}),
),
bounded_ty: self
.lower_ty(bounded_ty, ImplTraitContext::Disallowed(ImplTraitPosition::Bound)),
bounds: self.lower_param_bounds(
Expand Down
18 changes: 17 additions & 1 deletion compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId};
use rustc_hir::{
self as hir, ConstArg, GenericArg, HirId, IsAnonInPath, ItemLocalMap, LangItem, ParamName,
TraitCandidate,
PredicateOrigin, TraitCandidate,
};
use rustc_index::{Idx, IndexSlice, IndexVec};
use rustc_macros::extension;
Expand Down Expand Up @@ -1728,6 +1728,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {

hir::GenericBound::Trait(hir::PolyTraitRef {
bound_generic_params: &[],
bound_assumptions: &[],
modifiers: hir::TraitBoundModifiers::NONE,
trait_ref: hir::TraitRef {
path: self.make_lang_item_path(trait_lang_item, opaque_ty_span, Some(bound_args)),
Expand Down Expand Up @@ -1948,12 +1949,26 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
p: &PolyTraitRef,
itctx: ImplTraitContext,
) -> hir::PolyTraitRef<'hir> {
let bound_assumptions =
self.arena.alloc_from_iter(p.bound_generic_params.iter().filter_map(|param| {
self.lower_generic_bound_predicate(
param.ident,
param.id,
&param.kind,
&param.bounds,
param.colon_span,
p.span,
ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
PredicateOrigin::GenericParam,
)
}));
let bound_generic_params =
self.lower_lifetime_binder(p.trait_ref.ref_id, &p.bound_generic_params);
let trait_ref = self.lower_trait_ref(p.modifiers, &p.trait_ref, itctx);
let modifiers = self.lower_trait_bound_modifiers(p.modifiers);
hir::PolyTraitRef {
bound_generic_params,
bound_assumptions,
modifiers,
trait_ref,
span: self.lower_span(p.span),
Expand Down Expand Up @@ -2362,6 +2377,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
Res::Def(DefKind::Trait | DefKind::TraitAlias, _) => {
let principal = hir::PolyTraitRef {
bound_generic_params: &[],
bound_assumptions: &[],
modifiers: hir::TraitBoundModifiers::NONE,
trait_ref: hir::TraitRef { path, hir_ref_id: hir_id },
span: self.lower_span(span),
Expand Down
10 changes: 6 additions & 4 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,12 @@ impl<'a> PostExpansionVisitor<'a> {
}
}

for param in params {
if !param.bounds.is_empty() {
let spans: Vec<_> = param.bounds.iter().map(|b| b.span()).collect();
self.sess.dcx().emit_err(errors::ForbiddenBound { spans });
if !self.features.non_lifetime_binders() {
for param in params {
if !param.bounds.is_empty() {
let spans: Vec<_> = param.bounds.iter().map(|b| b.span()).collect();
self.sess.dcx().emit_err(errors::ForbiddenBound { spans });
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_borrowck/src/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1641,7 +1641,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
let expected_ty = self.infcx.instantiate_binder_with_fresh_vars(
self.body().source_info(location).span,
BoundRegionConversionTime::HigherRankedType,
binder_ty.into(),
*binder_ty,
);
self.sub_types(
operand_ty,
Expand Down Expand Up @@ -1891,7 +1891,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
let found_ty = self.infcx.instantiate_binder_with_fresh_vars(
self.body.source_info(location).span,
BoundRegionConversionTime::HigherRankedType,
binder_ty.into(),
*binder_ty,
);
self.relate_types(
ty,
Expand Down
7 changes: 6 additions & 1 deletion compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,8 @@ pub struct WhereBoundPredicate<'hir> {
pub origin: PredicateOrigin,
/// Any generics from a `for` binding.
pub bound_generic_params: &'hir [GenericParam<'hir>],
/// The `'a + Trait` in `for<T: 'a + Trait> ...`
pub bound_assumptions: &'hir [WherePredicate<'hir>],
/// The type being bounded.
pub bounded_ty: &'hir Ty<'hir>,
/// Trait and lifetime bounds (e.g., `Clone + Send + 'static`).
Expand Down Expand Up @@ -3840,6 +3842,9 @@ pub struct PolyTraitRef<'hir> {
/// The `'a` in `for<'a> Foo<&'a T>`.
pub bound_generic_params: &'hir [GenericParam<'hir>],

/// The `'a + Trait` in `for<T: 'a + Trait> ...`
pub bound_assumptions: &'hir [WherePredicate<'hir>],

/// The constness and polarity of the trait ref.
///
/// The `async` modifier is lowered directly into a different trait for now.
Expand Down Expand Up @@ -4843,7 +4848,7 @@ mod size_asserts {
static_assert_size!(ForeignItem<'_>, 88);
static_assert_size!(ForeignItemKind<'_>, 56);
static_assert_size!(GenericArg<'_>, 16);
static_assert_size!(GenericBound<'_>, 64);
static_assert_size!(GenericBound<'_>, 80);
static_assert_size!(Generics<'_>, 56);
static_assert_size!(Impl<'_>, 80);
static_assert_size!(ImplItem<'_>, 88);
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_hir/src/intravisit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1082,10 +1082,12 @@ pub fn walk_where_predicate<'v, V: Visitor<'v>>(
ref bounded_ty,
bounds,
bound_generic_params,
bound_assumptions,
origin: _,
}) => {
try_visit!(visitor.visit_ty_unambig(bounded_ty));
walk_list!(visitor, visit_param_bound, bounds);
walk_list!(visitor, visit_where_predicate, bound_assumptions);
walk_list!(visitor, visit_generic_param, bound_generic_params);
}
WherePredicateKind::RegionPredicate(WhereRegionPredicate {
Expand Down Expand Up @@ -1296,6 +1298,7 @@ pub fn walk_poly_trait_ref<'v, V: Visitor<'v>>(
trait_ref: &'v PolyTraitRef<'v>,
) -> V::Result {
walk_list!(visitor, visit_generic_param, trait_ref.bound_generic_params);
walk_list!(visitor, visit_where_predicate, trait_ref.bound_assumptions);
visitor.visit_trait_ref(&trait_ref.trait_ref)
}

Expand Down
18 changes: 16 additions & 2 deletions compiler/rustc_hir_analysis/src/collect/item_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ fn associated_type_bounds<'tcx>(

let icx = ItemCtxt::new(tcx, assoc_item_def_id);
let mut bounds = Vec::new();
icx.lowerer().lower_bounds(item_ty, hir_bounds, &mut bounds, ty::List::empty(), filter);
icx.lowerer().lower_bounds(
item_ty,
hir_bounds,
&mut bounds,
ty::List::empty(),
ty::ListWithCachedTypeInfo::empty(),
filter,
);
// Associated types are implicitly sized unless a `?Sized` bound is found
match filter {
PredicateFilter::All
Expand Down Expand Up @@ -326,7 +333,14 @@ fn opaque_type_bounds<'tcx>(
ty::print::with_reduced_queries!({
let icx = ItemCtxt::new(tcx, opaque_def_id);
let mut bounds = Vec::new();
icx.lowerer().lower_bounds(item_ty, hir_bounds, &mut bounds, ty::List::empty(), filter);
icx.lowerer().lower_bounds(
item_ty,
hir_bounds,
&mut bounds,
ty::List::empty(),
ty::ListWithCachedTypeInfo::empty(),
filter,
);
// Opaque types are implicitly sized unless a `?Sized` bound is found
match filter {
PredicateFilter::All
Expand Down
Loading
Loading