Skip to content

Commit 18c2491

Browse files
committed
Check single literal
1 parent 390540f commit 18c2491

File tree

2 files changed

+38
-19
lines changed

2 files changed

+38
-19
lines changed

crates/ra_hir/src/ty/tests.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4895,3 +4895,26 @@ fn main() {
48954895
"###
48964896
);
48974897
}
4898+
4899+
#[test]
4900+
fn infer_builtin_macros_compile_error() {
4901+
assert_snapshot!(
4902+
infer(r#"
4903+
#[rustc_builtin_macro]
4904+
macro_rules! compile_error {
4905+
($msg:expr) => ({ /* compiler built-in */ });
4906+
($msg:expr,) => ({ /* compiler built-in */ })
4907+
}
4908+
4909+
fn main() {
4910+
compile_error!("error!");
4911+
}
4912+
"#),
4913+
@r###"
4914+
![0; 14) 'loop{"error!"}': !
4915+
![4; 14) '{"error!"}': &str
4916+
![5; 13) '"error!"': &str
4917+
[166; 199) '{ ...!"); }': !
4918+
"###
4919+
);
4920+
}

crates/ra_hir_expand/src/builtin_macro.rs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -177,25 +177,21 @@ fn file_expand(
177177
}
178178

179179
fn compile_error_expand(
180-
db: &dyn AstDatabase,
181-
id: MacroCallId,
182-
_tt: &tt::Subtree,
180+
_db: &dyn AstDatabase,
181+
_id: MacroCallId,
182+
tt: &tt::Subtree,
183183
) -> Result<tt::Subtree, mbe::ExpandError> {
184-
let loc = db.lookup_intern_macro(id);
185-
let macro_call = loc.ast_id.to_node(db);
186-
187-
let macro_content = {
188-
// FIXME: verify that this is an ast::Literatal with kind() == LiteralKind.String, else error.
189-
let arg = macro_call.token_tree().ok_or_else(|| mbe::ExpandError::UnexpectedToken)?;
190-
let macro_args = arg.syntax().clone();
191-
let text = macro_args.text();
192-
let without_parens = TextUnit::of_char('(')..text.len() - TextUnit::of_char(')');
193-
text.slice(without_parens).to_string()
194-
};
195-
196-
let expanded = quote! {
197-
loop { #macro_content }
198-
};
184+
if tt.count() == 1 {
185+
match &tt.token_trees[0] {
186+
tt::TokenTree::Leaf(tt::Leaf::Literal(it)) => {
187+
let s = it.text.as_str();
188+
if s.starts_with(r#"""#) && s.ends_with(r#"""#) {
189+
return Ok(quote! { loop { #it }});
190+
}
191+
},
192+
_ => {},
193+
};
194+
}
199195

200-
Ok(expanded)
196+
Err(mbe::ExpandError::BindingError("Must be a string".into()))
201197
}

0 commit comments

Comments
 (0)