Skip to content

Commit e0e2627

Browse files
committed
Auto merge of #29072 - nagisa:place-arrow, r=pnkfelix
This commit generalises parsing of associative operators from left-associative only (with some ugly hacks to support right-associative assignment) to properly left/right-associative operators. Parsing is still is not general enough to handle non-associative, non-highest-precedence prefix or non-highest-precedence postfix operators (e.g. `..` range syntax) and should be made to be. Lastly, this commit adds support for parsing right-associative `<-` (left arrow) operator with precedence higher than assignment as the operator for placement-in feature. --- This PR still needs various non-parser changes (e.g. src/grammar and tests) and I’m still working on these; the meat of the PR can already be reviewed, though, I think. Please review carefully. I made sure that quirks I have discovered so far are preserved (see e.g. #29071) and am looking for more corner cases as I continue to work on tests et al, but there may be something I haven’t noticed or accounted for. EDIT: I’m also not sure I managed to preserve all the semantics with the range operator inside non-trivial expressions since these are a mess at the moment. Crater runs would be nice.
2 parents 8a72584 + 99f9bb1 commit e0e2627

19 files changed

+525
-223
lines changed

src/grammar/RustLexer.g4

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ lexer grammar RustLexer;
1010
tokens {
1111
EQ, LT, LE, EQEQ, NE, GE, GT, ANDAND, OROR, NOT, TILDE, PLUS,
1212
MINUS, STAR, SLASH, PERCENT, CARET, AND, OR, SHL, SHR, BINOP,
13-
BINOPEQ, AT, DOT, DOTDOT, DOTDOTDOT, COMMA, SEMI, COLON,
13+
BINOPEQ, LARROW, AT, DOT, DOTDOT, DOTDOTDOT, COMMA, SEMI, COLON,
1414
MOD_SEP, RARROW, FAT_ARROW, LPAREN, RPAREN, LBRACKET, RBRACKET,
1515
LBRACE, RBRACE, POUND, DOLLAR, UNDERSCORE, LIT_CHAR, LIT_BYTE,
1616
LIT_INTEGER, LIT_FLOAT, LIT_STR, LIT_STR_RAW, LIT_BYTE_STR,
@@ -44,6 +44,7 @@ AND : '&' ;
4444
OR : '|' ;
4545
SHL : '<<' ;
4646
SHR : '>>' ;
47+
LARROW : '<-' ;
4748

4849
BINOP
4950
: PLUS
@@ -56,6 +57,7 @@ BINOP
5657
| OR
5758
| SHL
5859
| SHR
60+
| LARROW
5961
;
6062

6163
BINOPEQ : BINOP EQ ;

src/grammar/lexer.l

+1
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ r/# {
317317
<str>\\[^n\nrt\\\x27\x220] { return -1; }
318318
<str>(.|\n) { yymore(); }
319319

320+
\<- { return LARROW; }
320321
-\> { return RARROW; }
321322
- { return '-'; }
322323
-= { return MINUSEQ; }

src/grammar/parser-lalr.y

+8-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ extern char *yytext;
4545
%token DOTDOTDOT
4646
%token MOD_SEP
4747
%token RARROW
48+
%token LARROW
4849
%token FAT_ARROW
4950
%token LIT_BYTE
5051
%token LIT_CHAR
@@ -167,7 +168,8 @@ extern char *yytext;
167168
// prefix_exprs
168169
%precedence RETURN
169170
170-
%left '=' SHLEQ SHREQ MINUSEQ ANDEQ OREQ PLUSEQ STAREQ SLASHEQ CARETEQ PERCENTEQ
171+
%right '=' SHLEQ SHREQ MINUSEQ ANDEQ OREQ PLUSEQ STAREQ SLASHEQ CARETEQ PERCENTEQ
172+
%right LARROW
171173
%left OROR
172174
%left ANDAND
173175
%left EQEQ NE
@@ -1316,6 +1318,7 @@ nonblock_expr
13161318
| RETURN expr { $$ = mk_node("ExprRet", 1, $2); }
13171319
| BREAK { $$ = mk_node("ExprBreak", 0); }
13181320
| BREAK lifetime { $$ = mk_node("ExprBreak", 1, $2); }
1321+
| nonblock_expr LARROW expr { $$ = mk_node("ExprInPlace", 2, $1, $3); }
13191322
| nonblock_expr '=' expr { $$ = mk_node("ExprAssign", 2, $1, $3); }
13201323
| nonblock_expr SHLEQ expr { $$ = mk_node("ExprAssignShl", 2, $1, $3); }
13211324
| nonblock_expr SHREQ expr { $$ = mk_node("ExprAssignShr", 2, $1, $3); }
@@ -1375,6 +1378,7 @@ expr
13751378
| RETURN expr { $$ = mk_node("ExprRet", 1, $2); }
13761379
| BREAK { $$ = mk_node("ExprBreak", 0); }
13771380
| BREAK ident { $$ = mk_node("ExprBreak", 1, $2); }
1381+
| expr LARROW expr { $$ = mk_node("ExprInPlace", 2, $1, $3); }
13781382
| expr '=' expr { $$ = mk_node("ExprAssign", 2, $1, $3); }
13791383
| expr SHLEQ expr { $$ = mk_node("ExprAssignShl", 2, $1, $3); }
13801384
| expr SHREQ expr { $$ = mk_node("ExprAssignShr", 2, $1, $3); }
@@ -1435,6 +1439,7 @@ nonparen_expr
14351439
| RETURN expr { $$ = mk_node("ExprRet", 1, $2); }
14361440
| BREAK { $$ = mk_node("ExprBreak", 0); }
14371441
| BREAK ident { $$ = mk_node("ExprBreak", 1, $2); }
1442+
| nonparen_expr LARROW nonparen_expr { $$ = mk_node("ExprInPlace", 2, $1, $3); }
14381443
| nonparen_expr '=' nonparen_expr { $$ = mk_node("ExprAssign", 2, $1, $3); }
14391444
| nonparen_expr SHLEQ nonparen_expr { $$ = mk_node("ExprAssignShl", 2, $1, $3); }
14401445
| nonparen_expr SHREQ nonparen_expr { $$ = mk_node("ExprAssignShr", 2, $1, $3); }
@@ -1495,6 +1500,7 @@ expr_nostruct
14951500
| RETURN expr { $$ = mk_node("ExprRet", 1, $2); }
14961501
| BREAK { $$ = mk_node("ExprBreak", 0); }
14971502
| BREAK ident { $$ = mk_node("ExprBreak", 1, $2); }
1503+
| expr_nostruct LARROW expr_nostruct { $$ = mk_node("ExprInPlace", 2, $1, $3); }
14981504
| expr_nostruct '=' expr_nostruct { $$ = mk_node("ExprAssign", 2, $1, $3); }
14991505
| expr_nostruct SHLEQ expr_nostruct { $$ = mk_node("ExprAssignShl", 2, $1, $3); }
15001506
| expr_nostruct SHREQ expr_nostruct { $$ = mk_node("ExprAssignShr", 2, $1, $3); }
@@ -1794,6 +1800,7 @@ unpaired_token
17941800
| GE { $$ = mk_atom(yytext); }
17951801
| ANDAND { $$ = mk_atom(yytext); }
17961802
| OROR { $$ = mk_atom(yytext); }
1803+
| LARROW { $$ = mk_atom(yytext); }
17971804
| SHLEQ { $$ = mk_atom(yytext); }
17981805
| SHREQ { $$ = mk_atom(yytext); }
17991806
| MINUSEQ { $$ = mk_atom(yytext); }

src/grammar/verify.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use syntax::parse::lexer::TokenAndSpan;
3535

3636
fn parse_token_list(file: &str) -> HashMap<String, token::Token> {
3737
fn id() -> token::Token {
38-
token::Ident(ast::Ident::with_empty_ctxt(Name(0))), token::Plain)
38+
token::Ident(ast::Ident::with_empty_ctxt(Name(0)), token::Plain)
3939
}
4040

4141
let mut res = HashMap::new();
@@ -208,7 +208,7 @@ fn parse_antlr_token(s: &str, tokens: &HashMap<String, token::Token>, surrogate_
208208
token::Literal(token::ByteStr(..), n) => token::Literal(token::ByteStr(nm), n),
209209
token::Literal(token::ByteStrRaw(..), n) => token::Literal(token::ByteStrRaw(fix(content),
210210
count(content)), n),
211-
token::Ident(..) => token::Ident(ast::Ident::with_empty_ctxt(nm)),
211+
token::Ident(..) => token::Ident(ast::Ident::with_empty_ctxt(nm),
212212
token::ModName),
213213
token::Lifetime(..) => token::Lifetime(ast::Ident::with_empty_ctxt(nm)),
214214
ref t => t.clone()

src/librustc_lint/unused.rs

+1
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ impl EarlyLintPass for UnusedParens {
369369
ast::ExprRet(Some(ref value)) => (value, "`return` value", false),
370370
ast::ExprAssign(_, ref value) => (value, "assigned value", false),
371371
ast::ExprAssignOp(_, _, ref value) => (value, "assigned value", false),
372+
ast::ExprInPlace(_, ref value) => (value, "emplacement value", false),
372373
_ => return
373374
};
374375
self.check_unused_parens_core(cx, &**value, msg, struct_lit_needs_parens);

src/libsyntax/ast_util.rs

-20
Original file line numberDiff line numberDiff line change
@@ -242,26 +242,6 @@ pub fn struct_field_visibility(field: ast::StructField) -> Visibility {
242242
}
243243
}
244244

245-
/// Maps a binary operator to its precedence
246-
pub fn operator_prec(op: ast::BinOp_) -> usize {
247-
match op {
248-
// 'as' sits here with 12
249-
BiMul | BiDiv | BiRem => 11,
250-
BiAdd | BiSub => 10,
251-
BiShl | BiShr => 9,
252-
BiBitAnd => 8,
253-
BiBitXor => 7,
254-
BiBitOr => 6,
255-
BiLt | BiLe | BiGe | BiGt | BiEq | BiNe => 3,
256-
BiAnd => 2,
257-
BiOr => 1
258-
}
259-
}
260-
261-
/// Precedence of the `as` operator, which is a binary operator
262-
/// not appearing in the prior table.
263-
pub const AS_PREC: usize = 12;
264-
265245
pub fn empty_generics() -> Generics {
266246
Generics {
267247
lifetimes: Vec::new(),

src/libsyntax/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ pub mod util {
6666
#[cfg(test)]
6767
pub mod parser_testing;
6868
pub mod small_vector;
69+
pub mod parser;
6970
}
7071

7172
pub mod diagnostics {

0 commit comments

Comments
 (0)