Skip to content

Commit

Permalink
Escape fetched env vars in env! expansion
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Jul 15, 2024
1 parent c013607 commit dcfda55
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ fn main() { file!(); }
#[rustc_builtin_macro]
macro_rules! file {() => {}}
fn main() { ""; }
fn main() { "file"; }
"##]],
);
}
Expand Down
39 changes: 22 additions & 17 deletions crates/hir-expand/src/builtin_fn_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ use intern::sym;
use mbe::{parse_exprs_with_sep, parse_to_token_tree};
use span::{Edition, Span, SpanAnchor, SyntaxContextId, ROOT_ERASED_FILE_AST_ID};
use stdx::format_to;
use syntax::unescape::{unescape_byte, unescape_char, unescape_unicode, Mode};
use syntax::{
format_smolstr,
unescape::{unescape_byte, unescape_char, unescape_unicode, Mode},
};

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

let expanded = quote! {span =>
#file_name
Expand All @@ -275,34 +278,36 @@ fn file_expand(
}

fn format_args_expand(
db: &dyn ExpandDatabase,
id: MacroCallId,
_db: &dyn ExpandDatabase,
_id: MacroCallId,
tt: &tt::Subtree,
span: Span,
) -> ExpandResult<tt::Subtree> {
format_args_expand_general(db, id, tt, "", span)
let pound = mk_pound(span);
let mut tt = tt.clone();
tt.delimiter.kind = tt::DelimiterKind::Parenthesis;
ExpandResult::ok(quote! {span =>
builtin #pound format_args #tt
})
}

fn format_args_nl_expand(
db: &dyn ExpandDatabase,
id: MacroCallId,
tt: &tt::Subtree,
span: Span,
) -> ExpandResult<tt::Subtree> {
format_args_expand_general(db, id, tt, "\\n", span)
}

fn format_args_expand_general(
_db: &dyn ExpandDatabase,
_id: MacroCallId,
tt: &tt::Subtree,
// FIXME: Make use of this so that mir interpretation works properly
_end_string: &str,
span: Span,
) -> ExpandResult<tt::Subtree> {
let pound = mk_pound(span);
let mut tt = tt.clone();
tt.delimiter.kind = tt::DelimiterKind::Parenthesis;
if let Some(tt::TokenTree::Leaf(tt::Leaf::Literal(tt::Literal {
text,
kind: tt::LitKind::Str,
..
}))) = tt.token_trees.first_mut()
{
*text = format_smolstr!("{text}\\n");
}
ExpandResult::ok(quote! {span =>
builtin #pound format_args #tt
})
Expand Down Expand Up @@ -788,7 +793,7 @@ fn include_str_expand(

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

fn env_expand(
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/tests/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ fn infer_builtin_macros_file() {
}
"#,
expect![[r#"
!0..2 '""': &'static str
!0..6 '"file"': &'static str
63..87 '{ ...!(); }': ()
73..74 'x': &'static str
"#]],
Expand Down
2 changes: 1 addition & 1 deletion crates/rust-analyzer/tests/slow-tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,7 @@ version = \"0.0.0\"

fn out_dirs_check_impl(root_contains_symlink: bool) {
if skip_slow_tests() {
return;
// return;
}

let mut server = Project::with_fixture(
Expand Down

0 comments on commit dcfda55

Please sign in to comment.