Skip to content

Commit 6893bb5

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 7ba34c7 + c74e11c commit 6893bb5

File tree

62 files changed

+738
-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

+738
-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,
@@ -1723,61 +1723,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
17231723
assert!(self.impl_trait_defs.is_empty());
17241724
assert!(self.impl_trait_bounds.is_empty());
17251725

1726-
// Error if `?Trait` bounds in where clauses don't refer directly to type parameters.
1727-
// Note: we used to clone these bounds directly onto the type parameter (and avoid lowering
1728-
// these into hir when we lower thee where clauses), but this makes it quite difficult to
1729-
// keep track of the Span info. Now, `<dyn HirTyLowerer>::add_implicit_sized_bound`
1730-
// checks both param bounds and where clauses for `?Sized`.
1731-
for pred in &generics.where_clause.predicates {
1732-
let WherePredicateKind::BoundPredicate(bound_pred) = &pred.kind else {
1733-
continue;
1734-
};
1735-
let compute_is_param = || {
1736-
// Check if the where clause type is a plain type parameter.
1737-
match self
1738-
.resolver
1739-
.get_partial_res(bound_pred.bounded_ty.id)
1740-
.and_then(|r| r.full_res())
1741-
{
1742-
Some(Res::Def(DefKind::TyParam, def_id))
1743-
if bound_pred.bound_generic_params.is_empty() =>
1744-
{
1745-
generics
1746-
.params
1747-
.iter()
1748-
.any(|p| def_id == self.local_def_id(p.id).to_def_id())
1749-
}
1750-
// Either the `bounded_ty` is not a plain type parameter, or
1751-
// it's not found in the generic type parameters list.
1752-
_ => false,
1753-
}
1754-
};
1755-
// We only need to compute this once per `WherePredicate`, but don't
1756-
// need to compute this at all unless there is a Maybe bound.
1757-
let mut is_param: Option<bool> = None;
1758-
for bound in &bound_pred.bounds {
1759-
if !matches!(
1760-
*bound,
1761-
GenericBound::Trait(PolyTraitRef {
1762-
modifiers: TraitBoundModifiers { polarity: BoundPolarity::Maybe(_), .. },
1763-
..
1764-
})
1765-
) {
1766-
continue;
1767-
}
1768-
let is_param = *is_param.get_or_insert_with(compute_is_param);
1769-
if !is_param && !self.tcx.features().more_maybe_bounds() {
1770-
self.tcx
1771-
.sess
1772-
.create_feature_err(
1773-
MisplacedRelaxTraitBound { span: bound.span() },
1774-
sym::more_maybe_bounds,
1775-
)
1776-
.emit();
1777-
}
1778-
}
1779-
}
1780-
17811726
let mut predicates: SmallVec<[hir::WherePredicate<'hir>; 4]> = SmallVec::new();
17821727
predicates.extend(generics.params.iter().filter_map(|param| {
17831728
self.lower_generic_bound_predicate(
@@ -1787,6 +1732,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
17871732
&param.bounds,
17881733
param.colon_span,
17891734
generics.span,
1735+
RelaxedBoundPolicy::Allowed,
17901736
itctx,
17911737
PredicateOrigin::GenericParam,
17921738
)
@@ -1796,7 +1742,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
17961742
.where_clause
17971743
.predicates
17981744
.iter()
1799-
.map(|predicate| self.lower_where_predicate(predicate)),
1745+
.map(|predicate| self.lower_where_predicate(predicate, &generics.params)),
18001746
);
18011747

18021748
let mut params: SmallVec<[hir::GenericParam<'hir>; 4]> = self
@@ -1873,6 +1819,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
18731819
bounds: &[GenericBound],
18741820
colon_span: Option<Span>,
18751821
parent_span: Span,
1822+
rbp: RelaxedBoundPolicy<'_>,
18761823
itctx: ImplTraitContext,
18771824
origin: PredicateOrigin,
18781825
) -> Option<hir::WherePredicate<'hir>> {
@@ -1881,7 +1828,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
18811828
return None;
18821829
}
18831830

1884-
let bounds = self.lower_param_bounds(bounds, itctx);
1831+
let bounds = self.lower_param_bounds(bounds, rbp, itctx);
18851832

18861833
let param_span = ident.span;
18871834

@@ -1933,7 +1880,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
19331880
Some(hir::WherePredicate { hir_id, span, kind })
19341881
}
19351882

1936-
fn lower_where_predicate(&mut self, pred: &WherePredicate) -> hir::WherePredicate<'hir> {
1883+
fn lower_where_predicate(
1884+
&mut self,
1885+
pred: &WherePredicate,
1886+
params: &[ast::GenericParam],
1887+
) -> hir::WherePredicate<'hir> {
19371888
let hir_id = self.lower_node_id(pred.id);
19381889
let span = self.lower_span(pred.span);
19391890
self.lower_attrs(hir_id, &pred.attrs, span);
@@ -1942,17 +1893,29 @@ impl<'hir> LoweringContext<'_, 'hir> {
19421893
bound_generic_params,
19431894
bounded_ty,
19441895
bounds,
1945-
}) => hir::WherePredicateKind::BoundPredicate(hir::WhereBoundPredicate {
1946-
bound_generic_params: self
1947-
.lower_generic_params(bound_generic_params, hir::GenericParamSource::Binder),
1948-
bounded_ty: self
1949-
.lower_ty(bounded_ty, ImplTraitContext::Disallowed(ImplTraitPosition::Bound)),
1950-
bounds: self.lower_param_bounds(
1951-
bounds,
1952-
ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
1953-
),
1954-
origin: PredicateOrigin::WhereClause,
1955-
}),
1896+
}) => {
1897+
let rbp = if bound_generic_params.is_empty() {
1898+
RelaxedBoundPolicy::AllowedIfOnTyParam(bounded_ty.id, params)
1899+
} else {
1900+
RelaxedBoundPolicy::Forbidden(RelaxedBoundForbiddenReason::LateBoundVarsInScope)
1901+
};
1902+
hir::WherePredicateKind::BoundPredicate(hir::WhereBoundPredicate {
1903+
bound_generic_params: self.lower_generic_params(
1904+
bound_generic_params,
1905+
hir::GenericParamSource::Binder,
1906+
),
1907+
bounded_ty: self.lower_ty(
1908+
bounded_ty,
1909+
ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
1910+
),
1911+
bounds: self.lower_param_bounds(
1912+
bounds,
1913+
rbp,
1914+
ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
1915+
),
1916+
origin: PredicateOrigin::WhereClause,
1917+
})
1918+
}
19561919
WherePredicateKind::RegionPredicate(WhereRegionPredicate { lifetime, bounds }) => {
19571920
hir::WherePredicateKind::RegionPredicate(hir::WhereRegionPredicate {
19581921
lifetime: self.lower_lifetime(
@@ -1962,6 +1925,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
19621925
),
19631926
bounds: self.lower_param_bounds(
19641927
bounds,
1928+
RelaxedBoundPolicy::Allowed,
19651929
ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
19661930
),
19671931
in_where_clause: true,

0 commit comments

Comments
 (0)