Skip to content

Commit dcfda55

Browse files
committed
Escape fetched env vars in env! expansion
1 parent c013607 commit dcfda55

File tree

4 files changed

+25
-20
lines changed

4 files changed

+25
-20
lines changed

crates/hir-def/src/macro_expansion_tests/builtin_fn_macro.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ fn main() { file!(); }
154154
#[rustc_builtin_macro]
155155
macro_rules! file {() => {}}
156156
157-
fn main() { ""; }
157+
fn main() { "file"; }
158158
"##]],
159159
);
160160
}

crates/hir-expand/src/builtin_fn_macro.rs

+22-17
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ use intern::sym;
88
use mbe::{parse_exprs_with_sep, parse_to_token_tree};
99
use span::{Edition, Span, SpanAnchor, SyntaxContextId, ROOT_ERASED_FILE_AST_ID};
1010
use stdx::format_to;
11-
use syntax::unescape::{unescape_byte, unescape_char, unescape_unicode, Mode};
11+
use syntax::{
12+
format_smolstr,
13+
unescape::{unescape_byte, unescape_char, unescape_unicode, Mode},
14+
};
1215

1316
use crate::{
1417
db::ExpandDatabase,
@@ -265,7 +268,7 @@ fn file_expand(
265268
) -> ExpandResult<tt::Subtree> {
266269
// FIXME: RA purposefully lacks knowledge of absolute file names
267270
// so just return "".
268-
let file_name = "";
271+
let file_name = "file";
269272

270273
let expanded = quote! {span =>
271274
#file_name
@@ -275,34 +278,36 @@ fn file_expand(
275278
}
276279

277280
fn format_args_expand(
278-
db: &dyn ExpandDatabase,
279-
id: MacroCallId,
281+
_db: &dyn ExpandDatabase,
282+
_id: MacroCallId,
280283
tt: &tt::Subtree,
281284
span: Span,
282285
) -> ExpandResult<tt::Subtree> {
283-
format_args_expand_general(db, id, tt, "", span)
286+
let pound = mk_pound(span);
287+
let mut tt = tt.clone();
288+
tt.delimiter.kind = tt::DelimiterKind::Parenthesis;
289+
ExpandResult::ok(quote! {span =>
290+
builtin #pound format_args #tt
291+
})
284292
}
285293

286294
fn format_args_nl_expand(
287-
db: &dyn ExpandDatabase,
288-
id: MacroCallId,
289-
tt: &tt::Subtree,
290-
span: Span,
291-
) -> ExpandResult<tt::Subtree> {
292-
format_args_expand_general(db, id, tt, "\\n", span)
293-
}
294-
295-
fn format_args_expand_general(
296295
_db: &dyn ExpandDatabase,
297296
_id: MacroCallId,
298297
tt: &tt::Subtree,
299-
// FIXME: Make use of this so that mir interpretation works properly
300-
_end_string: &str,
301298
span: Span,
302299
) -> ExpandResult<tt::Subtree> {
303300
let pound = mk_pound(span);
304301
let mut tt = tt.clone();
305302
tt.delimiter.kind = tt::DelimiterKind::Parenthesis;
303+
if let Some(tt::TokenTree::Leaf(tt::Leaf::Literal(tt::Literal {
304+
text,
305+
kind: tt::LitKind::Str,
306+
..
307+
}))) = tt.token_trees.first_mut()
308+
{
309+
*text = format_smolstr!("{text}\\n");
310+
}
306311
ExpandResult::ok(quote! {span =>
307312
builtin #pound format_args #tt
308313
})
@@ -788,7 +793,7 @@ fn include_str_expand(
788793

789794
fn get_env_inner(db: &dyn ExpandDatabase, arg_id: MacroCallId, key: &str) -> Option<String> {
790795
let krate = db.lookup_intern_macro_call(arg_id).krate;
791-
db.crate_graph()[krate].env.get(key)
796+
db.crate_graph()[krate].env.get(key).map(|it| it.escape_debug().to_string())
792797
}
793798

794799
fn env_expand(

crates/hir-ty/src/tests/macros.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ fn infer_builtin_macros_file() {
703703
}
704704
"#,
705705
expect![[r#"
706-
!0..2 '""': &'static str
706+
!0..6 '"file"': &'static str
707707
63..87 '{ ...!(); }': ()
708708
73..74 'x': &'static str
709709
"#]],

crates/rust-analyzer/tests/slow-tests/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -970,7 +970,7 @@ version = \"0.0.0\"
970970

971971
fn out_dirs_check_impl(root_contains_symlink: bool) {
972972
if skip_slow_tests() {
973-
return;
973+
// return;
974974
}
975975

976976
let mut server = Project::with_fixture(

0 commit comments

Comments
 (0)