revset, templater: include parameter names in AliasId

I'm going to add arity-based alias overloading, and we'll need function
(name, arity) pair to identify it in alias expansion stack. The exact parameter
names aren't necessary, but they can be embedded in error messages.
This commit is contained in:
Yuya Nishihara 2024-06-05 22:29:10 +09:00
parent 03a0921e12
commit d38c9e86e8
5 changed files with 26 additions and 18 deletions

View file

@ -220,7 +220,7 @@ impl AliasExpandError for TemplateParseError {
fn within_alias_expansion(self, id: AliasId<'_>, span: pest::Span<'_>) -> Self {
let kind = match id {
AliasId::Symbol(_) | AliasId::Function(_) => {
AliasId::Symbol(_) | AliasId::Function(..) => {
TemplateParseErrorKind::BadAliasExpansion(id.to_string())
}
AliasId::Parameter(_) => TemplateParseErrorKind::BadParameterExpansion(id.to_string()),
@ -1035,7 +1035,7 @@ mod tests {
assert_eq!(defn, r#""is symbol""#);
let (id, params, defn) = aliases_map.get_function("func").unwrap();
assert_eq!(id, AliasId::Function("func"));
assert_eq!(id, AliasId::Function("func", &["a".to_owned()]));
assert_eq!(params, ["a"]);
assert_eq!(defn, r#""is function""#);
@ -1219,7 +1219,7 @@ mod tests {
.parse("F(a)")
.unwrap_err()
.kind,
TemplateParseErrorKind::BadAliasExpansion("F()".to_owned()),
TemplateParseErrorKind::BadAliasExpansion("F(x)".to_owned()),
);
}
}

View file

@ -372,14 +372,14 @@ fn test_alias() {
let stderr = test_env.jj_cmd_failure(&repo_path, &["log", "-r", "my_author(none())"]);
insta::assert_snapshot!(stderr, @r###"
Error: Failed to parse revset: Alias "my_author()" cannot be expanded
Error: Failed to parse revset: Alias "my_author(x)" cannot be expanded
Caused by:
1: --> 1:1
|
1 | my_author(none())
| ^---------------^
|
= Alias "my_author()" cannot be expanded
= Alias "my_author(x)" cannot be expanded
2: --> 1:8
|
1 | author(x)

View file

@ -84,14 +84,14 @@ fn test_templater_parse_error() {
Hint: Did you mean "s", "self"?
"###);
insta::assert_snapshot!(render_err(r#"format_id(commit_id)"#), @r###"
Error: Failed to parse template: Alias "format_id()" cannot be expanded
Error: Failed to parse template: Alias "format_id(id)" cannot be expanded
Caused by:
1: --> 1:1
|
1 | format_id(commit_id)
| ^------------------^
|
= Alias "format_id()" cannot be expanded
= Alias "format_id(id)" cannot be expanded
2: --> 1:4
|
1 | id.sort()
@ -189,14 +189,14 @@ fn test_templater_alias() {
"###);
insta::assert_snapshot!(render_err(r#"identity(identity(commit_id.short("")))"#), @r###"
Error: Failed to parse template: Alias "identity()" cannot be expanded
Error: Failed to parse template: Alias "identity(x)" cannot be expanded
Caused by:
1: --> 1:1
|
1 | identity(identity(commit_id.short("")))
| ^-------------------------------------^
|
= Alias "identity()" cannot be expanded
= Alias "identity(x)" cannot be expanded
2: --> 1:1
|
1 | x
@ -208,7 +208,7 @@ fn test_templater_alias() {
1 | identity(identity(commit_id.short("")))
| ^---------------------------^
|
= Alias "identity()" cannot be expanded
= Alias "identity(x)" cannot be expanded
4: --> 1:1
|
1 | x
@ -272,14 +272,14 @@ fn test_templater_alias() {
"###);
insta::assert_snapshot!(render_err(r#"coalesce(label("x", "not boolean"), "")"#), @r###"
Error: Failed to parse template: Alias "coalesce()" cannot be expanded
Error: Failed to parse template: Alias "coalesce(x, y)" cannot be expanded
Caused by:
1: --> 1:1
|
1 | coalesce(label("x", "not boolean"), "")
| ^-------------------------------------^
|
= Alias "coalesce()" cannot be expanded
= Alias "coalesce(x, y)" cannot be expanded
2: --> 1:4
|
1 | if(x, x, y)

View file

@ -405,7 +405,13 @@ impl<P> AliasesMap<P> {
pub fn get_function(&self, name: &str) -> Option<(AliasId<'_>, &[String], &str)> {
self.function_aliases
.get_key_value(name)
.map(|(name, (params, defn))| (AliasId::Function(name), params.as_ref(), defn.as_ref()))
.map(|(name, (params, defn))| {
(
AliasId::Function(name, params),
params.as_ref(),
defn.as_ref(),
)
})
}
}
@ -414,8 +420,8 @@ impl<P> AliasesMap<P> {
pub enum AliasId<'a> {
/// Symbol name.
Symbol(&'a str),
/// Function name.
Function(&'a str),
/// Function name and parameter names.
Function(&'a str, &'a [String]),
/// Function parameter name.
Parameter(&'a str),
}
@ -424,7 +430,9 @@ impl fmt::Display for AliasId<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
AliasId::Symbol(name) => write!(f, "{name}"),
AliasId::Function(name) => write!(f, "{name}()"),
AliasId::Function(name, params) => {
write!(f, "{name}({params})", params = params.join(", "))
}
AliasId::Parameter(name) => write!(f, "{name}"),
}
}

View file

@ -233,7 +233,7 @@ impl AliasExpandError for RevsetParseError {
fn within_alias_expansion(self, id: AliasId<'_>, span: pest::Span<'_>) -> Self {
let kind = match id {
AliasId::Symbol(_) | AliasId::Function(_) => {
AliasId::Symbol(_) | AliasId::Function(..) => {
RevsetParseErrorKind::BadAliasExpansion(id.to_string())
}
AliasId::Parameter(_) => RevsetParseErrorKind::BadParameterExpansion(id.to_string()),
@ -1632,7 +1632,7 @@ mod tests {
.parse("F(a)")
.unwrap_err()
.kind,
RevsetParseErrorKind::BadAliasExpansion("F()".to_owned())
RevsetParseErrorKind::BadAliasExpansion("F(x)".to_owned())
);
}
}