Rollup merge of #81856 - Smittyvb:utf16-warn, r=matthewjasper

Suggest character encoding is incorrect when encountering random null bytes

This adds a note whenever null bytes are seen at the start of a token unexpectedly, since those tend to come from UTF-16 encoded files without a [BOM](https://en.wikipedia.org/wiki/Byte_order_mark) (if a UTF-16 BOM appears it won't be valid UTF-8, but if there is no BOM it be both valid UTF-16 and valid but garbled UTF-8). This approach was suggested in https://github.com/rust-lang/rust/issues/73979#issuecomment-653976451.

Closes #73979.
This commit is contained in:
Dylan DPC 2021-02-27 21:56:15 +01:00 committed by GitHub
commit be3d1eb301
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 3 additions and 0 deletions

View file

@ -268,6 +268,9 @@ impl<'a> StringReader<'a> {
// tokens like `<<` from `rustc_lexer`, and then add fancier error recovery to it,
// as there will be less overall work to do this way.
let token = unicode_chars::check_for_substitution(self, start, c, &mut err);
if c == '\x00' {
err.help("source files must contain UTF-8 encoded text, unexpected null bytes might occur when a different encoding is used");
}
err.emit();
token?
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.