mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-02 22:54:58 +00:00
Implement dummy assert macro
This commit is contained in:
parent
bddf6b5266
commit
759bcea96d
4 changed files with 60 additions and 6 deletions
|
@ -88,6 +88,7 @@ register_builtin! {
|
|||
(compile_error, CompileError) => compile_error_expand,
|
||||
(file, File) => file_expand,
|
||||
(line, Line) => line_expand,
|
||||
(assert, Assert) => assert_expand,
|
||||
(stringify, Stringify) => stringify_expand,
|
||||
(format_args, FormatArgs) => format_args_expand,
|
||||
// format_args_nl only differs in that it adds a newline in the end,
|
||||
|
@ -151,6 +152,45 @@ fn column_expand(
|
|||
Ok(expanded)
|
||||
}
|
||||
|
||||
fn assert_expand(
|
||||
_db: &dyn AstDatabase,
|
||||
_id: LazyMacroId,
|
||||
tt: &tt::Subtree,
|
||||
) -> Result<tt::Subtree, mbe::ExpandError> {
|
||||
// A hacky implementation for goto def and hover
|
||||
// We expand `assert!("", arg1, arg2)` to
|
||||
// ```
|
||||
// {(&(arg1), &(arg2));}
|
||||
// ```,
|
||||
// which is wrong but useful.
|
||||
|
||||
let mut args = Vec::new();
|
||||
let mut current = Vec::new();
|
||||
for tt in tt.token_trees.iter().cloned() {
|
||||
match tt {
|
||||
tt::TokenTree::Leaf(tt::Leaf::Punct(p)) if p.char == ',' => {
|
||||
args.push(current);
|
||||
current = Vec::new();
|
||||
}
|
||||
_ => {
|
||||
current.push(tt);
|
||||
}
|
||||
}
|
||||
}
|
||||
if !current.is_empty() {
|
||||
args.push(current);
|
||||
}
|
||||
|
||||
let arg_tts = args.into_iter().flat_map(|arg| {
|
||||
quote! { &(##arg), }
|
||||
}.token_trees).collect::<Vec<_>>();
|
||||
|
||||
let expanded = quote! {
|
||||
{ { (##arg_tts); } }
|
||||
};
|
||||
Ok(expanded)
|
||||
}
|
||||
|
||||
fn file_expand(
|
||||
_db: &dyn AstDatabase,
|
||||
_id: LazyMacroId,
|
||||
|
@ -493,6 +533,22 @@ mod tests {
|
|||
assert_eq!(expanded, "\"\"");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_assert_expand() {
|
||||
let expanded = expand_builtin_macro(
|
||||
r#"
|
||||
#[rustc_builtin_macro]
|
||||
macro_rules! assert {
|
||||
($fmt:expr) => ({ /* compiler built-in */ });
|
||||
($fmt:expr, $($args:tt)*) => ({ /* compiler built-in */ })
|
||||
}
|
||||
assert!(true, "{} {:?}", arg1(a, b, c), arg2);
|
||||
"#,
|
||||
);
|
||||
|
||||
assert_eq!(expanded, "{{(&(true), &(\"{} {:?}\"), &(arg1(a,b,c)), &(arg2),);}}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_compile_error_expand() {
|
||||
let expanded = expand_builtin_macro(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue