Skip to content

Commit 68013ee

Browse files
bors[bot]matklad
andauthored
Merge #11165
11165: internal: start enforcing invariants for top-level entry points r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents ddb420a + 640cc27 commit 68013ee

File tree

4 files changed

+107
-3
lines changed

4 files changed

+107
-3
lines changed

crates/hir_def/src/macro_expansion_tests/mbe/tt_conversion.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ macro_rules! m1 { () => (Some(x) left overs) }
141141
macro_rules! m2 { () => ($) }
142142
143143
fn main() {
144-
let Some(x) = ();
144+
let Some(x)left overs = ();
145145
let /* parse error: expected pattern */
146146
$ = ();
147147
}

crates/parser/src/grammar.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,32 @@ pub(crate) mod entry {
109109
items::mod_contents(p, false);
110110
m.complete(p, MACRO_ITEMS);
111111
}
112+
113+
pub(crate) fn pattern(p: &mut Parser) {
114+
let m = p.start();
115+
patterns::pattern_single(p);
116+
if p.at(EOF) {
117+
m.abandon(p);
118+
return;
119+
}
120+
while !p.at(EOF) {
121+
p.bump_any();
122+
}
123+
m.complete(p, ERROR);
124+
}
125+
126+
pub(crate) fn type_(p: &mut Parser) {
127+
let m = p.start();
128+
types::type_(p);
129+
if p.at(EOF) {
130+
m.abandon(p);
131+
return;
132+
}
133+
while !p.at(EOF) {
134+
p.bump_any();
135+
}
136+
m.complete(p, ERROR);
137+
}
112138
}
113139
}
114140

crates/parser/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ pub enum TopEntryPoint {
110110
Pattern,
111111
Type,
112112
Expr,
113+
/// Edge case -- macros generally don't expand to attributes, with the
114+
/// exception of `cfg_attr` which does!
113115
MetaItem,
114116
}
115117

@@ -119,9 +121,9 @@ impl TopEntryPoint {
119121
TopEntryPoint::SourceFile => grammar::entry::top::source_file,
120122
TopEntryPoint::MacroStmts => grammar::entry::top::macro_stmts,
121123
TopEntryPoint::MacroItems => grammar::entry::top::macro_items,
124+
TopEntryPoint::Pattern => grammar::entry::top::pattern,
125+
TopEntryPoint::Type => grammar::entry::top::type_,
122126
// FIXME
123-
TopEntryPoint::Pattern => grammar::entry::prefix::pat,
124-
TopEntryPoint::Type => grammar::entry::prefix::ty,
125127
TopEntryPoint::Expr => grammar::entry::prefix::expr,
126128
TopEntryPoint::MetaItem => grammar::entry::prefix::meta_item,
127129
};

crates/parser/src/tests/top_entries.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,82 @@ fn macro_pattern() {
146146
R_PAREN ")"
147147
"#]],
148148
);
149+
150+
check(
151+
TopEntryPoint::Pattern,
152+
"None leftover tokens",
153+
expect![[r#"
154+
ERROR
155+
IDENT_PAT
156+
NAME
157+
IDENT "None"
158+
WHITESPACE " "
159+
IDENT "leftover"
160+
WHITESPACE " "
161+
IDENT "tokens"
162+
"#]],
163+
);
164+
165+
check(
166+
TopEntryPoint::Pattern,
167+
"@err",
168+
expect![[r#"
169+
ERROR
170+
ERROR
171+
AT "@"
172+
IDENT "err"
173+
error 0: expected pattern
174+
"#]],
175+
);
176+
}
177+
178+
#[test]
179+
fn type_() {
180+
check(
181+
TopEntryPoint::Type,
182+
"Option<!>",
183+
expect![[r#"
184+
PATH_TYPE
185+
PATH
186+
PATH_SEGMENT
187+
NAME_REF
188+
IDENT "Option"
189+
GENERIC_ARG_LIST
190+
L_ANGLE "<"
191+
TYPE_ARG
192+
NEVER_TYPE
193+
BANG "!"
194+
R_ANGLE ">"
195+
"#]],
196+
);
197+
check(
198+
TopEntryPoint::Type,
199+
"() () ()",
200+
expect![[r#"
201+
ERROR
202+
TUPLE_TYPE
203+
L_PAREN "("
204+
R_PAREN ")"
205+
WHITESPACE " "
206+
L_PAREN "("
207+
R_PAREN ")"
208+
WHITESPACE " "
209+
L_PAREN "("
210+
R_PAREN ")"
211+
"#]],
212+
);
213+
check(
214+
TopEntryPoint::Type,
215+
"$$$",
216+
expect![[r#"
217+
ERROR
218+
ERROR
219+
DOLLAR "$"
220+
DOLLAR "$"
221+
DOLLAR "$"
222+
error 0: expected type
223+
"#]],
224+
);
149225
}
150226

151227
#[track_caller]

0 commit comments

Comments
 (0)