Rollup merge of #81828 - davidhewitt:capture-raw-format-strings, r=estebank
parse_format: treat r" as a literal This PR changes `format_args!` internal parsing machinery to treat raw strings starting `r"` as a literal. Currently `"` and `r#` are recognised as valid starting combinations for string literals, but `r"` is not. This was noticed when debugging https://github.com/rust-lang/rust/issues/67984#issuecomment-753413156 As well as fixing the behavior observed in that comment, this improves diagnostic spans for `r"` formatting strings.
This commit is contained in:
commit
b9045fabf8
@ -730,7 +730,7 @@ fn find_skips_from_snippet(
|
|||||||
str_style: Option<usize>,
|
str_style: Option<usize>,
|
||||||
) -> (Vec<usize>, bool) {
|
) -> (Vec<usize>, bool) {
|
||||||
let snippet = match snippet {
|
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),
|
_ => return (vec![], false),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
fn main() {
|
fn main() {
|
||||||
named_argument_takes_precedence_to_captured();
|
named_argument_takes_precedence_to_captured();
|
||||||
formatting_parameters_can_be_captured();
|
formatting_parameters_can_be_captured();
|
||||||
|
capture_raw_strings_and_idents();
|
||||||
|
|
||||||
#[cfg(panic = "unwind")]
|
#[cfg(panic = "unwind")]
|
||||||
{
|
{
|
||||||
@ -25,6 +26,16 @@ fn named_argument_takes_precedence_to_captured() {
|
|||||||
assert_eq!(&s, "positional-named-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")]
|
#[cfg(panic = "unwind")]
|
||||||
fn panic_with_single_argument_does_not_get_formatted() {
|
fn panic_with_single_argument_does_not_get_formatted() {
|
||||||
// panic! with a single argument does not perform string formatting.
|
// panic! with a single argument does not perform string formatting.
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
error: invalid reference to positional arguments 1 and 2 (there is 1 argument)
|
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);
|
LL | format!(r"{}{}{}", named_arg=1);
|
||||||
| ^^^^^^^^^
|
| ^^^^
|
||||||
|
|
|
|
||||||
= note: positional arguments are zero-based
|
= note: positional arguments are zero-based
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user