Skip to content

Commit

Permalink
Remove error checks for mixing up PEG rule and Rust value arguments
Browse files Browse the repository at this point in the history
When a rule with PEG rule closure arguments is recursive, it can be
useful to pass down the rule argument as a value, since re-capturing it
results in a type-length limit error from rustc. (#226)

Rustc's type error if you do pass the wrong type isn't as good as the
custom one, but isn't too bad.
  • Loading branch information
kevinmehall committed Mar 14, 2021
1 parent 2dacb48 commit 1071e80
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 42 deletions.
25 changes: 0 additions & 25 deletions peg-macros/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,31 +471,6 @@ fn compile_expr(context: &Context, e: &SpannedExpr, result_used: bool) -> TokenS
);
}

for (param, arg) in rule_def.params.iter().zip(rule_args.iter()) {
match (&param.ty, &arg) {
(RuleParamTy::Rust(..), RuleArg::Peg(..)) => {
return report_error_expr(
rule_name.span(),
format!(
"parameter `{}` expects a value, but a PEG expression was passed",
param.name
),
);
}
(RuleParamTy::Rule(..), RuleArg::Rust(..)) => {
return report_error_expr(
rule_name.span(),
format!(
"parameter `{}` expects a PEG expression, but a value was passed",
param.name
),
);
}
(RuleParamTy::Rule(..), RuleArg::Peg(..)) => (),
(RuleParamTy::Rust(..), RuleArg::Rust(..)) => (),
}
}

let func = format_ident!("__parse_{}", rule_name, span = rule_name.span());
let extra_args_call = &context.extra_args_call;

Expand Down
3 changes: 0 additions & 3 deletions tests/compile-fail/rule_args_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ peg::parser!(grammar foo() for str {
rule too_few() = foo(1) //~ ERROR
rule too_many() = foo(1, <[_]>, 2) //~ ERROR

rule wrong_type1() = foo(<[_]>, 1) //~ ERROR
rule wrong_type2() = foo(1, 1) //~ ERROR

pub rule pub_rule_arg(x: rule<()>) = "foo" //~ ERROR
});

Expand Down
16 changes: 2 additions & 14 deletions tests/compile-fail/rule_args_errors.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,8 @@ error: this rule takes 2 parameters but 3 parameters were supplied
8 | rule too_many() = foo(1, <[_]>, 2) //~ ERROR
| ^^^

error: parameter `x` expects a value, but a PEG expression was passed
--> $DIR/rule_args_errors.rs:10:26
|
10 | rule wrong_type1() = foo(<[_]>, 1) //~ ERROR
| ^^^

error: parameter `y` expects a PEG expression, but a value was passed
--> $DIR/rule_args_errors.rs:11:26
|
11 | rule wrong_type2() = foo(1, 1) //~ ERROR
| ^^^

error: parameters on `pub rule` must be Rust types
--> $DIR/rule_args_errors.rs:13:27
--> $DIR/rule_args_errors.rs:10:27
|
13 | pub rule pub_rule_arg(x: rule<()>) = "foo" //~ ERROR
10 | pub rule pub_rule_arg(x: rule<()>) = "foo" //~ ERROR
| ^

0 comments on commit 1071e80

Please sign in to comment.