Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default values may reference earlier parameters #2382

Merged
merged 1 commit into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/recipe_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ impl<'src: 'run, 'run> RecipeResolver<'src, 'run> {
}

for recipe in resolver.resolved_recipes.values() {
for parameter in &recipe.parameters {
for (i, parameter) in recipe.parameters.iter().enumerate() {
if let Some(expression) = &parameter.default {
for variable in expression.variables() {
resolver.resolve_variable(&variable, &[])?;
resolver.resolve_variable(&variable, &recipe.parameters[..i])?;
}
}
}
Expand Down Expand Up @@ -63,11 +63,12 @@ impl<'src: 'run, 'run> RecipeResolver<'src, 'run> {
parameters: &[Parameter],
) -> CompileResult<'src> {
let name = variable.lexeme();
let undefined = !self.assignments.contains_key(name)
&& !parameters.iter().any(|p| p.name.lexeme() == name)
&& !constants().contains_key(name);

if undefined {
let defined = self.assignments.contains_key(name)
|| parameters.iter().any(|p| p.name.lexeme() == name)
|| constants().contains_key(name);

if !defined {
return Err(variable.error(UndefinedVariable { variable: name }));
}

Expand Down
1 change: 1 addition & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ mod no_cd;
mod no_dependencies;
mod no_exit_message;
mod os_attributes;
mod parameters;
mod parser;
mod positional_arguments;
mod private;
Expand Down
20 changes: 0 additions & 20 deletions tests/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1941,26 +1941,6 @@ test! {
shell: false,
}

test! {
name: parameter_cross_reference_error,
justfile: "
foo:

bar a b=a:
",
args: (),
stdout: "",
stderr: "
error: Variable `a` not defined
——▶ justfile:3:9
3 │ bar a b=a:
│ ^
",
status: EXIT_FAILURE,
shell: false,
}

#[cfg(windows)]
test! {
name: pwsh_invocation_directory,
Expand Down
38 changes: 38 additions & 0 deletions tests/parameters.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use super::*;

#[test]
fn parameter_default_values_may_use_earlier_parameters() {
Test::new()
.justfile(
"
@foo a b=a:
echo {{ b }}
",
)
.args(["foo", "bar"])
.stdout("bar\n")
.run();
}

#[test]
fn parameter_default_values_may_not_use_later_parameters() {
Test::new()
.justfile(
"
@foo a b=c c='':
echo {{ b }}
",
)
.args(["foo", "bar"])
.stderr(
"
error: Variable `c` not defined
——▶ justfile:1:10
1 │ @foo a b=c c='':
│ ^
",
)
.status(EXIT_FAILURE)
.run();
}