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

fix(for): correct semantics for "for" without "in" #348

Merged
merged 1 commit into from
Jan 21, 2025
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
68 changes: 38 additions & 30 deletions brush-core/src/interp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,54 +555,62 @@ impl Execute for ast::ForClauseCommand {
) -> Result<ExecutionResult, error::Error> {
let mut result = ExecutionResult::success();

// If we were given explicit words to iterate over, then expand them all, with splitting
// enabled.
let mut expanded_values = vec![];
if let Some(unexpanded_values) = &self.values {
// Expand all values, with splitting enabled.
let mut expanded_values = vec![];
for value in unexpanded_values {
let mut expanded = expansion::full_expand_and_split_word(shell, value).await?;
expanded_values.append(&mut expanded);
}
} else {
// Otherwise, we use the current positional parameters.
expanded_values.extend_from_slice(&shell.positional_parameters);
}

for value in expanded_values {
if shell.options.print_commands_and_arguments {
for value in expanded_values {
if shell.options.print_commands_and_arguments {
if let Some(unexpanded_values) = &self.values {
shell.trace_command(std::format!(
"for {} in {}",
self.variable_name,
unexpanded_values.iter().join(" ")
))?;
} else {
shell.trace_command(std::format!("for {}", self.variable_name,))?;
Copy link
Preview

Copilot AI Jan 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The trace message for the case without 'in' is unclear. It should indicate that positional parameters are being used. Suggestion: shell.trace_command(std::format!("for {} (using positional parameters)", self.variable_name))?;

Suggested change
shell.trace_command(std::format!("for {}", self.variable_name,))?;
shell.trace_command(std::format!("for {} (using positional parameters)", self.variable_name))?;

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
}
}

// Update the variable.
shell.env.update_or_add(
&self.variable_name,
ShellValueLiteral::Scalar(value),
|_| Ok(()),
EnvironmentLookup::Anywhere,
EnvironmentScope::Global,
)?;
// Update the variable.
shell.env.update_or_add(
&self.variable_name,
ShellValueLiteral::Scalar(value),
|_| Ok(()),
EnvironmentLookup::Anywhere,
EnvironmentScope::Global,
)?;

result = self.body.0.execute(shell, params).await?;
if result.exit_shell || result.return_from_function_or_script {
break;
}
result = self.body.0.execute(shell, params).await?;
if result.exit_shell || result.return_from_function_or_script {
break;
}

if let Some(continue_count) = &result.continue_loop {
if *continue_count == 0 {
result.continue_loop = None;
} else {
result.continue_loop = Some(*continue_count - 1);
break;
}
}
if let Some(break_count) = &result.break_loop {
if *break_count == 0 {
result.break_loop = None;
} else {
result.break_loop = Some(*break_count - 1);
}
if let Some(continue_count) = &result.continue_loop {
if *continue_count == 0 {
result.continue_loop = None;
} else {
result.continue_loop = Some(*continue_count - 1);
break;
}
}
if let Some(break_count) = &result.break_loop {
if *break_count == 0 {
result.break_loop = None;
} else {
result.break_loop = Some(*break_count - 1);
}
break;
}
}

shell.last_exit_status = result.exit_code;
Expand Down
26 changes: 26 additions & 0 deletions brush-shell/tests/cases/compound_cmds/for.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,36 @@ cases:
stdin: |
for f in 1 2 3; do echo $f; done

- name: "Multi-line for loop"
stdin: |
for f in 1 2 3; do
echo $f
done

- name: "Empty for loop"
stdin: |
for f in; do echo $f; done

- name: "for loop without in"
stdin: |
set -- a b c

echo "Loop 1"
for f; do echo $f; done

echo "Loop 2: no semicolon"
for f do echo $f; done

- name: "for loop without in but spaces"
stdin: |
set -- a "b c" d

echo "Loop 1"
for f; do echo $f; done

echo "Loop 2: no semicolon"
for f do echo $f; done

- name: "Break in for loop"
stdin: |
for f in 1 2 3; do
Expand Down
Loading