revset: report invalid string argument with span

Also dropped "found: {}" from the error summary as it's obvious.
This commit is contained in:
Yuya Nishihara 2022-11-01 23:13:18 +09:00
parent aeee0acd08
commit b938b5e907
2 changed files with 19 additions and 10 deletions

View file

@ -736,7 +736,7 @@ fn parse_function_expression(
} }
"description" | "author" | "committer" => { "description" | "author" | "committer" => {
let arg = expect_one_argument(name, argument_pairs)?; let arg = expect_one_argument(name, argument_pairs)?;
let needle = parse_function_argument_to_string(name, arg.into_inner())?; let needle = parse_function_argument_to_string(name, arg)?;
let candidates = RevsetExpression::all(); let candidates = RevsetExpression::all();
match name { match name {
"description" => Ok(candidates.with_description(needle)), "description" => Ok(candidates.with_description(needle)),
@ -752,7 +752,7 @@ fn parse_function_expression(
let paths = argument_pairs let paths = argument_pairs
.map(|arg| { .map(|arg| {
let span = arg.as_span(); let span = arg.as_span();
let needle = parse_function_argument_to_string(name, arg.into_inner())?; let needle = parse_function_argument_to_string(name, arg)?;
let path = RepoPath::parse_fs_path(ctx.cwd, ctx.workspace_root, &needle) let path = RepoPath::parse_fs_path(ctx.cwd, ctx.workspace_root, &needle)
.map_err(|e| { .map_err(|e| {
RevsetParseError::with_span( RevsetParseError::with_span(
@ -838,20 +838,19 @@ fn expect_one_optional_argument<'i>(
fn parse_function_argument_to_string( fn parse_function_argument_to_string(
name: &str, name: &str,
pairs: Pairs<Rule>, pair: Pair<Rule>,
) -> Result<String, RevsetParseError> { ) -> Result<String, RevsetParseError> {
let span = pair.as_span();
let workspace_ctx = None; // string literal shouldn't depend on workspace information let workspace_ctx = None; // string literal shouldn't depend on workspace information
let expression = parse_expression_rule(pairs.clone(), workspace_ctx)?; let expression = parse_expression_rule(pair.into_inner(), workspace_ctx)?;
match expression.as_ref() { match expression.as_ref() {
RevsetExpression::Symbol(symbol) => Ok(symbol.clone()), RevsetExpression::Symbol(symbol) => Ok(symbol.clone()),
_ => Err(RevsetParseError::new( _ => Err(RevsetParseError::with_span(
RevsetParseErrorKind::InvalidFunctionArguments { RevsetParseErrorKind::InvalidFunctionArguments {
name: name.to_string(), name: name.to_string(),
message: format!( message: "Expected function argument of type string".to_owned(),
"Expected function argument of type string, found: {}",
pairs.as_str()
),
}, },
span,
)), )),
} }
} }
@ -1917,7 +1916,7 @@ mod tests {
parse("description(heads())"), parse("description(heads())"),
Err(RevsetParseErrorKind::InvalidFunctionArguments { Err(RevsetParseErrorKind::InvalidFunctionArguments {
name: "description".to_string(), name: "description".to_string(),
message: "Expected function argument of type string, found: heads()".to_string() message: "Expected function argument of type string".to_string()
}) })
); );
assert_eq!( assert_eq!(

View file

@ -22,6 +22,16 @@ fn test_bad_function_call() {
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]); test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo"); let repo_path = test_env.env_root().join("repo");
let stderr = test_env.jj_cmd_failure(&repo_path, &["log", "-r", "file(a, not:a-string)"]);
insta::assert_snapshot!(stderr, @r###"
Error: Failed to parse revset: --> 1:9
|
1 | file(a, not:a-string)
| ^----------^
|
= Invalid arguments to revset function "file": Expected function argument of type string
"###);
let stderr = test_env.jj_cmd_failure(&repo_path, &["log", "-r", r#"file(a, "../out")"#]); let stderr = test_env.jj_cmd_failure(&repo_path, &["log", "-r", r#"file(a, "../out")"#]);
insta::assert_snapshot!(stderr, @r###" insta::assert_snapshot!(stderr, @r###"
Error: Failed to parse revset: --> 1:9 Error: Failed to parse revset: --> 1:9