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
18 changes: 17 additions & 1 deletion compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1985,6 +1985,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
bounded_ty: self.arena.alloc(bounded_ty),
bounds,
bound_generic_params: &[],
bound_assumptions: &[],
origin,
})
}
Expand Down Expand Up @@ -2017,7 +2018,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
bounded_ty,
bounds,
}) => {
let rbp = if bound_generic_params.is_empty()
let mut 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
Expand All @@ -2032,6 +2033,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
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,
rbp.reborrow(),
ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
PredicateOrigin::GenericParam,
)
}),
),
bounded_ty: self.lower_ty_alloc(
bounded_ty,
ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
Expand Down
22 changes: 20 additions & 2 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ use rustc_hir::definitions::PerParentDisambiguatorState;
use rustc_hir::lints::DelayedLint;
use rustc_hir::{
self as hir, AngleBrackets, ConstArg, GenericArg, HirId, ItemLocalMap, LifetimeSource,
LifetimeSyntax, MissingLifetimeKind, ParamName, Target, TraitCandidate, find_attr,
LifetimeSyntax, MissingLifetimeKind, ParamName, PredicateOrigin, Target, TraitCandidate,
find_attr,
};
use rustc_index::{Idx, IndexVec};
use rustc_macros::extension;
Expand Down Expand Up @@ -2076,6 +2077,7 @@ impl<'hir> LoweringContext<'_, '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 @@ -2328,9 +2330,23 @@ impl<'hir> LoweringContext<'_, 'hir> {
fn lower_poly_trait_ref(
&mut self,
PolyTraitRef { bound_generic_params, modifiers, trait_ref, span, parens: _ }: &PolyTraitRef,
rbp: RelaxedBoundPolicy<'_>,
mut rbp: RelaxedBoundPolicy<'_>,
itctx: ImplTraitContext,
) -> hir::PolyTraitRef<'hir> {
let 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,
rbp.reborrow(),
ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
PredicateOrigin::GenericParam,
)
}));
let bound_generic_params =
self.lower_lifetime_binder(trait_ref.ref_id, bound_generic_params);
let trait_ref = self.lower_trait_ref(*modifiers, trait_ref, itctx);
Expand All @@ -2342,6 +2358,7 @@ impl<'hir> LoweringContext<'_, 'hir> {

hir::PolyTraitRef {
bound_generic_params,
bound_assumptions,
modifiers,
trait_ref,
span: self.lower_span(*span),
Expand Down Expand Up @@ -3102,6 +3119,7 @@ impl<'hir> LoweringContext<'_, '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
22 changes: 12 additions & 10 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,18 @@ impl<'a> PostExpansionVisitor<'a> {
}
}

for param in params {
if !param.bounds.is_empty() {
let spans: Vec<_> = param.bounds.iter().map(|b| b.span()).collect();
if param.bounds.iter().any(|bound| matches!(bound, GenericBound::Trait(_))) {
// Issue #149695
// Abort immediately otherwise items defined in complex bounds will be lowered into HIR,
// which will cause ICEs when errors of the items visit unlowered parents.
self.sess.dcx().emit_fatal(diagnostics::ForbiddenBound { spans });
} else {
self.sess.dcx().emit_err(diagnostics::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();
if param.bounds.iter().any(|bound| matches!(bound, GenericBound::Trait(_))) {
// Issue #149695
// Abort immediately otherwise items defined in complex bounds will be lowered into HIR,
// which will cause ICEs when errors of the items visit unlowered parents.
self.sess.dcx().emit_fatal(diagnostics::ForbiddenBound { spans });
} else {
self.sess.dcx().emit_err(diagnostics::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 @@ -1679,7 +1679,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 @@ -1924,7 +1924,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
2 changes: 1 addition & 1 deletion compiler/rustc_const_eval/src/const_eval/type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> {
pub(crate) fn write_fn_ptr_type_info(
&mut self,
place: impl Writeable<'tcx, CtfeProvenance>,
sig: &FnSigTys<TyCtxt<'tcx>>,
sig: &FnSigTys<'tcx>,
fn_header: &FnHeader<TyCtxt<'tcx>>,
) -> InterpResult<'tcx> {
let FnHeader { fn_sig_kind } = fn_header;
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 @@ -1227,6 +1227,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 @@ -4447,6 +4449,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 @@ -5470,7 +5475,7 @@ mod size_asserts {
static_assert_size!(ForeignItem<'_>, 96);
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<'_>, 48);
static_assert_size!(ImplItem<'_>, 88);
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_hir/src/intravisit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1186,10 +1186,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 @@ -1392,8 +1394,10 @@ pub fn walk_poly_trait_ref<'v, V: Visitor<'v>>(
visitor: &mut V,
trait_ref: &'v PolyTraitRef<'v>,
) -> V::Result {
let PolyTraitRef { bound_generic_params, modifiers: _, trait_ref, span: _ } = trait_ref;
let PolyTraitRef { bound_generic_params, bound_assumptions, modifiers: _, trait_ref, span: _ } =
trait_ref;
walk_list!(visitor, visit_generic_param, *bound_generic_params);
walk_list!(visitor, visit_where_predicate, *bound_assumptions);
visitor.visit_trait_ref(trait_ref)
}

Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_hir_analysis/src/collect/item_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ fn associated_type_bounds<'tcx>(
hir_bounds,
&mut bounds,
ty::List::empty(),
ty::ListWithCachedTypeInfo::empty(),
filter,
OverlappingAsssocItemConstraints::Allowed,
);
Expand Down Expand Up @@ -373,6 +374,7 @@ fn opaque_type_bounds<'tcx>(
hir_bounds,
&mut bounds,
ty::List::empty(),
ty::ListWithCachedTypeInfo::empty(),
filter,
OverlappingAsssocItemConstraints::Allowed,
);
Expand Down
Loading
Loading