Skip to content

Commit ad43c2e

Browse files
Move trait bound modifiers into ast::PolyTraitRef
1 parent 1029c4d commit ad43c2e

File tree

17 files changed

+118
-103
lines changed

17 files changed

+118
-103
lines changed

Diff for: compiler/rustc_ast/src/ast.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ impl TraitBoundModifiers {
308308

309309
#[derive(Clone, Encodable, Decodable, Debug)]
310310
pub enum GenericBound {
311-
Trait(PolyTraitRef, TraitBoundModifiers),
311+
Trait(PolyTraitRef),
312312
Outlives(Lifetime),
313313
/// Precise capturing syntax: `impl Sized + use<'a>`
314314
Use(ThinVec<PreciseCapturingArg>, Span),
@@ -1213,10 +1213,12 @@ impl Expr {
12131213

12141214
pub fn to_bound(&self) -> Option<GenericBound> {
12151215
match &self.kind {
1216-
ExprKind::Path(None, path) => Some(GenericBound::Trait(
1217-
PolyTraitRef::new(ThinVec::new(), path.clone(), self.span),
1216+
ExprKind::Path(None, path) => Some(GenericBound::Trait(PolyTraitRef::new(
1217+
ThinVec::new(),
1218+
path.clone(),
12181219
TraitBoundModifiers::NONE,
1219-
)),
1220+
self.span,
1221+
))),
12201222
_ => None,
12211223
}
12221224
}
@@ -2965,16 +2967,25 @@ pub struct PolyTraitRef {
29652967
/// The `'a` in `for<'a> Foo<&'a T>`.
29662968
pub bound_generic_params: ThinVec<GenericParam>,
29672969

2970+
// Optional constness, asyncness, or polarity.
2971+
pub modifiers: TraitBoundModifiers,
2972+
29682973
/// The `Foo<&'a T>` in `<'a> Foo<&'a T>`.
29692974
pub trait_ref: TraitRef,
29702975

29712976
pub span: Span,
29722977
}
29732978

29742979
impl PolyTraitRef {
2975-
pub fn new(generic_params: ThinVec<GenericParam>, path: Path, span: Span) -> Self {
2980+
pub fn new(
2981+
generic_params: ThinVec<GenericParam>,
2982+
path: Path,
2983+
modifiers: TraitBoundModifiers,
2984+
span: Span,
2985+
) -> Self {
29762986
PolyTraitRef {
29772987
bound_generic_params: generic_params,
2988+
modifiers,
29782989
trait_ref: TraitRef { path, ref_id: DUMMY_NODE_ID },
29792990
span,
29802991
}

Diff for: compiler/rustc_ast/src/mut_visit.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,7 @@ fn walk_fn_ret_ty<T: MutVisitor>(vis: &mut T, fn_ret_ty: &mut FnRetTy) {
913913

914914
fn walk_param_bound<T: MutVisitor>(vis: &mut T, pb: &mut GenericBound) {
915915
match pb {
916-
GenericBound::Trait(ty, _modifier) => vis.visit_poly_trait_ref(ty),
916+
GenericBound::Trait(trait_ref) => vis.visit_poly_trait_ref(trait_ref),
917917
GenericBound::Outlives(lifetime) => walk_lifetime(vis, lifetime),
918918
GenericBound::Use(args, span) => {
919919
for arg in args {
@@ -1034,7 +1034,7 @@ fn walk_trait_ref<T: MutVisitor>(vis: &mut T, TraitRef { path, ref_id }: &mut Tr
10341034
}
10351035

10361036
fn walk_poly_trait_ref<T: MutVisitor>(vis: &mut T, p: &mut PolyTraitRef) {
1037-
let PolyTraitRef { bound_generic_params, trait_ref, span } = p;
1037+
let PolyTraitRef { bound_generic_params, modifiers: _, trait_ref, span } = p;
10381038
bound_generic_params.flat_map_in_place(|param| vis.flat_map_generic_param(param));
10391039
vis.visit_trait_ref(trait_ref);
10401040
vis.visit_span(span);

Diff for: compiler/rustc_ast/src/util/classify.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ fn type_trailing_braced_mac_call(mut ty: &ast::Ty) -> Option<&ast::MacCall> {
263263

264264
ast::TyKind::TraitObject(bounds, _) | ast::TyKind::ImplTrait(_, bounds) => {
265265
match bounds.last() {
266-
Some(ast::GenericBound::Trait(bound, _)) => {
266+
Some(ast::GenericBound::Trait(bound)) => {
267267
match path_return_type(&bound.trait_ref.path) {
268268
Some(trailing_ty) => ty = trailing_ty,
269269
None => break None,

Diff for: compiler/rustc_ast/src/visit.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ pub fn walk_poly_trait_ref<'a, V>(visitor: &mut V, trait_ref: &'a PolyTraitRef)
329329
where
330330
V: Visitor<'a>,
331331
{
332-
let PolyTraitRef { bound_generic_params, trait_ref, span: _ } = trait_ref;
332+
let PolyTraitRef { bound_generic_params, modifiers: _, trait_ref, span: _ } = trait_ref;
333333
walk_list!(visitor, visit_generic_param, bound_generic_params);
334334
visitor.visit_trait_ref(trait_ref)
335335
}
@@ -720,7 +720,7 @@ impl WalkItemKind for ForeignItemKind {
720720

721721
pub fn walk_param_bound<'a, V: Visitor<'a>>(visitor: &mut V, bound: &'a GenericBound) -> V::Result {
722722
match bound {
723-
GenericBound::Trait(typ, _modifier) => visitor.visit_poly_trait_ref(typ),
723+
GenericBound::Trait(trait_ref) => visitor.visit_poly_trait_ref(trait_ref),
724724
GenericBound::Outlives(lifetime) => visitor.visit_lifetime(lifetime, LifetimeCtxt::Bound),
725725
GenericBound::Use(args, _span) => {
726726
walk_list!(visitor, visit_precise_capturing_arg, args);

Diff for: compiler/rustc_ast_lowering/src/item.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1504,8 +1504,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
15041504
for bound in &bound_pred.bounds {
15051505
if !matches!(
15061506
*bound,
1507-
GenericBound::Trait(_, TraitBoundModifiers {
1508-
polarity: BoundPolarity::Maybe(_),
1507+
GenericBound::Trait(PolyTraitRef {
1508+
modifiers: TraitBoundModifiers { polarity: BoundPolarity::Maybe(_), .. },
15091509
..
15101510
})
15111511
) {

Diff for: compiler/rustc_ast_lowering/src/lib.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -1219,11 +1219,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12191219
let bound = this.lower_poly_trait_ref(
12201220
&PolyTraitRef {
12211221
bound_generic_params: ThinVec::new(),
1222+
modifiers: TraitBoundModifiers::NONE,
12221223
trait_ref: TraitRef { path: path.clone(), ref_id: t.id },
12231224
span: t.span,
12241225
},
12251226
itctx,
1226-
TraitBoundModifiers::NONE,
12271227
);
12281228
let bounds = this.arena.alloc_from_iter([bound]);
12291229
let lifetime_bound = this.elided_dyn_bound(t.span);
@@ -1325,8 +1325,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13251325
// We can safely ignore constness here since AST validation
13261326
// takes care of rejecting invalid modifier combinations and
13271327
// const trait bounds in trait object types.
1328-
GenericBound::Trait(ty, modifiers) => {
1329-
let trait_ref = this.lower_poly_trait_ref(ty, itctx, *modifiers);
1328+
GenericBound::Trait(ty) => {
1329+
let trait_ref = this.lower_poly_trait_ref(ty, itctx);
13301330
Some(trait_ref)
13311331
}
13321332
GenericBound::Outlives(lifetime) => {
@@ -1974,9 +1974,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19741974
itctx: ImplTraitContext,
19751975
) -> hir::GenericBound<'hir> {
19761976
match tpb {
1977-
GenericBound::Trait(p, modifiers) => {
1978-
hir::GenericBound::Trait(self.lower_poly_trait_ref(p, itctx, *modifiers))
1979-
}
1977+
GenericBound::Trait(p) => hir::GenericBound::Trait(self.lower_poly_trait_ref(p, itctx)),
19801978
GenericBound::Outlives(lifetime) => {
19811979
hir::GenericBound::Outlives(self.lower_lifetime(lifetime))
19821980
}
@@ -2180,12 +2178,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21802178
&mut self,
21812179
p: &PolyTraitRef,
21822180
itctx: ImplTraitContext,
2183-
modifiers: ast::TraitBoundModifiers,
21842181
) -> hir::PolyTraitRef<'hir> {
21852182
let bound_generic_params =
21862183
self.lower_lifetime_binder(p.trait_ref.ref_id, &p.bound_generic_params);
2187-
let trait_ref = self.lower_trait_ref(modifiers, &p.trait_ref, itctx);
2188-
let modifiers = self.lower_trait_bound_modifiers(modifiers);
2184+
let trait_ref = self.lower_trait_ref(p.modifiers, &p.trait_ref, itctx);
2185+
let modifiers = self.lower_trait_bound_modifiers(p.modifiers);
21892186
hir::PolyTraitRef {
21902187
bound_generic_params,
21912188
modifiers,

Diff for: compiler/rustc_ast_passes/src/ast_validation.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -1260,7 +1260,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
12601260
if !bound_pred.bound_generic_params.is_empty() {
12611261
for bound in &bound_pred.bounds {
12621262
match bound {
1263-
GenericBound::Trait(t, _) => {
1263+
GenericBound::Trait(t) => {
12641264
if !t.bound_generic_params.is_empty() {
12651265
self.dcx()
12661266
.emit_err(errors::NestedLifetimes { span: t.span });
@@ -1280,8 +1280,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
12801280

12811281
fn visit_param_bound(&mut self, bound: &'a GenericBound, ctxt: BoundKind) {
12821282
match bound {
1283-
GenericBound::Trait(trait_ref, modifiers) => {
1284-
match (ctxt, modifiers.constness, modifiers.polarity) {
1283+
GenericBound::Trait(trait_ref) => {
1284+
match (ctxt, trait_ref.modifiers.constness, trait_ref.modifiers.polarity) {
12851285
(BoundKind::SuperTraits, BoundConstness::Never, BoundPolarity::Maybe(_))
12861286
if !self.features.more_maybe_bounds =>
12871287
{
@@ -1321,7 +1321,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
13211321
}
13221322

13231323
// Negative trait bounds are not allowed to have associated constraints
1324-
if let BoundPolarity::Negative(_) = modifiers.polarity
1324+
if let BoundPolarity::Negative(_) = trait_ref.modifiers.polarity
13251325
&& let Some(segment) = trait_ref.trait_ref.path.segments.last()
13261326
{
13271327
match segment.args.as_deref() {
@@ -1669,7 +1669,9 @@ fn deny_equality_constraints(
16691669
}),
16701670
) {
16711671
for bound in bounds {
1672-
if let GenericBound::Trait(poly, TraitBoundModifiers::NONE) = bound {
1672+
if let GenericBound::Trait(poly) = bound
1673+
&& poly.modifiers == TraitBoundModifiers::NONE
1674+
{
16731675
if full_path.segments[..full_path.segments.len() - 1]
16741676
.iter()
16751677
.map(|segment| segment.ident.name)
@@ -1697,7 +1699,9 @@ fn deny_equality_constraints(
16971699
) {
16981700
if ident == potential_param.ident {
16991701
for bound in bounds {
1700-
if let ast::GenericBound::Trait(poly, TraitBoundModifiers::NONE) = bound {
1702+
if let ast::GenericBound::Trait(poly) = bound
1703+
&& poly.modifiers == TraitBoundModifiers::NONE
1704+
{
17011705
suggest(poly, potential_assoc, predicate);
17021706
}
17031707
}

Diff for: compiler/rustc_ast_pretty/src/pprust/state.rs

+22-26
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ mod item;
88

99
use std::borrow::Cow;
1010

11-
use ast::TraitBoundModifiers;
1211
use rustc_ast::attr::AttrIdGenerator;
1312
use rustc_ast::ptr::P;
1413
use rustc_ast::token::{
@@ -1253,6 +1252,27 @@ impl<'a> State<'a> {
12531252

12541253
fn print_poly_trait_ref(&mut self, t: &ast::PolyTraitRef) {
12551254
self.print_formal_generic_params(&t.bound_generic_params);
1255+
1256+
let ast::TraitBoundModifiers { constness, asyncness, polarity } = t.modifiers;
1257+
match constness {
1258+
ast::BoundConstness::Never => {}
1259+
ast::BoundConstness::Always(_) | ast::BoundConstness::Maybe(_) => {
1260+
self.word_space(constness.as_str());
1261+
}
1262+
}
1263+
match asyncness {
1264+
ast::BoundAsyncness::Normal => {}
1265+
ast::BoundAsyncness::Async(_) => {
1266+
self.word_space(asyncness.as_str());
1267+
}
1268+
}
1269+
match polarity {
1270+
ast::BoundPolarity::Positive => {}
1271+
ast::BoundPolarity::Negative(_) | ast::BoundPolarity::Maybe(_) => {
1272+
self.word(polarity.as_str());
1273+
}
1274+
}
1275+
12561276
self.print_trait_ref(&t.trait_ref)
12571277
}
12581278

@@ -1740,31 +1760,7 @@ impl<'a> State<'a> {
17401760
}
17411761

17421762
match bound {
1743-
GenericBound::Trait(
1744-
tref,
1745-
TraitBoundModifiers { constness, asyncness, polarity },
1746-
) => {
1747-
match constness {
1748-
ast::BoundConstness::Never => {}
1749-
ast::BoundConstness::Always(_) | ast::BoundConstness::Maybe(_) => {
1750-
self.word_space(constness.as_str());
1751-
}
1752-
}
1753-
1754-
match asyncness {
1755-
ast::BoundAsyncness::Normal => {}
1756-
ast::BoundAsyncness::Async(_) => {
1757-
self.word_space(asyncness.as_str());
1758-
}
1759-
}
1760-
1761-
match polarity {
1762-
ast::BoundPolarity::Positive => {}
1763-
ast::BoundPolarity::Negative(_) | ast::BoundPolarity::Maybe(_) => {
1764-
self.word(polarity.as_str());
1765-
}
1766-
}
1767-
1763+
GenericBound::Trait(tref) => {
17681764
self.print_poly_trait_ref(tref);
17691765
}
17701766
GenericBound::Outlives(lt) => self.print_lifetime(*lt),

Diff for: compiler/rustc_builtin_macros/src/deriving/smart_ptr.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -333,12 +333,12 @@ fn contains_maybe_sized_bound_on_pointee(predicates: &[WherePredicate], pointee:
333333
}
334334

335335
fn is_maybe_sized_bound(bound: &GenericBound) -> bool {
336-
if let GenericBound::Trait(
337-
trait_ref,
338-
TraitBoundModifiers { polarity: ast::BoundPolarity::Maybe(_), .. },
339-
) = bound
336+
if let GenericBound::Trait(trait_ref) = bound
337+
&& let TraitBoundModifiers { polarity: ast::BoundPolarity::Maybe(_), .. } =
338+
trait_ref.modifiers
339+
&& is_sized_marker(&trait_ref.trait_ref.path)
340340
{
341-
is_sized_marker(&trait_ref.trait_ref.path)
341+
true
342342
} else {
343343
false
344344
}

Diff for: compiler/rustc_expand/src/build.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -143,24 +143,25 @@ impl<'a> ExtCtxt<'a> {
143143
ast::TraitRef { path, ref_id: ast::DUMMY_NODE_ID }
144144
}
145145

146-
pub fn poly_trait_ref(&self, span: Span, path: ast::Path) -> ast::PolyTraitRef {
146+
pub fn poly_trait_ref(&self, span: Span, path: ast::Path, is_const: bool) -> ast::PolyTraitRef {
147147
ast::PolyTraitRef {
148148
bound_generic_params: ThinVec::new(),
149+
modifiers: ast::TraitBoundModifiers {
150+
polarity: ast::BoundPolarity::Positive,
151+
constness: if is_const {
152+
ast::BoundConstness::Maybe(DUMMY_SP)
153+
} else {
154+
ast::BoundConstness::Never
155+
},
156+
asyncness: ast::BoundAsyncness::Normal,
157+
},
149158
trait_ref: self.trait_ref(path),
150159
span,
151160
}
152161
}
153162

154163
pub fn trait_bound(&self, path: ast::Path, is_const: bool) -> ast::GenericBound {
155-
ast::GenericBound::Trait(self.poly_trait_ref(path.span, path), ast::TraitBoundModifiers {
156-
polarity: ast::BoundPolarity::Positive,
157-
constness: if is_const {
158-
ast::BoundConstness::Maybe(DUMMY_SP)
159-
} else {
160-
ast::BoundConstness::Never
161-
},
162-
asyncness: ast::BoundAsyncness::Normal,
163-
})
164+
ast::GenericBound::Trait(self.poly_trait_ref(path.span, path, is_const))
164165
}
165166

166167
pub fn lifetime(&self, span: Span, ident: Ident) -> ast::Lifetime {

Diff for: compiler/rustc_parse/src/parser/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2113,7 +2113,7 @@ impl<'a> Parser<'a> {
21132113
&& let Some(poly) = bounds
21142114
.iter()
21152115
.filter_map(|bound| match bound {
2116-
ast::GenericBound::Trait(poly, _) => Some(poly),
2116+
ast::GenericBound::Trait(poly) => Some(poly),
21172117
_ => None,
21182118
})
21192119
.last()

Diff for: compiler/rustc_parse/src/parser/path.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -948,8 +948,8 @@ impl<'a> Parser<'a> {
948948
{
949949
return Ok((false, seg.ident, seg.args.as_deref().cloned()));
950950
} else if let ast::TyKind::TraitObject(bounds, ast::TraitObjectSyntax::None) = &ty.kind
951-
&& let [ast::GenericBound::Trait(trait_ref, ast::TraitBoundModifiers::NONE)] =
952-
bounds.as_slice()
951+
&& let [ast::GenericBound::Trait(trait_ref)] = bounds.as_slice()
952+
&& trait_ref.modifiers == ast::TraitBoundModifiers::NONE
953953
&& let [seg] = trait_ref.trait_ref.path.segments.as_slice()
954954
{
955955
return Ok((true, seg.ident, seg.args.as_deref().cloned()));

Diff for: compiler/rustc_parse/src/parser/ty.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,13 @@ impl<'a> Parser<'a> {
419419
lo: Span,
420420
parse_plus: bool,
421421
) -> PResult<'a, TyKind> {
422-
let poly_trait_ref = PolyTraitRef::new(generic_params, path, lo.to(self.prev_token.span));
423-
let bounds = vec![GenericBound::Trait(poly_trait_ref, TraitBoundModifiers::NONE)];
422+
let poly_trait_ref = PolyTraitRef::new(
423+
generic_params,
424+
path,
425+
TraitBoundModifiers::NONE,
426+
lo.to(self.prev_token.span),
427+
);
428+
let bounds = vec![GenericBound::Trait(poly_trait_ref)];
424429
self.parse_remaining_bounds(bounds, parse_plus)
425430
}
426431

@@ -1085,8 +1090,9 @@ impl<'a> Parser<'a> {
10851090
}
10861091
}
10871092

1088-
let poly_trait = PolyTraitRef::new(lifetime_defs, path, lo.to(self.prev_token.span));
1089-
Ok(GenericBound::Trait(poly_trait, modifiers))
1093+
let poly_trait =
1094+
PolyTraitRef::new(lifetime_defs, path, modifiers, lo.to(self.prev_token.span));
1095+
Ok(GenericBound::Trait(poly_trait))
10901096
}
10911097

10921098
// recovers a `Fn(..)` parenthesized-style path from `fn(..)`

0 commit comments

Comments
 (0)