Skip to content

convert \r\n -> \n in include_str! macro #63681

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

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 3 additions & 1 deletion src/libcore/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,9 @@ pub(crate) mod builtin {
/// modules are found)
///
/// This macro will yield an expression of type `&'static str` which is the
/// contents of the file.
/// contents of the file. The string is normalized:
/// * Byte Order Mark (BOM), if any, is removed,
/// * DOS line endings (`\r\n`) are converted to `\n`.
///
/// # Examples
///
Expand Down
16 changes: 6 additions & 10 deletions src/libsyntax_ext/source_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,12 @@ pub fn expand_include_str(cx: &mut ExtCtxt<'_>, sp: Span, tts: &[tokenstream::To
None => return DummyResult::any(sp)
};
let file = cx.resolve_path(file, sp);
match cx.source_map().load_binary_file(&file) {
Ok(bytes) => match std::str::from_utf8(&bytes) {
Ok(src) => {
let interned_src = Symbol::intern(&src);
base::MacEager::expr(cx.expr_str(sp, interned_src))
}
Err(_) => {
cx.span_err(sp, &format!("{} wasn't a utf-8 file", file.display()));
DummyResult::any(sp)
}
match cx.source_map().load_file(&file) {
Ok(source_file) => {
let src = source_file.src.as_ref()
.expect("freshly loaded file should have a source");
let interned_src = Symbol::intern(src.as_str());
base::MacEager::expr(cx.expr_str(sp, interned_src))
},
Err(e) => {
cx.span_err(sp, &format!("couldn't read {}: {}", file.display(), e));
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/include-macros/normalization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ fn main() {
);
assert_eq!(
include_str!("data.bin"),
"\u{FEFF}This file starts with BOM.\r\nLines are separated by \\r\\n.\r\n",
"This file starts with BOM.\nLines are separated by \\r\\n.\n",
Copy link
Contributor

Choose a reason for hiding this comment

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

"Lines are separated by \\n."

Copy link
Member Author

Choose a reason for hiding this comment

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

Nope, it should be \\r\\n, escaped \r is not changed in any way.

);
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ literal";
assert_eq!(s, "byte string\nliteral".as_bytes());

// validate that our source file has CRLF endings
let source = include_str!("lexer-crlf-line-endings-string-literal-doc-comment.rs");
let source = include_bytes!("lexer-crlf-line-endings-string-literal-doc-comment.rs");
let source = std::str::from_utf8(&source[..]).unwrap();
assert!(source.contains("string\r\nliteral"));
}