Skip to content

Commit bfa10de

Browse files
Auto merge of #142693 - fmease:unbound-bettering, r=<try>
More robustly deal with relaxed bounds and improve their diagnostics Scaffolding for #135229 (CC #135331) Fixes #136944. Fixes #142718.
2 parents 9c4ff56 + 04dad4e commit bfa10de

File tree

62 files changed

+680
-655
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+680
-655
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 & 74 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::{
2421
AstOwner, FnDeclKind, ImplTraitContext, ImplTraitPosition, LoweringContext, ParamMode,
25-
ResolverAstLoweringExt,
22+
RelaxedBoundForbiddenReason, RelaxedBoundPolicy, 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+
RelaxedBoundPolicy::Forbidden(RelaxedBoundForbiddenReason::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+
RelaxedBoundPolicy::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+
RelaxedBoundPolicy::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+
RelaxedBoundPolicy::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+
rbp: RelaxedBoundPolicy<'_>,
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, rbp, 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,29 @@ 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 rbp = if bound_generic_params.is_empty() {
1878+
RelaxedBoundPolicy::AllowedIfOnTyParam(bounded_ty.id, params)
1879+
} else {
1880+
RelaxedBoundPolicy::Forbidden(RelaxedBoundForbiddenReason::LateBoundVarsInScope)
1881+
};
1882+
hir::WherePredicateKind::BoundPredicate(hir::WhereBoundPredicate {
1883+
bound_generic_params: self.lower_generic_params(
1884+
bound_generic_params,
1885+
hir::GenericParamSource::Binder,
1886+
),
1887+
bounded_ty: self.lower_ty(
1888+
bounded_ty,
1889+
ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
1890+
),
1891+
bounds: self.lower_param_bounds(
1892+
bounds,
1893+
rbp,
1894+
ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
1895+
),
1896+
origin: PredicateOrigin::WhereClause,
1897+
})
1898+
}
19361899
WherePredicateKind::RegionPredicate(WhereRegionPredicate { lifetime, bounds }) => {
19371900
hir::WherePredicateKind::RegionPredicate(hir::WhereRegionPredicate {
19381901
lifetime: self.lower_lifetime(
@@ -1942,6 +1905,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
19421905
),
19431906
bounds: self.lower_param_bounds(
19441907
bounds,
1908+
RelaxedBoundPolicy::Allowed,
19451909
ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
19461910
),
19471911
in_where_clause: true,

0 commit comments

Comments
 (0)