Allow recipe parameters to shadow variables (#1480)

This commit is contained in:
Casey Rodarmor 2023-01-09 16:59:02 -08:00 committed by GitHub
parent 9b6b0b7fac
commit af54dafa77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 24 additions and 42 deletions

View File

@ -97,16 +97,6 @@ impl<'src> Analyzer<'src> {
let recipes = RecipeResolver::resolve_recipes(self.recipes, &assignments)?; let recipes = RecipeResolver::resolve_recipes(self.recipes, &assignments)?;
for recipe in recipes.values() {
for parameter in &recipe.parameters {
if assignments.contains_key(parameter.name.lexeme()) {
return Err(parameter.name.token().error(ParameterShadowsVariable {
parameter: parameter.name.lexeme(),
}));
}
}
}
let mut aliases = Table::new(); let mut aliases = Table::new();
while let Some(alias) = self.aliases.pop() { while let Some(alias) = self.aliases.pop() {
aliases.insert(Self::resolve_alias(&recipes, alias)?); aliases.insert(Self::resolve_alias(&recipes, alias)?);
@ -316,16 +306,6 @@ mod tests {
kind: DuplicateParameter{recipe: "a", parameter: "b"}, kind: DuplicateParameter{recipe: "a", parameter: "b"},
} }
analysis_error! {
name: parameter_shadows_variable,
input: "foo := \"h\"\na foo:",
offset: 13,
line: 1,
column: 2,
width: 3,
kind: ParameterShadowsVariable{parameter: "foo"},
}
analysis_error! { analysis_error! {
name: duplicate_recipe, name: duplicate_recipe,
input: "a:\nb:\na:", input: "a:\nb:\na:",

View File

@ -226,12 +226,6 @@ impl Display for CompileError<'_> {
ParameterFollowsVariadicParameter { parameter } => { ParameterFollowsVariadicParameter { parameter } => {
write!(f, "Parameter `{parameter}` follows variadic parameter")?; write!(f, "Parameter `{parameter}` follows variadic parameter")?;
} }
ParameterShadowsVariable { parameter } => {
write!(
f,
"Parameter `{parameter}` shadows variable of the same name",
)?;
}
ParsingRecursionDepthExceeded => { ParsingRecursionDepthExceeded => {
write!(f, "Parsing recursion depth exceeded")?; write!(f, "Parsing recursion depth exceeded")?;
} }

View File

@ -79,9 +79,6 @@ pub(crate) enum CompileErrorKind<'src> {
ParameterFollowsVariadicParameter { ParameterFollowsVariadicParameter {
parameter: &'src str, parameter: &'src str,
}, },
ParameterShadowsVariable {
parameter: &'src str,
},
ParsingRecursionDepthExceeded, ParsingRecursionDepthExceeded,
RequiredParameterFollowsDefaultParameter { RequiredParameterFollowsDefaultParameter {
parameter: &'src str, parameter: &'src str,

View File

@ -72,6 +72,7 @@ mod recursion_limit;
mod regexes; mod regexes;
mod run; mod run;
mod search; mod search;
mod shadowing_parameters;
mod shebang; mod shebang;
mod shell; mod shell;
mod show; mod show;

View File

@ -1151,19 +1151,6 @@ c: b a
stdout: "", stdout: "",
} }
test! {
name: parameter_shadows_variable,
justfile: "FOO := 'hello'\na FOO:",
args: ("a"),
stdout: "",
stderr: "error: Parameter `FOO` shadows variable of the same name
|
2 | a FOO:
| ^^^
",
status: EXIT_FAILURE,
}
test! { test! {
name: unknown_function_in_assignment, name: unknown_function_in_assignment,
justfile: r#"foo := foo() + "hello" justfile: r#"foo := foo() + "hello"

View File

@ -0,0 +1,23 @@
test! {
name: parameter_may_shadow_variable,
justfile: "FOO := 'hello'\na FOO:\n echo {{FOO}}\n",
args: ("a", "bar"),
stdout: "bar\n",
stderr: "echo bar\n",
}
test! {
name: shadowing_parameters_do_not_change_environment,
justfile: "export FOO := 'hello'\na FOO:\n echo $FOO\n",
args: ("a", "bar"),
stdout: "hello\n",
stderr: "echo $FOO\n",
}
test! {
name: exporting_shadowing_parameters_does_change_environment,
justfile: "export FOO := 'hello'\na $FOO:\n echo $FOO\n",
args: ("a", "bar"),
stdout: "bar\n",
stderr: "echo $FOO\n",
}