Skip to content

Escape control chars in source when printing diagnostics #8049

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

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from

Conversation

andyleiserson
Copy link
Contributor

Replace control characters when emitting diagnostics in naga.

The motivation here is that my fuzzing runs were making my terminals unhappy. In most cases, in a browser, they won't go to a terminal so it won't matter, but this seems like the right thing to do regardless.

Testing
Adds a unit test and a wgsl_errors test.

Squash or Rebase? Squash

Checklist

  • Run cargo fmt.
  • Run taplo format.
  • Run cargo clippy --tests. If applicable, add:
    • --target wasm32-unknown-unknown
  • Run cargo xtask test to run tests.
  • If this contains user-facing changes, add a CHANGELOG.md entry.

Copy link
Member

@ErichDonGubler ErichDonGubler left a comment

Choose a reason for hiding this comment

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

LGTM minus nits!

Comment on lines +148 to +153
let found_len = res[base + found_pos..].chars().next().unwrap().len_utf8();
res.to_mut().replace_range(
base + found_pos..base + found_pos + found_len,
REPLACEMENT_CHAR,
);
base += found_pos + REPLACEMENT_CHAR.len();
Copy link
Member

Choose a reason for hiding this comment

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

nitpick(non-blocking): I think it'd be easier to understand this code (including base += …) with base + found_pos factored out to a variable. Not a big deal, though.

@@ -137,3 +137,29 @@ where
Some(&self.inner)
}
}

pub(crate) fn replace_control_chars(s: &str) -> Cow<'_, str> {
const REPLACEMENT_CHAR: &str = "\u{FFFD}";
Copy link
Member

Choose a reason for hiding this comment

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

nitpick: We should use std::char::REPLACEMENT_CHARACTER instead of using this ad-hoc constant.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wanted to, but couldn't find a way to go from char to &str without a buffer.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, hmm. I suppose you could use a stack-allocated buffer and char::encode_utf8?

I'd be satisfied with merely a (doc) comment pointing to REPLACEMENT_CHARACTER expressing the type woe you're working around, too.

Copy link
Member

Choose a reason for hiding this comment

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

suggestion: Let's add a CHANGELOG entry. This isn't likely to be noticeable for most folks' shaders, but it's good to call out.

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