Skip to content

[WIP] unbound bettering #142693

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

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
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
3 changes: 0 additions & 3 deletions compiler/rustc_ast_lowering/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,6 @@ ast_lowering_misplaced_impl_trait =
`impl Trait` is not allowed in {$position}
.note = `impl Trait` is only allowed in arguments and return types of functions and methods

ast_lowering_misplaced_relax_trait_bound =
`?Trait` bounds are only permitted at the point where a type parameter is declared

ast_lowering_never_pattern_with_body =
a never pattern is always unreachable
.label = this will never be executed
Expand Down
7 changes: 0 additions & 7 deletions compiler/rustc_ast_lowering/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,13 +324,6 @@ pub(crate) struct MisplacedDoubleDot {
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(ast_lowering_misplaced_relax_trait_bound)]
pub(crate) struct MisplacedRelaxTraitBound {
#[primary_span]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(ast_lowering_match_arm_with_no_body)]
pub(crate) struct MatchArmWithNoBody {
Expand Down
113 changes: 38 additions & 75 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@ use smallvec::{SmallVec, smallvec};
use thin_vec::ThinVec;
use tracing::instrument;

use super::errors::{
InvalidAbi, InvalidAbiSuggestion, MisplacedRelaxTraitBound, TupleStructWithDefault,
UnionWithDefault,
};
use super::errors::{InvalidAbi, InvalidAbiSuggestion, TupleStructWithDefault, UnionWithDefault};
use super::stability::{enabled_names, gate_unstable_abi};
use super::{
AstOwner, FnDeclKind, ImplTraitContext, ImplTraitPosition, LoweringContext, ParamMode,
ResolverAstLoweringExt,
AstOwner, FnDeclKind, ImplTraitContext, ImplTraitPosition, LoweringContext,
MaybeBoundForbiddenReason, MaybeBoundPolicy, ParamMode, ResolverAstLoweringExt,
};

pub(super) struct ItemLowerer<'a, 'hir> {
Expand Down Expand Up @@ -427,6 +424,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
|this| {
let bounds = this.lower_param_bounds(
bounds,
MaybeBoundPolicy::Forbidden(MaybeBoundForbiddenReason::SuperTrait),
ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
);
let items = this.arena.alloc_from_iter(
Expand All @@ -447,6 +445,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
|this| {
this.lower_param_bounds(
bounds,
MaybeBoundPolicy::Allowed,
ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
)
},
Expand Down Expand Up @@ -938,6 +937,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
hir::TraitItemKind::Type(
this.lower_param_bounds(
bounds,
MaybeBoundPolicy::Allowed,
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
),
ty,
Expand Down Expand Up @@ -1703,61 +1703,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
assert!(self.impl_trait_defs.is_empty());
assert!(self.impl_trait_bounds.is_empty());

// Error if `?Trait` bounds in where clauses don't refer directly to type parameters.
// Note: we used to clone these bounds directly onto the type parameter (and avoid lowering
// these into hir when we lower thee where clauses), but this makes it quite difficult to
// keep track of the Span info. Now, `<dyn HirTyLowerer>::add_implicit_sized_bound`
// checks both param bounds and where clauses for `?Sized`.
for pred in &generics.where_clause.predicates {
let WherePredicateKind::BoundPredicate(bound_pred) = &pred.kind else {
continue;
};
let compute_is_param = || {
// Check if the where clause type is a plain type parameter.
match self
.resolver
.get_partial_res(bound_pred.bounded_ty.id)
.and_then(|r| r.full_res())
{
Some(Res::Def(DefKind::TyParam, def_id))
if bound_pred.bound_generic_params.is_empty() =>
{
generics
.params
.iter()
.any(|p| def_id == self.local_def_id(p.id).to_def_id())
}
// Either the `bounded_ty` is not a plain type parameter, or
// it's not found in the generic type parameters list.
_ => false,
}
};
// We only need to compute this once per `WherePredicate`, but don't
// need to compute this at all unless there is a Maybe bound.
let mut is_param: Option<bool> = None;
for bound in &bound_pred.bounds {
if !matches!(
*bound,
GenericBound::Trait(PolyTraitRef {
modifiers: TraitBoundModifiers { polarity: BoundPolarity::Maybe(_), .. },
..
})
) {
continue;
}
let is_param = *is_param.get_or_insert_with(compute_is_param);
if !is_param && !self.tcx.features().more_maybe_bounds() {
self.tcx
.sess
.create_feature_err(
MisplacedRelaxTraitBound { span: bound.span() },
sym::more_maybe_bounds,
)
.emit();
}
}
}

let mut predicates: SmallVec<[hir::WherePredicate<'hir>; 4]> = SmallVec::new();
predicates.extend(generics.params.iter().filter_map(|param| {
self.lower_generic_bound_predicate(
Expand All @@ -1767,6 +1712,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
&param.bounds,
param.colon_span,
generics.span,
MaybeBoundPolicy::Allowed,
itctx,
PredicateOrigin::GenericParam,
)
Expand All @@ -1776,7 +1722,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
.where_clause
.predicates
.iter()
.map(|predicate| self.lower_where_predicate(predicate)),
.map(|predicate| self.lower_where_predicate(predicate, &generics.params)),
);

let mut params: SmallVec<[hir::GenericParam<'hir>; 4]> = self
Expand Down Expand Up @@ -1853,6 +1799,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
bounds: &[GenericBound],
colon_span: Option<Span>,
parent_span: Span,
mbp: MaybeBoundPolicy<'_>,
itctx: ImplTraitContext,
origin: PredicateOrigin,
) -> Option<hir::WherePredicate<'hir>> {
Expand All @@ -1861,7 +1808,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
return None;
}

let bounds = self.lower_param_bounds(bounds, itctx);
let bounds = self.lower_param_bounds(bounds, mbp, itctx);

let param_span = ident.span;

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

fn lower_where_predicate(&mut self, pred: &WherePredicate) -> hir::WherePredicate<'hir> {
fn lower_where_predicate(
&mut self,
pred: &WherePredicate,
params: &[ast::GenericParam],
) -> hir::WherePredicate<'hir> {
let hir_id = self.lower_node_id(pred.id);
let span = self.lower_span(pred.span);
self.lower_attrs(hir_id, &pred.attrs, span);
Expand All @@ -1922,17 +1873,28 @@ impl<'hir> LoweringContext<'_, 'hir> {
bound_generic_params,
bounded_ty,
bounds,
}) => hir::WherePredicateKind::BoundPredicate(hir::WhereBoundPredicate {
bound_generic_params: self
.lower_generic_params(bound_generic_params, hir::GenericParamSource::Binder),
bounded_ty: self
.lower_ty(bounded_ty, ImplTraitContext::Disallowed(ImplTraitPosition::Bound)),
bounds: self.lower_param_bounds(
bounds,
ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
),
origin: PredicateOrigin::WhereClause,
}),
}) => {
let mbp = match bound_generic_params.is_empty() {
true => MaybeBoundPolicy::AllowedIfOwnTyParam(bounded_ty.id, params),
false => MaybeBoundPolicy::Forbidden(MaybeBoundForbiddenReason::Other),
};
hir::WherePredicateKind::BoundPredicate(hir::WhereBoundPredicate {
bound_generic_params: self.lower_generic_params(
bound_generic_params,
hir::GenericParamSource::Binder,
),
bounded_ty: self.lower_ty(
bounded_ty,
ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
),
bounds: self.lower_param_bounds(
bounds,
mbp,
ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
),
origin: PredicateOrigin::WhereClause,
})
}
WherePredicateKind::RegionPredicate(WhereRegionPredicate { lifetime, bounds }) => {
hir::WherePredicateKind::RegionPredicate(hir::WhereRegionPredicate {
lifetime: self.lower_lifetime(
Expand All @@ -1942,6 +1904,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
),
bounds: self.lower_param_bounds(
bounds,
MaybeBoundPolicy::Allowed,
ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
),
in_where_clause: true,
Expand Down
Loading
Loading