Skip to content

Commit 665c4b9

Browse files
committed
AST lowering: More robustly deal with maybe-bounds
1 parent a119ed6 commit 665c4b9

14 files changed

+170
-198
lines changed

compiler/rustc_ast_lowering/messages.ftl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,6 @@ ast_lowering_misplaced_impl_trait =
127127
`impl Trait` is not allowed in {$position}
128128
.note = `impl Trait` is only allowed in arguments and return types of functions and methods
129129
130-
ast_lowering_misplaced_relax_trait_bound =
131-
`?Trait` bounds are only permitted at the point where a type parameter is declared
132-
133130
ast_lowering_never_pattern_with_body =
134131
a never pattern is always unreachable
135132
.label = this will never be executed

compiler/rustc_ast_lowering/src/errors.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -324,13 +324,6 @@ pub(crate) struct MisplacedDoubleDot {
324324
pub span: Span,
325325
}
326326

327-
#[derive(Diagnostic)]
328-
#[diag(ast_lowering_misplaced_relax_trait_bound)]
329-
pub(crate) struct MisplacedRelaxTraitBound {
330-
#[primary_span]
331-
pub span: Span,
332-
}
333-
334327
#[derive(Diagnostic)]
335328
#[diag(ast_lowering_match_arm_with_no_body)]
336329
pub(crate) struct MatchArmWithNoBody {

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 38 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,11 @@ use smallvec::{SmallVec, smallvec};
1515
use thin_vec::ThinVec;
1616
use tracing::instrument;
1717

18-
use super::errors::{
19-
InvalidAbi, InvalidAbiSuggestion, MisplacedRelaxTraitBound, TupleStructWithDefault,
20-
UnionWithDefault,
21-
};
18+
use super::errors::{InvalidAbi, InvalidAbiSuggestion, TupleStructWithDefault, UnionWithDefault};
2219
use super::stability::{enabled_names, gate_unstable_abi};
2320
use super::{
24-
AstOwner, FnDeclKind, ImplTraitContext, ImplTraitPosition, LoweringContext, ParamMode,
25-
ResolverAstLoweringExt,
21+
AstOwner, FnDeclKind, ImplTraitContext, ImplTraitPosition, LoweringContext,
22+
MaybeBoundForbiddenReason, MaybeBoundPolicy, ParamMode, ResolverAstLoweringExt,
2623
};
2724

2825
pub(super) struct ItemLowerer<'a, 'hir> {
@@ -427,6 +424,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
427424
|this| {
428425
let bounds = this.lower_param_bounds(
429426
bounds,
427+
MaybeBoundPolicy::Forbidden(MaybeBoundForbiddenReason::SuperTrait),
430428
ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
431429
);
432430
let items = this.arena.alloc_from_iter(
@@ -447,6 +445,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
447445
|this| {
448446
this.lower_param_bounds(
449447
bounds,
448+
MaybeBoundPolicy::Allowed,
450449
ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
451450
)
452451
},
@@ -938,6 +937,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
938937
hir::TraitItemKind::Type(
939938
this.lower_param_bounds(
940939
bounds,
940+
MaybeBoundPolicy::Allowed,
941941
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
942942
),
943943
ty,
@@ -1703,61 +1703,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
17031703
assert!(self.impl_trait_defs.is_empty());
17041704
assert!(self.impl_trait_bounds.is_empty());
17051705

1706-
// Error if `?Trait` bounds in where clauses don't refer directly to type parameters.
1707-
// Note: we used to clone these bounds directly onto the type parameter (and avoid lowering
1708-
// these into hir when we lower thee where clauses), but this makes it quite difficult to
1709-
// keep track of the Span info. Now, `<dyn HirTyLowerer>::add_implicit_sized_bound`
1710-
// checks both param bounds and where clauses for `?Sized`.
1711-
for pred in &generics.where_clause.predicates {
1712-
let WherePredicateKind::BoundPredicate(bound_pred) = &pred.kind else {
1713-
continue;
1714-
};
1715-
let compute_is_param = || {
1716-
// Check if the where clause type is a plain type parameter.
1717-
match self
1718-
.resolver
1719-
.get_partial_res(bound_pred.bounded_ty.id)
1720-
.and_then(|r| r.full_res())
1721-
{
1722-
Some(Res::Def(DefKind::TyParam, def_id))
1723-
if bound_pred.bound_generic_params.is_empty() =>
1724-
{
1725-
generics
1726-
.params
1727-
.iter()
1728-
.any(|p| def_id == self.local_def_id(p.id).to_def_id())
1729-
}
1730-
// Either the `bounded_ty` is not a plain type parameter, or
1731-
// it's not found in the generic type parameters list.
1732-
_ => false,
1733-
}
1734-
};
1735-
// We only need to compute this once per `WherePredicate`, but don't
1736-
// need to compute this at all unless there is a Maybe bound.
1737-
let mut is_param: Option<bool> = None;
1738-
for bound in &bound_pred.bounds {
1739-
if !matches!(
1740-
*bound,
1741-
GenericBound::Trait(PolyTraitRef {
1742-
modifiers: TraitBoundModifiers { polarity: BoundPolarity::Maybe(_), .. },
1743-
..
1744-
})
1745-
) {
1746-
continue;
1747-
}
1748-
let is_param = *is_param.get_or_insert_with(compute_is_param);
1749-
if !is_param && !self.tcx.features().more_maybe_bounds() {
1750-
self.tcx
1751-
.sess
1752-
.create_feature_err(
1753-
MisplacedRelaxTraitBound { span: bound.span() },
1754-
sym::more_maybe_bounds,
1755-
)
1756-
.emit();
1757-
}
1758-
}
1759-
}
1760-
17611706
let mut predicates: SmallVec<[hir::WherePredicate<'hir>; 4]> = SmallVec::new();
17621707
predicates.extend(generics.params.iter().filter_map(|param| {
17631708
self.lower_generic_bound_predicate(
@@ -1767,6 +1712,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
17671712
&param.bounds,
17681713
param.colon_span,
17691714
generics.span,
1715+
MaybeBoundPolicy::Allowed,
17701716
itctx,
17711717
PredicateOrigin::GenericParam,
17721718
)
@@ -1776,7 +1722,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
17761722
.where_clause
17771723
.predicates
17781724
.iter()
1779-
.map(|predicate| self.lower_where_predicate(predicate)),
1725+
.map(|predicate| self.lower_where_predicate(predicate, &generics.params)),
17801726
);
17811727

17821728
let mut params: SmallVec<[hir::GenericParam<'hir>; 4]> = self
@@ -1853,6 +1799,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
18531799
bounds: &[GenericBound],
18541800
colon_span: Option<Span>,
18551801
parent_span: Span,
1802+
mbp: MaybeBoundPolicy<'_>,
18561803
itctx: ImplTraitContext,
18571804
origin: PredicateOrigin,
18581805
) -> Option<hir::WherePredicate<'hir>> {
@@ -1861,7 +1808,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
18611808
return None;
18621809
}
18631810

1864-
let bounds = self.lower_param_bounds(bounds, itctx);
1811+
let bounds = self.lower_param_bounds(bounds, mbp, itctx);
18651812

18661813
let param_span = ident.span;
18671814

@@ -1913,7 +1860,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
19131860
Some(hir::WherePredicate { hir_id, span, kind })
19141861
}
19151862

1916-
fn lower_where_predicate(&mut self, pred: &WherePredicate) -> hir::WherePredicate<'hir> {
1863+
fn lower_where_predicate(
1864+
&mut self,
1865+
pred: &WherePredicate,
1866+
params: &[ast::GenericParam],
1867+
) -> hir::WherePredicate<'hir> {
19171868
let hir_id = self.lower_node_id(pred.id);
19181869
let span = self.lower_span(pred.span);
19191870
self.lower_attrs(hir_id, &pred.attrs, span);
@@ -1922,17 +1873,28 @@ impl<'hir> LoweringContext<'_, 'hir> {
19221873
bound_generic_params,
19231874
bounded_ty,
19241875
bounds,
1925-
}) => hir::WherePredicateKind::BoundPredicate(hir::WhereBoundPredicate {
1926-
bound_generic_params: self
1927-
.lower_generic_params(bound_generic_params, hir::GenericParamSource::Binder),
1928-
bounded_ty: self
1929-
.lower_ty(bounded_ty, ImplTraitContext::Disallowed(ImplTraitPosition::Bound)),
1930-
bounds: self.lower_param_bounds(
1931-
bounds,
1932-
ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
1933-
),
1934-
origin: PredicateOrigin::WhereClause,
1935-
}),
1876+
}) => {
1877+
let mbp = match bound_generic_params.is_empty() {
1878+
true => MaybeBoundPolicy::AllowedIfOwnTyParam(bounded_ty.id, params),
1879+
false => MaybeBoundPolicy::Forbidden(MaybeBoundForbiddenReason::Other),
1880+
};
1881+
hir::WherePredicateKind::BoundPredicate(hir::WhereBoundPredicate {
1882+
bound_generic_params: self.lower_generic_params(
1883+
bound_generic_params,
1884+
hir::GenericParamSource::Binder,
1885+
),
1886+
bounded_ty: self.lower_ty(
1887+
bounded_ty,
1888+
ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
1889+
),
1890+
bounds: self.lower_param_bounds(
1891+
bounds,
1892+
mbp,
1893+
ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
1894+
),
1895+
origin: PredicateOrigin::WhereClause,
1896+
})
1897+
}
19361898
WherePredicateKind::RegionPredicate(WhereRegionPredicate { lifetime, bounds }) => {
19371899
hir::WherePredicateKind::RegionPredicate(hir::WhereRegionPredicate {
19381900
lifetime: self.lower_lifetime(
@@ -1942,6 +1904,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
19421904
),
19431905
bounds: self.lower_param_bounds(
19441906
bounds,
1907+
MaybeBoundPolicy::Allowed,
19451908
ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
19461909
),
19471910
in_where_clause: true,

0 commit comments

Comments
 (0)