Skip to content

Commit fb57b75

Browse files
committed
Add term
Instead of having a separate enum variant for types and consts have one but have either a const or type.
1 parent 0765999 commit fb57b75

File tree

11 files changed

+68
-37
lines changed

11 files changed

+68
-37
lines changed

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

+21-4
Original file line numberDiff line numberDiff line change
@@ -1851,13 +1851,30 @@ pub struct AssocConstraint {
18511851
pub span: Span,
18521852
}
18531853

1854+
/// The kinds of an `AssocConstraint`.
1855+
#[derive(Clone, Encodable, Decodable, Debug)]
1856+
pub enum Term {
1857+
Ty(P<Ty>),
1858+
Const(AnonConst),
1859+
}
1860+
1861+
impl From<P<Ty>> for Term {
1862+
fn from(v: P<Ty>) -> Self {
1863+
Term::Ty(v)
1864+
}
1865+
}
1866+
1867+
impl From<AnonConst> for Term {
1868+
fn from(v: AnonConst) -> Self {
1869+
Term::Const(v)
1870+
}
1871+
}
1872+
18541873
/// The kinds of an `AssocConstraint`.
18551874
#[derive(Clone, Encodable, Decodable, Debug)]
18561875
pub enum AssocConstraintKind {
1857-
/// E.g., `A = Bar` in `Foo<A = Bar>` where A is an associated type.
1858-
Equality { ty: P<Ty> },
1859-
/// E.g., `A = 3` in `Foo<N = 3>` where N is an associated const.
1860-
ConstEquality { c: AnonConst },
1876+
/// E.g., `A = Bar`, `A = 3` in `Foo<A = Bar>` where A is an associated type.
1877+
Equality { term: Term },
18611878
/// E.g. `A: TraitA + TraitB` in `Foo<A: TraitA + TraitB>`.
18621879
Bound { bounds: GenericBounds },
18631880
}

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,10 @@ pub fn noop_visit_constraint<T: MutVisitor>(
440440
vis.visit_generic_args(gen_args);
441441
}
442442
match kind {
443-
AssocConstraintKind::Equality { ref mut ty } => vis.visit_ty(ty),
444-
AssocConstraintKind::ConstEquality { ref mut c } => vis.visit_anon_const(c),
443+
AssocConstraintKind::Equality { ref mut term } => match term {
444+
Term::Ty(ty) => vis.visit_ty(ty),
445+
Term::Const(c) => vis.visit_anon_const(c),
446+
},
445447
AssocConstraintKind::Bound { ref mut bounds } => visit_bounds(bounds, vis),
446448
}
447449
vis.visit_span(span);

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -492,8 +492,10 @@ pub fn walk_assoc_constraint<'a, V: Visitor<'a>>(visitor: &mut V, constraint: &'
492492
visitor.visit_generic_args(gen_args.span(), gen_args);
493493
}
494494
match constraint.kind {
495-
AssocConstraintKind::Equality { ref ty } => visitor.visit_ty(ty),
496-
AssocConstraintKind::ConstEquality { ref c } => visitor.visit_anon_const(c),
495+
AssocConstraintKind::Equality { ref term } => match term {
496+
Term::Ty(ty) => visitor.visit_ty(ty),
497+
Term::Const(c) => visitor.visit_anon_const(c),
498+
},
497499
AssocConstraintKind::Bound { ref bounds } => {
498500
walk_list!(visitor, visit_param_bound, bounds);
499501
}

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

+4-6
Original file line numberDiff line numberDiff line change
@@ -997,12 +997,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
997997
};
998998

