Skip to content

Commit 6af9846

Browse files
committed
Auto merge of #77124 - spastorino:const-exprs-rfc-2920, r=oli-obk
Implement const expressions and patterns (RFC 2920) cc `@ecstatic-morse` `@lcnr` `@oli-obk` `@petrochenkov`
2 parents dda2b5e + 03321b8 commit 6af9846

File tree

48 files changed

+298
-33
lines changed

Some content is hidden

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

48 files changed

+298
-33
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,7 @@ impl Expr {
11521152
match self.kind {
11531153
ExprKind::Box(_) => ExprPrecedence::Box,
11541154
ExprKind::Array(_) => ExprPrecedence::Array,
1155+
ExprKind::ConstBlock(_) => ExprPrecedence::ConstBlock,
11551156
ExprKind::Call(..) => ExprPrecedence::Call,
11561157
ExprKind::MethodCall(..) => ExprPrecedence::MethodCall,
11571158
ExprKind::Tup(_) => ExprPrecedence::Tup,
@@ -1207,6 +1208,8 @@ pub enum ExprKind {
12071208
Box(P<Expr>),
12081209
/// An array (`[a, b, c, d]`)
12091210
Array(Vec<P<Expr>>),
1211+
/// Allow anonymous constants from an inline `const` block
1212+
ConstBlock(AnonConst),
12101213
/// A function call
12111214
///
12121215
/// The first field resolves to the function itself,

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,9 @@ pub fn noop_visit_expr<T: MutVisitor>(
11061106
match kind {
11071107
ExprKind::Box(expr) => vis.visit_expr(expr),
11081108
ExprKind::Array(exprs) => visit_exprs(exprs, vis),
1109+
ExprKind::ConstBlock(anon_const) => {
1110+
vis.visit_anon_const(anon_const);
1111+
}
11091112
ExprKind::Repeat(expr, count) => {
11101113
vis.visit_expr(expr);
11111114
vis.visit_anon_const(count);

compiler/rustc_ast/src/token.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ pub fn ident_can_begin_expr(name: Symbol, span: Span, is_raw: bool) -> bool {
153153
kw::Do,
154154
kw::Box,
155155
kw::Break,
156+
kw::Const,
156157
kw::Continue,
157158
kw::False,
158159
kw::For,

compiler/rustc_ast/src/util/parser.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ pub enum ExprPrecedence {
282282
ForLoop,
283283
Loop,
284284
Match,
285+
ConstBlock,
285286
Block,
286287
TryBlock,
287288
Struct,
@@ -346,6 +347,7 @@ impl ExprPrecedence {
346347
ExprPrecedence::ForLoop |
347348
ExprPrecedence::Loop |
348349
ExprPrecedence::Match |
350+
ExprPrecedence::ConstBlock |
349351
ExprPrecedence::Block |
350352
ExprPrecedence::TryBlock |
351353
ExprPrecedence::Async |

compiler/rustc_ast/src/visit.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,7 @@ pub trait Visitor<'ast>: Sized {
200200
walk_generic_args(self, path_span, generic_args)
201201
}
202202
fn visit_generic_arg(&mut self, generic_arg: &'ast GenericArg) {
203-
match generic_arg {
204-
GenericArg::Lifetime(lt) => self.visit_lifetime(lt),
205-
GenericArg::Type(ty) => self.visit_ty(ty),
206-
GenericArg::Const(ct) => self.visit_anon_const(ct),
207-
}
203+
walk_generic_arg(self, generic_arg)
208204
}
209205
fn visit_assoc_ty_constraint(&mut self, constraint: &'ast AssocTyConstraint) {
210206
walk_assoc_ty_constraint(self, constraint)
@@ -486,6 +482,17 @@ where
486482
}
487483
}
488484

485+
pub fn walk_generic_arg<'a, V>(visitor: &mut V, generic_arg: &'a GenericArg)
486+
where
487+
V: Visitor<'a>,
488+
{
489+
match generic_arg {
490+
GenericArg::Lifetime(lt) => visitor.visit_lifetime(lt),
491+
GenericArg::Type(ty) => visitor.visit_ty(ty),
492+
GenericArg::Const(ct) => visitor.visit_anon_const(ct),
493+
}
494+
}
495+
489496
pub fn walk_assoc_ty_constraint<'a, V: Visitor<'a>>(
490497
visitor: &mut V,
491498
constraint: &'a AssocTyConstraint,
@@ -717,6 +724,7 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
717724
ExprKind::Array(ref subexpressions) => {
718725
walk_list!(visitor, visit_expr, subexpressions);
719726
}
727+
ExprKind::ConstBlock(ref anon_const) => visitor.visit_anon_const(anon_const),
720728
ExprKind::Repeat(ref element, ref count) => {
721729
visitor.visit_expr(element);
722730
visitor.visit_anon_const(count)

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
3030
let kind = match e.kind {
3131
ExprKind::Box(ref inner) => hir::ExprKind::Box(self.lower_expr(inner)),
3232
ExprKind::Array(ref exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)),
33+
ExprKind::ConstBlock(ref anon_const) => {
34+
let anon_const = self.lower_anon_const(anon_const);
35+
hir::ExprKind::ConstBlock(anon_const)
36+
}
3337
ExprKind::Repeat(ref expr, ref count) => {
3438
let expr = self.lower_expr(expr);
3539
let count = self.lower_anon_const(count);

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ impl<'a> AstValidator<'a> {
287287
// ```
288288
fn check_expr_within_pat(&self, expr: &Expr, allow_paths: bool) {
289289
match expr.kind {
290-
ExprKind::Lit(..) | ExprKind::Err => {}
290+
ExprKind::Lit(..) | ExprKind::ConstBlock(..) | ExprKind::Err => {}
291291
ExprKind::Path(..) if allow_paths => {}
292292
ExprKind::Unary(UnOp::Neg, ref inner) if matches!(inner.kind, ExprKind::Lit(_)) => {}
293293
_ => self.err_handler().span_err(

compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,7 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session) {
629629
gate_all!(const_trait_bound_opt_out, "`?const` on trait bounds is experimental");
630630
gate_all!(const_trait_impl, "const trait impls are experimental");
631631
gate_all!(half_open_range_patterns, "half-open range patterns are unstable");
632+
gate_all!(inline_const, "inline-const is experimental");
632633

633634
// All uses of `gate_all!` below this point were added in #65742,
634635
// and subsequently disabled (with the non-early gating readded).

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1714,6 +1714,14 @@ impl<'a> State<'a> {
17141714
self.end();
17151715
}
17161716

1717+
fn print_expr_anon_const(&mut self, expr: &ast::AnonConst, attrs: &[ast::Attribute]) {
1718+
self.ibox(INDENT_UNIT);
1719+
self.s.word("const");
1720+
self.print_inner_attributes_inline(attrs);
1721+
self.print_expr(&expr.value);
1722+
self.end();
1723+
}
1724+
17171725
fn print_expr_repeat(
17181726
&mut self,
17191727
element: &ast::Expr,
@@ -1890,6 +1898,9 @@ impl<'a> State<'a> {
18901898
ast::ExprKind::Array(ref exprs) => {
18911899
self.print_expr_vec(&exprs[..], attrs);
18921900
}
1901+
ast::ExprKind::ConstBlock(ref anon_const) => {
1902+
self.print_expr_anon_const(anon_const, attrs);
1903+
}
18931904
ast::ExprKind::Repeat(ref element, ref count) => {
18941905
self.print_expr_repeat(element, count, attrs);
18951906
}

compiler/rustc_feature/src/active.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,9 @@ declare_features! (
598598
/// Allows `#[instruction_set(_)]` attribute
599599
(active, isa_attribute, "1.48.0", Some(74727), None),
600600

601+
/// Allow anonymous constants from an inline `const` block
602+
(active, inline_const, "1.49.0", Some(76001), None),
603+
601604
// -------------------------------------------------------------------------
602605
// feature-group-end: actual feature gates
603606
// -------------------------------------------------------------------------
@@ -618,6 +621,7 @@ pub const INCOMPLETE_FEATURES: &[Symbol] = &[
618621
sym::const_trait_bound_opt_out,
619622
sym::lazy_normalization_consts,
620623
sym::specialization,
624+
sym::inline_const,
621625
];
622626

623627
/// Some features are not allowed to be used together at the same time, if

0 commit comments

Comments
 (0)