Unnamed repository; edit this file 'description' to name the repository.
Remove parser restriction on varargs positioning
Jonas Schievink 2022-03-31
parent 9b000b5 · commit 42ecf40
-rw-r--r--crates/parser/src/grammar/params.rs45
-rw-r--r--crates/parser/test_data/parser/inline/ok/0123_param_list_vararg.rast12
-rw-r--r--crates/parser/test_data/parser/inline/ok/0123_param_list_vararg.rs2
-rw-r--r--crates/parser/test_data/parser/inline/ok/0156_fn_def_param.rast4
-rw-r--r--crates/parser/test_data/parser/inline/ok/0156_fn_def_param.rs2
5 files changed, 37 insertions, 28 deletions
diff --git a/crates/parser/src/grammar/params.rs b/crates/parser/src/grammar/params.rs
index a000f515cc..c2e633fdb0 100644
--- a/crates/parser/src/grammar/params.rs
+++ b/crates/parser/src/grammar/params.rs
@@ -71,13 +71,10 @@ fn list_(p: &mut Parser, flavor: Flavor) {
m.abandon(p);
break;
}
- let param = param(p, m, flavor);
+ param(p, m, flavor);
if !p.at(ket) {
p.expect(T![,]);
}
- if let Variadic(true) = param {
- break;
- }
}
if let Some(m) = param_marker {
@@ -90,27 +87,24 @@ fn list_(p: &mut Parser, flavor: Flavor) {
const PARAM_FIRST: TokenSet = patterns::PATTERN_FIRST.union(types::TYPE_FIRST);
-struct Variadic(bool);
-
-fn param(p: &mut Parser, m: Marker, flavor: Flavor) -> Variadic {
- let mut res = Variadic(false);
+fn param(p: &mut Parser, m: Marker, flavor: Flavor) {
match flavor {
// test param_list_vararg
- // extern "C" { fn printf(format: *const i8, ...) -> i32; }
- Flavor::FnDef | Flavor::FnPointer if p.eat(T![...]) => res = Variadic(true),
+ // extern "C" { fn printf(format: *const i8, ..., _: u8) -> i32; }
+ Flavor::FnDef | Flavor::FnPointer if p.eat(T![...]) => {}
// test fn_def_param
- // fn foo((x, y): (i32, i32)) {}
+ // fn foo(..., (x, y): (i32, i32)) {}
Flavor::FnDef => {
patterns::pattern(p);
- if variadic_param(p) {
- res = Variadic(true);
- } else if p.at(T![:]) {
- types::ascription(p);
- } else {
- // test_err missing_fn_param_type
- // fn f(x y: i32, z, t: i32) {}
- p.error("missing type for function parameter");
+ if !variadic_param(p) {
+ if p.at(T![:]) {
+ types::ascription(p);
+ } else {
+ // test_err missing_fn_param_type
+ // fn f(x y: i32, z, t: i32) {}
+ p.error("missing type for function parameter");
+ }
}
}
// test value_parameters_no_patterns
@@ -127,12 +121,12 @@ fn param(p: &mut Parser, m: Marker, flavor: Flavor) -> Variadic {
Flavor::FnPointer => {
if (p.at(IDENT) || p.at(UNDERSCORE)) && p.nth(1) == T![:] && !p.nth_at(1, T![::]) {
patterns::pattern_single(p);
- if variadic_param(p) {
- res = Variadic(true);
- } else if p.at(T![:]) {
- types::ascription(p);
- } else {
- p.error("missing type for function parameter");
+ if !variadic_param(p) {
+ if p.at(T![:]) {
+ types::ascription(p);
+ } else {
+ p.error("missing type for function parameter");
+ }
}
} else {
types::type_(p);
@@ -150,7 +144,6 @@ fn param(p: &mut Parser, m: Marker, flavor: Flavor) -> Variadic {
}
}
m.complete(p, PARAM);
- res
}
fn variadic_param(p: &mut Parser) -> bool {
diff --git a/crates/parser/test_data/parser/inline/ok/0123_param_list_vararg.rast b/crates/parser/test_data/parser/inline/ok/0123_param_list_vararg.rast
index c86cf18bbe..338d53995a 100644
--- a/crates/parser/test_data/parser/inline/ok/0123_param_list_vararg.rast
+++ b/crates/parser/test_data/parser/inline/ok/0123_param_list_vararg.rast
@@ -34,6 +34,18 @@ SOURCE_FILE
WHITESPACE " "
PARAM
DOT3 "..."
+ COMMA ","
+ WHITESPACE " "
+ PARAM
+ WILDCARD_PAT
+ UNDERSCORE "_"
+ COLON ":"
+ WHITESPACE " "
+ PATH_TYPE
+ PATH
+ PATH_SEGMENT
+ NAME_REF
+ IDENT "u8"
R_PAREN ")"
WHITESPACE " "
RET_TYPE
diff --git a/crates/parser/test_data/parser/inline/ok/0123_param_list_vararg.rs b/crates/parser/test_data/parser/inline/ok/0123_param_list_vararg.rs
index c59addaf42..533096cd5b 100644
--- a/crates/parser/test_data/parser/inline/ok/0123_param_list_vararg.rs
+++ b/crates/parser/test_data/parser/inline/ok/0123_param_list_vararg.rs
@@ -1 +1 @@
-extern "C" { fn printf(format: *const i8, ...) -> i32; }
+extern "C" { fn printf(format: *const i8, ..., _: u8) -> i32; }
diff --git a/crates/parser/test_data/parser/inline/ok/0156_fn_def_param.rast b/crates/parser/test_data/parser/inline/ok/0156_fn_def_param.rast
index 7511877aa8..ce425a1afd 100644
--- a/crates/parser/test_data/parser/inline/ok/0156_fn_def_param.rast
+++ b/crates/parser/test_data/parser/inline/ok/0156_fn_def_param.rast
@@ -7,6 +7,10 @@ SOURCE_FILE
PARAM_LIST
L_PAREN "("
PARAM
+ DOT3 "..."
+ COMMA ","
+ WHITESPACE " "
+ PARAM
TUPLE_PAT
L_PAREN "("
IDENT_PAT
diff --git a/crates/parser/test_data/parser/inline/ok/0156_fn_def_param.rs b/crates/parser/test_data/parser/inline/ok/0156_fn_def_param.rs
index 7b277c16b1..7b4c626582 100644
--- a/crates/parser/test_data/parser/inline/ok/0156_fn_def_param.rs
+++ b/crates/parser/test_data/parser/inline/ok/0156_fn_def_param.rs
@@ -1 +1 @@
-fn foo((x, y): (i32, i32)) {}
+fn foo(..., (x, y): (i32, i32)) {}