Auto merge of #17279 - Veykril:format_args-escape, r=Veykril · rust-lang/rust@d9dda8f

3 files changed

lines changed

Original file line numberDiff line numberDiff line change

@@ -17,7 +17,7 @@ use syntax::{

1717

self, ArrayExprKind, AstChildren, BlockExpr, HasArgList, HasAttrs, HasLoopBody, HasName,

1818

RangeItem, SlicePatComponents,

1919

},

20-

AstNode, AstPtr, SyntaxNodePtr,

20+

AstNode, AstPtr, AstToken as _, SyntaxNodePtr,

2121

};

2222

use triomphe::Arc;

2323

@@ -1577,7 +1577,13 @@ impl ExprCollector<'_> {

15771577

});

15781578

});

15791579

let template = f.template();

1580-

let fmt_snippet = template.as_ref().map(ToString::to_string);

1580+

let fmt_snippet = template.as_ref().and_then(|it| match it {

1581+

ast::Expr::Literal(literal) => match literal.kind() {

1582+

ast::LiteralKind::String(s) => Some(s.text().to_owned()),

1583+

_ => None,

1584+

},

1585+

_ => None,

1586+

});

15811587

let mut mappings = vec![];

15821588

let fmt = match template.and_then(|it| self.expand_macros_to_string(it)) {

15831589

Some((s, is_direct_literal)) => format_args::parse(

Original file line numberDiff line numberDiff line change

@@ -150,7 +150,7 @@ fn desugar_builtin_format_args() {

150150

fn main() {

151151

let are = "are";

152152

let count = 10;

153-

builtin#format_args("hello {count:02} {} friends, we {are:?} {0}{last}", "fancy", last = "!");

153+

builtin#format_args("\u{1b}hello {count:02} {} friends, we {are:?} {0}{last}", "fancy", last = "!");

154154

}

155155

"#,

156156

);

@@ -161,7 +161,7 @@ fn main() {

161161

let count = 10;

162162

builtin#lang(Arguments::new_v1_formatted)(

163163

&[

164-

"hello ", " ", " friends, we ", " ", "",

164+

"\u{1b}hello ", " ", " friends, we ", " ", "",

165165

],

166166

&[

167167

builtin#lang(Argument::new_display)(

Original file line numberDiff line numberDiff line change

@@ -175,7 +175,13 @@ pub(crate) fn parse(

175175

mut synth: impl FnMut(Name) -> ExprId,

176176

mut record_usage: impl FnMut(Name, Option<TextRange>),

177177

) -> FormatArgs {

178-

let text = s.text_without_quotes();

178+

let Ok(text) = s.value() else {

179+

return FormatArgs {

180+

template: Default::default(),

181+

arguments: args.finish(),

182+

orphans: vec![],

183+

};

184+

};

179185

let str_style = match s.quote_offsets() {

180186

Some(offsets) => {

181187

let raw = usize::from(offsets.quotes.0.len()) - 1;

@@ -186,7 +192,7 @@ pub(crate) fn parse(

186192

};

187193
188194

let mut parser =

189-

parse::Parser::new(text, str_style, fmt_snippet, false, parse::ParseMode::Format);

195+

parse::Parser::new(&text, str_style, fmt_snippet, false, parse::ParseMode::Format);

190196
191197

let mut pieces = Vec::new();

192198

while let Some(piece) = parser.next() {