Skip to content

Commit 3ab8d7a

Browse files
bors[bot]Veykril
andauthored
Merge #7414
7414: Add validation for mutable const items r=Veykril a=Veykril bors r+ Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2 parents a7a1bb4 + 70d43c3 commit 3ab8d7a

File tree

6 files changed

+40
-25
lines changed

6 files changed

+40
-25
lines changed

crates/parser/src/grammar/items/consts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub(super) fn konst(p: &mut Parser, m: Marker) {
1313
fn const_or_static(p: &mut Parser, m: Marker, kw: SyntaxKind, def: SyntaxKind) {
1414
assert!(p.at(kw));
1515
p.bump(kw);
16-
p.eat(T![mut]); // FIXME: validator to forbid const mut
16+
p.eat(T![mut]);
1717

1818
// Allow `_` in place of an identifier in a `const`.
1919
let is_const_underscore = kw == T![const] && p.eat(T![_]);

crates/syntax/src/validation.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
//! FIXME: write short doc here
1+
//! This module implements syntax validation that the parser doesn't handle.
2+
//!
3+
//! A failed validation emits a diagnostic.
24
35
mod block;
46

@@ -92,6 +94,7 @@ pub(crate) fn validate(root: &SyntaxNode) -> Vec<SyntaxError> {
9294
match_ast! {
9395
match node {
9496
ast::Literal(it) => validate_literal(it, &mut errors),
97+
ast::Const(it) => validate_const(it, &mut errors),
9598
ast::BlockExpr(it) => block::validate_block_expr(it, &mut errors),
9699
ast::FieldExpr(it) => validate_numeric_name(it.name_ref(), &mut errors),
97100
ast::RecordExprField(it) => validate_numeric_name(it.name_ref(), &mut errors),
@@ -362,3 +365,14 @@ fn validate_macro_rules(mac: ast::MacroRules, errors: &mut Vec<SyntaxError>) {
362365
));
363366
}
364367
}
368+
369+
fn validate_const(const_: ast::Const, errors: &mut Vec<SyntaxError>) {
370+
if let Some(mut_token) = const_
371+
.const_token()
372+
.and_then(|t| t.next_token())
373+
.and_then(|t| algo::skip_trivia_token(t, Direction::Next))
374+
.filter(|t| t.kind() == T![mut])
375+
{
376+
errors.push(SyntaxError::new("const globals cannot be mutable", mut_token.text_range()));
377+
}
378+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
SOURCE_FILE@0..24
2+
CONST@0..23
3+
CONST_KW@0..5 "const"
4+
WHITESPACE@5..6 " "
5+
MUT_KW@6..9 "mut"
6+
WHITESPACE@9..10 " "
7+
NAME@10..13
8+
IDENT@10..13 "FOO"
9+
COLON@13..14 ":"
10+
WHITESPACE@14..15 " "
11+
TUPLE_TYPE@15..17
12+
L_PAREN@15..16 "("
13+
R_PAREN@16..17 ")"
14+
WHITESPACE@17..18 " "
15+
EQ@18..19 "="
16+
WHITESPACE@19..20 " "
17+
TUPLE_EXPR@20..22
18+
L_PAREN@20..21 "("
19+
R_PAREN@21..22 ")"
20+
SEMICOLON@22..23 ";"
21+
WHITESPACE@23..24 "\n"
22+
error 6..9: const globals cannot be mutable
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const mut FOO: () = ();

crates/syntax/test_data/parser/ok/0024_const_item.rast

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
SOURCE_FILE@0..64
1+
SOURCE_FILE@0..39
22
CONST@0..17
33
CONST_KW@0..5 "const"
44
WHITESPACE@5..6 " "
@@ -36,24 +36,3 @@ SOURCE_FILE@0..64
3636
INT_NUMBER@35..37 "92"
3737
SEMICOLON@37..38 ";"
3838
WHITESPACE@38..39 "\n"
39-
CONST@39..63
40-
CONST_KW@39..44 "const"
41-
WHITESPACE@44..45 " "
42-
MUT_KW@45..48 "mut"
43-
WHITESPACE@48..49 " "
44-
NAME@49..52
45-
IDENT@49..52 "BAR"
46-
COLON@52..53 ":"
47-
WHITESPACE@53..54 " "
48-
PATH_TYPE@54..57
49-
PATH@54..57
50-
PATH_SEGMENT@54..57
51-
NAME_REF@54..57
52-
IDENT@54..57 "u32"
53-
WHITESPACE@57..58 " "
54-
EQ@58..59 "="
55-
WHITESPACE@59..60 " "
56-
LITERAL@60..62
57-
INT_NUMBER@60..62 "62"
58-
SEMICOLON@62..63 ";"
59-
WHITESPACE@63..64 "\n"
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
const _: u32 = 0;
22
const FOO: u32 = 92;
3-
const mut BAR: u32 = 62;

0 commit comments

Comments
 (0)