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

parse_format: treat r" as a literal #81828

Merged
merged 1 commit into from
Feb 8, 2021
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
2 changes: 1 addition & 1 deletion compiler/rustc_parse_format/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ fn find_skips_from_snippet(
str_style: Option<usize>,
) -> (Vec<usize>, bool) {
let snippet = match snippet {
Some(ref s) if s.starts_with('"') || s.starts_with("r#") => s,
Some(ref s) if s.starts_with('"') || s.starts_with("r\"") || s.starts_with("r#") => s,
_ => return (vec![], false),
};

Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/fmt/format-args-capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
fn main() {
named_argument_takes_precedence_to_captured();
formatting_parameters_can_be_captured();
capture_raw_strings_and_idents();

#[cfg(panic = "unwind")]
{
Expand All @@ -25,6 +26,16 @@ fn named_argument_takes_precedence_to_captured() {
assert_eq!(&s, "positional-named-captured");
}

fn capture_raw_strings_and_idents() {
let r#type = "apple";
let s = format!(r#"The fruit is an {type}"#);
assert_eq!(&s, "The fruit is an apple");

let r#type = "orange";
let s = format!(r"The fruit is an {type}");
assert_eq!(&s, "The fruit is an orange");
}

#[cfg(panic = "unwind")]
fn panic_with_single_argument_does_not_get_formatted() {
// panic! with a single argument does not perform string formatting.
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/issues/issue-75307.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: invalid reference to positional arguments 1 and 2 (there is 1 argument)
--> $DIR/issue-75307.rs:2:13
--> $DIR/issue-75307.rs:2:17
|
LL | format!(r"{}{}{}", named_arg=1);
| ^^^^^^^^^
| ^^^^
|
= note: positional arguments are zero-based

Expand Down