Skip to content

Commit d653995

Browse files
bors[bot]matklad
andauthored
Merge #10343
10343: internal: parser cleanups r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 9abea74 + d72f7cf commit d653995

File tree

9 files changed

+142
-173
lines changed

9 files changed

+142
-173
lines changed

crates/ide_assists/src/utils/gen_trait_fn_body.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,10 +439,10 @@ fn gen_partial_eq(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
439439
let eq_check =
440440
make::expr_bin_op(lhs, BinaryOp::CmpOp(CmpOp::Eq { negated: false }), rhs);
441441

442-
let mut case_count = 0;
442+
let mut n_cases = 0;
443443
let mut arms = vec![];
444444
for variant in enum_.variant_list()?.variants() {
445-
case_count += 1;
445+
n_cases += 1;
446446
match variant.field_list() {
447447
// => (Self::Bar { bin: l_bin }, Self::Bar { bin: r_bin }) => l_bin == r_bin,
448448
Some(ast::FieldList::RecordFieldList(list)) => {
@@ -517,7 +517,7 @@ fn gen_partial_eq(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
517517
let expr = match arms.len() {
518518
0 => eq_check,
519519
_ => {
520-
if case_count > arms.len() {
520+
if n_cases > arms.len() {
521521
let lhs = make::wildcard_pat().into();
522522
arms.push(make::match_arm(Some(lhs), None, eq_check));
523523
}

crates/parser/src/grammar/expressions.rs

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
mod atom;
22

3+
use super::*;
4+
35
pub(crate) use self::atom::{block_expr, match_arm_list};
46
pub(super) use self::atom::{literal, LITERAL_FIRST};
5-
use super::*;
67

78
pub(super) enum StmtWithSemi {
89
Yes,
@@ -47,11 +48,6 @@ fn expr_no_struct(p: &mut Parser) {
4748
expr_bp(p, r, 1);
4849
}
4950

50-
fn is_expr_stmt_attr_allowed(kind: SyntaxKind) -> bool {
51-
let forbid = matches!(kind, BIN_EXPR | RANGE_EXPR);
52-
!forbid
53-
}
54-
5551
pub(super) fn stmt(p: &mut Parser, with_semi: StmtWithSemi, prefer_expr: bool) {
5652
let m = p.start();
5753
// test attr_on_expr_stmt
@@ -79,13 +75,15 @@ pub(super) fn stmt(p: &mut Parser, with_semi: StmtWithSemi, prefer_expr: bool) {
7975
let (cm, blocklike) = expr_stmt(p);
8076
let kind = cm.as_ref().map(|cm| cm.kind()).unwrap_or(ERROR);
8177

82-
if has_attrs && !is_expr_stmt_attr_allowed(kind) {
83-
// test_err attr_on_expr_not_allowed
84-
// fn foo() {
85-
// #[A] 1 + 2;
86-
// #[B] if true {};
87-
// }
88-
p.error(format!("attributes are not allowed on {:?}", kind));
78+
if has_attrs {
79+
if matches!(kind, BIN_EXPR | RANGE_EXPR) {
80+
// test_err attr_on_expr_not_allowed
81+
// fn foo() {
82+
// #[A] 1 + 2;
83+
// #[B] if true {};
84+
// }
85+
p.error(format!("attributes are not allowed on {:?}", kind));
86+
}
8987
}
9088

9189
if p.at(T!['}']) || (prefer_expr && p.at(EOF)) {
@@ -117,55 +115,46 @@ pub(super) fn stmt(p: &mut Parser, with_semi: StmtWithSemi, prefer_expr: bool) {
117115
// }
118116

119117
match with_semi {
118+
StmtWithSemi::No => (),
119+
StmtWithSemi::Optional => {
120+
p.eat(T![;]);
121+
}
120122
StmtWithSemi::Yes => {
121123
if blocklike.is_block() {
122124
p.eat(T![;]);
123125
} else {
124126
p.expect(T![;]);
125127
}
126128
}
127-
StmtWithSemi::No => {}
128-
StmtWithSemi::Optional => {
129-
if p.at(T![;]) {
130-
p.eat(T![;]);
131-
}
132-
}
133129
}
134130

135131
m.complete(p, EXPR_STMT);
136132
}
137133

138134
// test let_stmt
139-
// fn foo() {
140-
// let a;
141-
// let b: i32;
142-
// let c = 92;
143-
// let d: i32 = 92;
144-
// let e: !;
145-
// let _: ! = {};
146-
// let f = #[attr]||{};
147-
// }
135+
// fn f() { let x: i32 = 92; }
148136
fn let_stmt(p: &mut Parser, m: Marker, with_semi: StmtWithSemi) {
149-
assert!(p.at(T![let]));
150137
p.bump(T![let]);
151138
patterns::pattern(p);
152139
if p.at(T![:]) {
140+
// test let_stmt_ascription
141+
// fn f() { let x: i32; }
153142
types::ascription(p);
154143
}
155144
if p.eat(T![=]) {
145+
// test let_stmt_init
146+
// fn f() { let x = 92; }
156147
expressions::expr_with_attrs(p);
157148
}
158149

159150
match with_semi {
151+
StmtWithSemi::No => (),
152+
StmtWithSemi::Optional => {
153+
p.eat(T![;]);
154+
}
160155
StmtWithSemi::Yes => {
161156
p.expect(T![;]);
162157
}
163-
StmtWithSemi::No => {}
164-
StmtWithSemi::Optional => {
165-
if p.at(T![;]) {
166-
p.eat(T![;]);
167-
}
168-
}
169158
}
170159
m.complete(p, LET_STMT);
171160
}
Lines changed: 33 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,35 @@
1-
SOURCE_FILE@0..135
2-
FN@0..134
1+
SOURCE_FILE@0..28
2+
FN@0..27
33
44
5-
6-
7-
8-
9-
10-
11-
12-
13-
14-
15-
16-
17-
18-
19-
20-
21-
22-
23-
24-
25-
26-
27-
28-
29-
30-
31-
32-
33-
34-
35-
36-
37-
38-
39-
40-
41-
42-
43-
44-
45-
46-
47-
48-
49-
50-
51-
52-
53-
54-
55-
56-
57-
58-
59-
60-
61-
62-
63-
64-
65-
66-
67-
68-
69-
70-
71-
72-
73-
74-
75-
76-
77-
78-
79-
80-
81-
82-
83-
84-
85-
86-
87-
88-
89-
90-
91-
92-
93-
94-
95-
96-
97-
98-
99-
100-
101-
102-
103-
104-
105-
106-
107-
108-
109-
110-
111-
112-
113-
114-
115-
116-
117-
118-
119-
120-
121-
122-
123-
124-
125-
126-
127-
5+
6+
7+
8+
9+
10+
11+
12+
13+
14+
15+
16+
17+
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
31+
32+
33+
34+
35+
Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1 @@
1-
fn foo() {
2-
let a;
3-
let b: i32;
4-
let c = 92;
5-
let d: i32 = 92;
6-
let e: !;
7-
let _: ! = {};
8-
let f = #[attr]||{};
9-
}
1+
fn f() { let x: i32 = 92; }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn f() { let x = 92; }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn f() { let x: i32; }

0 commit comments

Comments
 (0)