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

[hlsl-out] Fix fallthrough in switch statements (for FXC) #1920

Merged
merged 1 commit into from
May 30, 2022

Conversation

teoxoy
Copy link
Member

@teoxoy teoxoy commented May 14, 2022

Fixes FXC error X3537: Fall-throughs in switch statements are not allowed by emitting the statements of the following case blocks (the ones the fallthrough was supposed to reach).

Copy link
Member

@jimblandy jimblandy left a comment

Choose a reason for hiding this comment

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

Looks good. Just one suggestion.

Comment on lines +1668 to +1682
let curr_len = i + 1;
let end_case_idx = curr_len
+ cases
.iter()
.skip(curr_len)
.position(|case| !case.fall_through)
.unwrap();
let indent_level_3 = indent_level_2.next();
for case in &cases[i..=end_case_idx] {
writeln!(self.out, "{}{{", indent_level_2)?;
for sta in case.body.iter() {
self.write_stmt(module, sta, func_ctx, indent_level_3)?;
}
writeln!(self.out, "{}}}", indent_level_2)?;
}
Copy link
Member

Choose a reason for hiding this comment

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

I think this could be simplified a bit:

Suggested change
let curr_len = i + 1;
let end_case_idx = curr_len
+ cases
.iter()
.skip(curr_len)
.position(|case| !case.fall_through)
.unwrap();
let indent_level_3 = indent_level_2.next();
for case in &cases[i..=end_case_idx] {
writeln!(self.out, "{}{{", indent_level_2)?;
for sta in case.body.iter() {
self.write_stmt(module, sta, func_ctx, indent_level_3)?;
}
writeln!(self.out, "{}}}", indent_level_2)?;
}
let rest = &cases[i..];
let fallthrough_count = rest
.iter()
.position(|case| !case.fall_through)
.unwrap();
// Include the final, non-`fall_through` case.
let fallthrough = &rest[..=fallthrough_count];
let indent_level_3 = indent_level_2.next();
for case in fallthrough {
writeln!(self.out, "{}{{", indent_level_2)?;
for sta in case.body.iter() {
self.write_stmt(module, sta, func_ctx, indent_level_3)?;
}
writeln!(self.out, "{}}}", indent_level_2)?;
}

Copy link
Member Author

Choose a reason for hiding this comment

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

I find the naming to be slightly more confusing here since rest and fallthrough contain the first case as well. Getting fallthrough_count should also skip the first element (since it's the first case) and add one at the end.

I tried to make it more clear as well without much luck...

Comment on lines +1684 to +1687
let last_case = &cases[end_case_idx];
if last_case.body.last().map_or(true, |s| !s.is_terminator()) {
writeln!(self.out, "{}break;", indent_level_2)?;
}
Copy link
Member

Choose a reason for hiding this comment

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

Pair with the above suggestion:

Suggested change
let last_case = &cases[end_case_idx];
if last_case.body.last().map_or(true, |s| !s.is_terminator()) {
writeln!(self.out, "{}break;", indent_level_2)?;
}
let last_case = &fallthrough[fallthrough_count];
if last_case.body.last().map_or(true, |s| !s.is_terminator()) {
writeln!(self.out, "{}break;", indent_level_2)?;
}

@jimblandy
Copy link
Member

You know, I should just keep minor style stuff like that to myself. Merging as posted.

@jimblandy jimblandy merged commit 91ee407 into gfx-rs:master May 30, 2022
@teoxoy teoxoy deleted the fix-fxc-fallthrough branch May 30, 2022 20:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants