Skip to content

Commit 5d4bb05

Browse files
Merge #10374
10374: fix: Make `stringify!` insert/collapse whitespace when needed r=jonas-schievink a=jonas-schievink Fixes #10365 Unlike rustc, we don't insert newlines, but that should be fine. bors r+ Co-authored-by: Jonas Schievink <[email protected]>
2 parents f22eea9 + d05eae6 commit 5d4bb05

File tree

6 files changed

+48
-133
lines changed

6 files changed

+48
-133
lines changed

crates/hir_expand/src/builtin_macro.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Builtin macro
22
use crate::{
33
db::AstDatabase, name, quote, AstId, CrateId, MacroCallId, MacroCallLoc, MacroDefId,
4-
MacroDefKind, TextSize,
4+
MacroDefKind,
55
};
66

77
use base_db::{AnchoredPath, Edition, FileId};
@@ -148,25 +148,14 @@ fn line_expand(
148148
}
149149

150150
fn stringify_expand(
151-
db: &dyn AstDatabase,
152-
id: MacroCallId,
153-
_tt: &tt::Subtree,
151+
_db: &dyn AstDatabase,
152+
_id: MacroCallId,
153+
tt: &tt::Subtree,
154154
) -> ExpandResult<tt::Subtree> {
155-
let loc = db.lookup_intern_macro(id);
156-
157-
let macro_content = {
158-
let arg = match loc.kind.arg(db) {
159-
Some(arg) => arg,
160-
None => return ExpandResult::only_err(mbe::ExpandError::UnexpectedToken),
161-
};
162-
let macro_args = arg;
163-
let text = macro_args.text();
164-
let without_parens = TextSize::of('(')..text.len() - TextSize::of(')');
165-
text.slice(without_parens).to_string()
166-
};
155+
let pretty = tt::pretty(&tt.token_trees);
167156

168157
let expanded = quote! {
169-
#macro_content
158+
#pretty
170159
};
171160

172161
ExpandResult::ok(expanded)
@@ -685,7 +674,11 @@ mod tests {
685674
r#"
686675
#[rustc_builtin_macro]
687676
macro_rules! stringify {() => {}}
688-
stringify!(a b c)
677+
stringify!(
678+
a
679+
b
680+
c
681+
)
689682
"#,
690683
expect![["\"a b c\""]],
691684
);

crates/hir_expand/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use base_db::{impl_intern_key, salsa, CrateId, FileId, FileRange};
2626
use syntax::{
2727
algo::skip_trivia_token,
2828
ast::{self, AstNode, HasAttrs},
29-
Direction, SyntaxNode, SyntaxToken, TextRange, TextSize,
29+
Direction, SyntaxNode, SyntaxToken, TextRange,
3030
};
3131

3232
use crate::{

crates/proc_macro_srv/src/abis/abi_1_47/rustc_server.rs

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -199,44 +199,7 @@ pub mod token_stream {
199199

200200
impl ToString for TokenStream {
201201
fn to_string(&self) -> String {
202-
return tokentrees_to_text(&self.token_trees[..]);
203-
204-
fn tokentrees_to_text(tkns: &[tt::TokenTree]) -> String {
205-
tkns.iter()
206-
.fold((String::new(), true), |(last, last_to_joint), tkn| {
207-
let s = [last, tokentree_to_text(tkn)].join(if last_to_joint {
208-
""
209-
} else {
210-
" "
211-
});
212-
let mut is_joint = false;
213-
if let tt::TokenTree::Leaf(tt::Leaf::Punct(punct)) = tkn {
214-
if punct.spacing == tt::Spacing::Joint {
215-
is_joint = true;
216-
}
217-
}
218-
(s, is_joint)
219-
})
220-
.0
221-
}
222-
223-
fn tokentree_to_text(tkn: &tt::TokenTree) -> String {
224-
match tkn {
225-
tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) => ident.text.clone().into(),
226-
tt::TokenTree::Leaf(tt::Leaf::Literal(literal)) => literal.text.clone().into(),
227-
tt::TokenTree::Leaf(tt::Leaf::Punct(punct)) => format!("{}", punct.char),
228-
tt::TokenTree::Subtree(subtree) => {
229-
let content = tokentrees_to_text(&subtree.token_trees);
230-
let (open, close) = match subtree.delimiter.map(|it| it.kind) {
231-
None => ("", ""),
232-
Some(tt::DelimiterKind::Brace) => ("{", "}"),
233-
Some(tt::DelimiterKind::Parenthesis) => ("(", ")"),
234-
Some(tt::DelimiterKind::Bracket) => ("[", "]"),
235-
};
236-
format!("{}{}{}", open, content, close)
237-
}
238-
}
239-
}
202+
tt::pretty(&self.token_trees)
240203
}
241204
}
242205

crates/proc_macro_srv/src/abis/abi_1_55/rustc_server.rs

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -199,44 +199,7 @@ pub mod token_stream {
199199

200200
impl ToString for TokenStream {
201201
fn to_string(&self) -> String {
202-
return tokentrees_to_text(&self.token_trees[..]);
203-
204-
fn tokentrees_to_text(tkns: &[tt::TokenTree]) -> String {
205-
tkns.iter()
206-
.fold((String::new(), true), |(last, last_to_joint), tkn| {
207-
let s = [last, tokentree_to_text(tkn)].join(if last_to_joint {
208-
""
209-
} else {
210-
" "
211-
});
212-
let mut is_joint = false;
213-
if let tt::TokenTree::Leaf(tt::Leaf::Punct(punct)) = tkn {
214-
if punct.spacing == tt::Spacing::Joint {
215-
is_joint = true;
216-
}
217-
}
218-
(s, is_joint)
219-
})
220-
.0
221-
}
222-
223-
fn tokentree_to_text(tkn: &tt::TokenTree) -> String {
224-
match tkn {
225-
tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) => ident.text.clone().into(),
226-
tt::TokenTree::Leaf(tt::Leaf::Literal(literal)) => literal.text.clone().into(),
227-
tt::TokenTree::Leaf(tt::Leaf::Punct(punct)) => format!("{}", punct.char),
228-
tt::TokenTree::Subtree(subtree) => {
229-
let content = tokentrees_to_text(&subtree.token_trees);
230-
let (open, close) = match subtree.delimiter.map(|it| it.kind) {
231-
None => ("", ""),
232-
Some(tt::DelimiterKind::Brace) => ("{", "}"),
233-
Some(tt::DelimiterKind::Parenthesis) => ("(", ")"),
234-
Some(tt::DelimiterKind::Bracket) => ("[", "]"),
235-
};
236-
format!("{}{}{}", open, content, close)
237-
}
238-
}
239-
}
202+
tt::pretty(&self.token_trees)
240203
}
241204
}
242205

crates/proc_macro_srv/src/abis/abi_1_56/rustc_server.rs

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -199,44 +199,7 @@ pub mod token_stream {
199199

200200
impl ToString for TokenStream {
201201
fn to_string(&self) -> String {
202-
return tokentrees_to_text(&self.token_trees[..]);
203-
204-
fn tokentrees_to_text(tkns: &[tt::TokenTree]) -> String {
205-
tkns.iter()
206-
.fold((String::new(), true), |(last, last_to_joint), tkn| {
207-
let s = [last, tokentree_to_text(tkn)].join(if last_to_joint {
208-
""
209-
} else {
210-
" "
211-
});
212-
let mut is_joint = false;
213-
if let tt::TokenTree::Leaf(tt::Leaf::Punct(punct)) = tkn {
214-
if punct.spacing == tt::Spacing::Joint {
215-
is_joint = true;
216-
}
217-
}
218-
(s, is_joint)
219-
})
220-
.0
221-
}
222-
223-
fn tokentree_to_text(tkn: &tt::TokenTree) -> String {
224-
match tkn {
225-
tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) => ident.text.clone().into(),
226-
tt::TokenTree::Leaf(tt::Leaf::Literal(literal)) => literal.text.clone().into(),
227-
tt::TokenTree::Leaf(tt::Leaf::Punct(punct)) => format!("{}", punct.char),
228-
tt::TokenTree::Subtree(subtree) => {
229-
let content = tokentrees_to_text(&subtree.token_trees);
230-
let (open, close) = match subtree.delimiter.map(|it| it.kind) {
231-
None => ("", ""),
232-
Some(tt::DelimiterKind::Brace) => ("{", "}"),
233-
Some(tt::DelimiterKind::Parenthesis) => ("(", ")"),
234-
Some(tt::DelimiterKind::Bracket) => ("[", "]"),
235-
};
236-
format!("{}{}{}", open, content, close)
237-
}
238-
}
239-
}
202+
tt::pretty(&self.token_trees)
240203
}
241204
}
242205

crates/tt/src/lib.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,36 @@ impl Subtree {
274274
}
275275

276276
pub mod buffer;
277+
278+
pub fn pretty(tkns: &[TokenTree]) -> String {
279+
fn tokentree_to_text(tkn: &TokenTree) -> String {
280+
match tkn {
281+
TokenTree::Leaf(Leaf::Ident(ident)) => ident.text.clone().into(),
282+
TokenTree::Leaf(Leaf::Literal(literal)) => literal.text.clone().into(),
283+
TokenTree::Leaf(Leaf::Punct(punct)) => format!("{}", punct.char),
284+
TokenTree::Subtree(subtree) => {
285+
let content = pretty(&subtree.token_trees);
286+
let (open, close) = match subtree.delimiter.map(|it| it.kind) {
287+
None => ("", ""),
288+
Some(DelimiterKind::Brace) => ("{", "}"),
289+
Some(DelimiterKind::Parenthesis) => ("(", ")"),
290+
Some(DelimiterKind::Bracket) => ("[", "]"),
291+
};
292+
format!("{}{}{}", open, content, close)
293+
}
294+
}
295+
}
296+
297+
tkns.iter()
298+
.fold((String::new(), true), |(last, last_to_joint), tkn| {
299+
let s = [last, tokentree_to_text(tkn)].join(if last_to_joint { "" } else { " " });
300+
let mut is_joint = false;
301+
if let TokenTree::Leaf(Leaf::Punct(punct)) = tkn {
302+
if punct.spacing == Spacing::Joint {
303+
is_joint = true;
304+
}
305+
}
306+
(s, is_joint)
307+
})
308+
.0
309+
}

0 commit comments

Comments
 (0)