999999
let kind = match constraint.kind {
1000-
AssocConstraintKind::Equality { ref ty } => {
1001-
hir::TypeBindingKind::Equality { ty: self.lower_ty(ty, itctx) }
1002-
}
1003-
AssocConstraintKind::ConstEquality { ref c } => {
1004-
hir::TypeBindingKind::Const { c: self.lower_anon_const(c) }
1005-
}
1000+
AssocConstraintKind::Equality { ref term } => match term {
1001+
Term::Ty(ref ty) => hir::TypeBindingKind::Equality { ty: self.lower_ty(ty, itctx) },
1002+
Term::Const(ref c) => hir::TypeBindingKind::Const { c: self.lower_anon_const(c) },
1003+
},
10061004
AssocConstraintKind::Bound { ref bounds } => {
10071005
let mut capturable_lifetimes;
10081006
let mut parent_def_id = self.current_hir_id_owner;

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ impl<'a> AstValidator<'a> {
141141
fn visit_assoc_constraint_from_generic_args(&mut self, constraint: &'a AssocConstraint) {
142142
match constraint.kind {
143143
AssocConstraintKind::Equality { .. } => {}
144-
AssocConstraintKind::ConstEquality { .. } => {}
145144
AssocConstraintKind::Bound { .. } => {
146145
if self.is_assoc_ty_bound_banned {
147146
self.err_handler().span_err(
@@ -1592,7 +1591,7 @@ fn deny_equality_constraints(
15921591
ident: *ident,
15931592
gen_args,
15941593
kind: AssocConstraintKind::Equality {
1595-
ty: predicate.rhs_ty.clone(),
1594+
term: predicate.rhs_ty.clone().into(),
15961595
},
15971596
span: ident.span,
15981597
});

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

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use crate::pp::Breaks::{Consistent, Inconsistent};
22
use crate::pp::{self, Breaks};
33

4-
use rustc_ast::attr;
54
use rustc_ast::ptr::P;
65
use rustc_ast::token::{self, BinOpToken, CommentKind, DelimToken, Nonterminal, Token, TokenKind};
76
use rustc_ast::tokenstream::{TokenStream, TokenTree};
87
use rustc_ast::util::classify;
98
use rustc_ast::util::comments::{gather_comments, Comment, CommentStyle};
109
use rustc_ast::util::parser::{self, AssocOp, Fixity};
1110
use rustc_ast::{self as ast, BlockCheckMode, PatKind, RangeEnd, RangeSyntax};
11+
use rustc_ast::{attr, Term};
1212
use rustc_ast::{GenericArg, MacArgs, ModKind};
1313
use rustc_ast::{GenericBound, SelfKind, TraitBoundModifier};
1414
use rustc_ast::{InlineAsmOperand, InlineAsmRegOrRegClass};
@@ -957,13 +957,12 @@ impl<'a> State<'a> {
957957
constraint.gen_args.as_ref().map(|args| self.print_generic_args(args, false));
958958
self.space();
959959
match &constraint.kind {
960-
ast::AssocConstraintKind::Equality { ty } => {
960+
ast::AssocConstraintKind::Equality { term } => {
961961
self.word_space("=");
962-
self.print_type(ty);
963-
}
964-
ast::AssocConstraintKind::ConstEquality { c } => {
965-
self.word_space("=");
966-
self.print_expr_anon_const(c);
962+
match term {
963+
Term::Ty(ty) => self.print_type(ty),
964+
Term::Const(c) => self.print_expr_anon_const(c),
965+
}
967966
}
968967
ast::AssocConstraintKind::Bound { bounds } => {
969968
self.print_type_bounds(":", &*bounds);

Diff for: compiler/rustc_interface/src/util.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use libloading::Library;
22
use rustc_ast::mut_visit::{visit_clobber, MutVisitor, *};
33
use rustc_ast::ptr::P;
4-
use rustc_ast::{self as ast, AttrVec, BlockCheckMode};
4+
use rustc_ast::{self as ast, AttrVec, BlockCheckMode, Term};
55
use rustc_codegen_ssa::traits::CodegenBackend;
66
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
77
#[cfg(parallel_compiler)]
@@ -739,9 +739,11 @@ impl<'a, 'b> ReplaceBodyWithLoop<'a, 'b> {
739739
},
740740
ast::AngleBracketedArg::Constraint(c) => match c.kind {
741741
ast::AssocConstraintKind::Bound { .. } => true,
742-
ast::AssocConstraintKind::ConstEquality { .. } => false,
743-
ast::AssocConstraintKind::Equality { ref ty } => {
744-
involves_impl_trait(ty)
742+
ast::AssocConstraintKind::Equality { ref term } => {
743+
match term {
744+
Term::Ty(ty) => involves_impl_trait(ty),
745+
Term::Const(_) => false,
746+
}
745747
}
746748
},
747749
})

Diff for: compiler/rustc_middle/src/ty/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,7 @@ pub type PolyCoercePredicate<'tcx> = ty::Binder<'tcx, CoercePredicate<'tcx>>;
799799
/// syntax, but it roughly corresponds to the syntactic forms:
800800
///
801801
/// 1. `T: TraitRef<..., Item = Type>`
802+
/// - Or `T: TraitRef<..., Item = Const>`
802803
/// 2. `<T as TraitRef<...>>::Item == Type` (NYI)
803804
///
804805
/// In particular, form #1 is "desugared" to the combination of a

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,9 @@ impl<'a> Parser<'a> {
505505
let span = ident.span.to(self.prev_token.span);
506506
let ty = match arg {
507507
Some(GenericArg::Type(ty)) => ty,
508-
Some(GenericArg::Const(c)) => return Ok(AssocConstraintKind::ConstEquality { c }),
508+
Some(GenericArg::Const(c)) => {
509+
return Ok(AssocConstraintKind::Equality { term: c.into() });
510+
}
509511
Some(GenericArg::Lifetime(lt)) => {
510512
self.struct_span_err(span, "associated lifetimes are not supported")
511513
.span_label(lt.ident.span, "the lifetime is given here")
@@ -540,7 +542,7 @@ impl<'a> Parser<'a> {
540542
return Err(err);
541543
}
542544
};
543-
Ok(AssocConstraintKind::Equality { ty })
545+
Ok(AssocConstraintKind::Equality { term: ty.into() })
544546
}
545547

546548
/// We do not permit arbitrary expressions as const arguments. They must be one of:

Diff for: src/tools/clippy/clippy_utils/src/ast_utils.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -645,13 +645,20 @@ pub fn eq_generic_bound(l: &GenericBound, r: &GenericBound) -> bool {
645645
}
646646
}
647647

648+
fn eq_term(l: &Term, r: &Term) -> bool {
649+
match (l, r) {
650+
(Term::Ty(l), Term::Ty(r)) => eq_ty(l,r),
651+
(Term::Const(l), Term::Const(r)) => eq_anon_const(l,r),
652+
_ => false,
653+
}
654+
}
655+
648656
pub fn eq_assoc_constraint(l: &AssocConstraint, r: &AssocConstraint) -> bool {
649657
use AssocConstraintKind::*;
650658
eq_id(l.ident, r.ident)
651659
&& match (&l.kind, &r.kind) {
652-
(Equality { ty: l }, Equality { ty: r }) => eq_ty(l, r),
660+
(Equality { term: l }, Equality { term: r }) => eq_term(l, r),
653661
(Bound { bounds: l }, Bound { bounds: r }) => over(l, r, eq_generic_bound),
654-
(ConstEquality { c: l }, ConstEquality { c: r }) => eq_anon_const(l, r),
655662
_ => false,
656663
}
657664
}

Diff for: src/tools/rustfmt/src/types.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::iter::ExactSizeIterator;
22
use std::ops::Deref;
33

4-
use rustc_ast::ast::{self, FnRetTy, Mutability};
4+
use rustc_ast::ast::{self, FnRetTy, Mutability, Term};
55
use rustc_ast::ptr;
66
use rustc_span::{symbol::kw, BytePos, Pos, Span};
77

@@ -178,7 +178,7 @@ impl<'a> Rewrite for SegmentParam<'a> {
178178

179179
impl Rewrite for ast::AssocConstraint {
180180
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
181-
use ast::AssocConstraintKind::{Bound, Equality, ConstEquality};
181+
use ast::AssocConstraintKind::{Bound, Equality};
182182

183183
let mut result = String::with_capacity(128);
184184
result.push_str(rewrite_ident(context, self.ident));
@@ -192,8 +192,8 @@ impl Rewrite for ast::AssocConstraint {
192192

193193
let infix = match (&self.kind, context.config.type_punctuation_density()) {
194194
(Bound { .. }, _) => ": ",
195-
(ConstEquality { .. } | Equality { .. }, TypeDensity::Wide) => " = ",
196-
(ConstEquality { .. } | Equality { .. }, TypeDensity::Compressed) => "=",
195+
(Equality { .. }, TypeDensity::Wide) => " = ",
196+
(Equality { .. }, TypeDensity::Compressed) => "=",
197197
};
198198
result.push_str(infix);
199199

@@ -209,8 +209,10 @@ impl Rewrite for ast::AssocConstraint {
209209
impl Rewrite for ast::AssocConstraintKind {
210210
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
211211
match self {
212-
ast::AssocConstraintKind::Equality { ty } => ty.rewrite(context, shape),
213-
ast::AssocConstraintKind::ConstEquality { c } => c.rewrite(context, shape),
212+
ast::AssocConstraintKind::Equality { term } => match term {
213+
Term::Ty(ty) => ty.rewrite(context, shape),
214+
Term::Const(c) => c.rewrite(context,shape),
215+
},
214216
ast::AssocConstraintKind::Bound { bounds } => bounds.rewrite(context, shape),
215217
}
216218
}

0 commit comments

Comments
 (0)