mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 14:21:44 +00:00
Escape fetched env vars in env! expansion
This commit is contained in:
parent
c0136070a7
commit
dcfda55c82
4 changed files with 31 additions and 26 deletions
|
@ -154,7 +154,7 @@ fn main() { file!(); }
|
||||||
#[rustc_builtin_macro]
|
#[rustc_builtin_macro]
|
||||||
macro_rules! file {() => {}}
|
macro_rules! file {() => {}}
|
||||||
|
|
||||||
fn main() { ""; }
|
fn main() { "file"; }
|
||||||
"##]],
|
"##]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,10 @@ use intern::sym;
|
||||||
use mbe::{parse_exprs_with_sep, parse_to_token_tree};
|
use mbe::{parse_exprs_with_sep, parse_to_token_tree};
|
||||||
use span::{Edition, Span, SpanAnchor, SyntaxContextId, ROOT_ERASED_FILE_AST_ID};
|
use span::{Edition, Span, SpanAnchor, SyntaxContextId, ROOT_ERASED_FILE_AST_ID};
|
||||||
use stdx::format_to;
|
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::{
|
use crate::{
|
||||||
db::ExpandDatabase,
|
db::ExpandDatabase,
|
||||||
|
@ -265,7 +268,7 @@ fn file_expand(
|
||||||
) -> ExpandResult<tt::Subtree> {
|
) -> ExpandResult<tt::Subtree> {
|
||||||
// FIXME: RA purposefully lacks knowledge of absolute file names
|
// FIXME: RA purposefully lacks knowledge of absolute file names
|
||||||
// so just return "".
|
// so just return "".
|
||||||
let file_name = "";
|
let file_name = "file";
|
||||||
|
|
||||||
let expanded = quote! {span =>
|
let expanded = quote! {span =>
|
||||||
#file_name
|
#file_name
|
||||||
|
@ -275,29 +278,9 @@ fn file_expand(
|
||||||
}
|
}
|
||||||
|
|
||||||
fn format_args_expand(
|
fn format_args_expand(
|
||||||
db: &dyn ExpandDatabase,
|
|
||||||
id: MacroCallId,
|
|
||||||
tt: &tt::Subtree,
|
|
||||||
span: Span,
|
|
||||||
) -> ExpandResult<tt::Subtree> {
|
|
||||||
format_args_expand_general(db, id, tt, "", span)
|
|
||||||
}
|
|
||||||
|
|
||||||
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,
|
_db: &dyn ExpandDatabase,
|
||||||
_id: MacroCallId,
|
_id: MacroCallId,
|
||||||
tt: &tt::Subtree,
|
tt: &tt::Subtree,
|
||||||
// FIXME: Make use of this so that mir interpretation works properly
|
|
||||||
_end_string: &str,
|
|
||||||
span: Span,
|
span: Span,
|
||||||
) -> ExpandResult<tt::Subtree> {
|
) -> ExpandResult<tt::Subtree> {
|
||||||
let pound = mk_pound(span);
|
let pound = mk_pound(span);
|
||||||
|
@ -308,6 +291,28 @@ fn format_args_expand_general(
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn format_args_nl_expand(
|
||||||
|
_db: &dyn ExpandDatabase,
|
||||||
|
_id: MacroCallId,
|
||||||
|
tt: &tt::Subtree,
|
||||||
|
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
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
fn asm_expand(
|
fn asm_expand(
|
||||||
_db: &dyn ExpandDatabase,
|
_db: &dyn ExpandDatabase,
|
||||||
_id: MacroCallId,
|
_id: MacroCallId,
|
||||||
|
@ -788,7 +793,7 @@ fn include_str_expand(
|
||||||
|
|
||||||
fn get_env_inner(db: &dyn ExpandDatabase, arg_id: MacroCallId, key: &str) -> Option<String> {
|
fn get_env_inner(db: &dyn ExpandDatabase, arg_id: MacroCallId, key: &str) -> Option<String> {
|
||||||
let krate = db.lookup_intern_macro_call(arg_id).krate;
|
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(
|
fn env_expand(
|
||||||
|
|
|
@ -703,7 +703,7 @@ fn infer_builtin_macros_file() {
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
!0..2 '""': &'static str
|
!0..6 '"file"': &'static str
|
||||||
63..87 '{ ...!(); }': ()
|
63..87 '{ ...!(); }': ()
|
||||||
73..74 'x': &'static str
|
73..74 'x': &'static str
|
||||||
"#]],
|
"#]],
|
||||||
|
|
|
@ -970,7 +970,7 @@ version = \"0.0.0\"
|
||||||
|
|
||||||
fn out_dirs_check_impl(root_contains_symlink: bool) {
|
fn out_dirs_check_impl(root_contains_symlink: bool) {
|
||||||
if skip_slow_tests() {
|
if skip_slow_tests() {
|
||||||
return;
|
// return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut server = Project::with_fixture(
|
let mut server = Project::with_fixture(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue