mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-26 04:53:34 +00:00
Make compile_error!
lazy and emit a diagnostic
This commit is contained in:
parent
4634bfb332
commit
883c8d177d
3 changed files with 34 additions and 21 deletions
|
@ -84,6 +84,9 @@ macro_rules! env {}
|
||||||
#[rustc_builtin_macro]
|
#[rustc_builtin_macro]
|
||||||
macro_rules! include {}
|
macro_rules! include {}
|
||||||
|
|
||||||
|
#[rustc_builtin_macro]
|
||||||
|
macro_rules! compile_error {}
|
||||||
|
|
||||||
#[rustc_builtin_macro]
|
#[rustc_builtin_macro]
|
||||||
macro_rules! format_args {
|
macro_rules! format_args {
|
||||||
() => {}
|
() => {}
|
||||||
|
@ -103,6 +106,9 @@ fn f() {
|
||||||
env!("OUT_DIR");
|
env!("OUT_DIR");
|
||||||
//^^^^^^^^^^^^^^^ `OUT_DIR` not set, enable "load out dirs from check" to fix
|
//^^^^^^^^^^^^^^^ `OUT_DIR` not set, enable "load out dirs from check" to fix
|
||||||
|
|
||||||
|
compile_error!("compile_error works");
|
||||||
|
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `compile_error!` called: compile_error works
|
||||||
|
|
||||||
// Lazy:
|
// Lazy:
|
||||||
|
|
||||||
format_args!();
|
format_args!();
|
||||||
|
|
|
@ -86,7 +86,6 @@ pub fn find_builtin_macro(
|
||||||
register_builtin! {
|
register_builtin! {
|
||||||
LAZY:
|
LAZY:
|
||||||
(column, Column) => column_expand,
|
(column, Column) => column_expand,
|
||||||
(compile_error, CompileError) => compile_error_expand,
|
|
||||||
(file, File) => file_expand,
|
(file, File) => file_expand,
|
||||||
(line, Line) => line_expand,
|
(line, Line) => line_expand,
|
||||||
(assert, Assert) => assert_expand,
|
(assert, Assert) => assert_expand,
|
||||||
|
@ -97,6 +96,7 @@ register_builtin! {
|
||||||
(format_args_nl, FormatArgsNl) => format_args_expand,
|
(format_args_nl, FormatArgsNl) => format_args_expand,
|
||||||
|
|
||||||
EAGER:
|
EAGER:
|
||||||
|
(compile_error, CompileError) => compile_error_expand,
|
||||||
(concat, Concat) => concat_expand,
|
(concat, Concat) => concat_expand,
|
||||||
(include, Include) => include_expand,
|
(include, Include) => include_expand,
|
||||||
(include_bytes, IncludeBytes) => include_bytes_expand,
|
(include_bytes, IncludeBytes) => include_bytes_expand,
|
||||||
|
@ -213,25 +213,6 @@ fn file_expand(
|
||||||
ExpandResult::ok(expanded)
|
ExpandResult::ok(expanded)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compile_error_expand(
|
|
||||||
_db: &dyn AstDatabase,
|
|
||||||
_id: LazyMacroId,
|
|
||||||
tt: &tt::Subtree,
|
|
||||||
) -> ExpandResult<tt::Subtree> {
|
|
||||||
if tt.count() == 1 {
|
|
||||||
if let tt::TokenTree::Leaf(tt::Leaf::Literal(it)) = &tt.token_trees[0] {
|
|
||||||
let s = it.text.as_str();
|
|
||||||
if s.contains('"') {
|
|
||||||
return ExpandResult::ok(quote! { loop { #it }});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
ExpandResult::only_err(mbe::ExpandError::BindingError(
|
|
||||||
"`compile_error!` argument be a string".into(),
|
|
||||||
))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn format_args_expand(
|
fn format_args_expand(
|
||||||
_db: &dyn AstDatabase,
|
_db: &dyn AstDatabase,
|
||||||
_id: LazyMacroId,
|
_id: LazyMacroId,
|
||||||
|
@ -280,6 +261,30 @@ fn unquote_str(lit: &tt::Literal) -> Option<String> {
|
||||||
token.value().map(|it| it.into_owned())
|
token.value().map(|it| it.into_owned())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn compile_error_expand(
|
||||||
|
_db: &dyn AstDatabase,
|
||||||
|
_id: EagerMacroId,
|
||||||
|
tt: &tt::Subtree,
|
||||||
|
) -> ExpandResult<Option<(tt::Subtree, FragmentKind)>> {
|
||||||
|
let err = match &*tt.token_trees {
|
||||||
|
[tt::TokenTree::Leaf(tt::Leaf::Literal(it))] => {
|
||||||
|
let text = it.text.as_str();
|
||||||
|
if text.starts_with('"') && text.ends_with('"') {
|
||||||
|
// FIXME: does not handle raw strings
|
||||||
|
mbe::ExpandError::Other(format!(
|
||||||
|
"`compile_error!` called: {}",
|
||||||
|
&text[1..text.len() - 1]
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
mbe::ExpandError::BindingError("`compile_error!` argument must be a string".into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => mbe::ExpandError::BindingError("`compile_error!` argument must be a string".into()),
|
||||||
|
};
|
||||||
|
|
||||||
|
ExpandResult { value: Some((quote! {}, FragmentKind::Items)), err: Some(err) }
|
||||||
|
}
|
||||||
|
|
||||||
fn concat_expand(
|
fn concat_expand(
|
||||||
_db: &dyn AstDatabase,
|
_db: &dyn AstDatabase,
|
||||||
_arg_id: EagerMacroId,
|
_arg_id: EagerMacroId,
|
||||||
|
@ -646,7 +651,8 @@ mod tests {
|
||||||
"#,
|
"#,
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(expanded, r#"loop{"error!"}"#);
|
// This expands to nothing (since it's in item position), but emits an error.
|
||||||
|
assert_eq!(expanded, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -207,6 +207,7 @@ fn macro_expand_with_arg(
|
||||||
} else {
|
} else {
|
||||||
return ExpandResult {
|
return ExpandResult {
|
||||||
value: Some(db.lookup_intern_eager_expansion(id).subtree),
|
value: Some(db.lookup_intern_eager_expansion(id).subtree),
|
||||||
|
// FIXME: There could be errors here!
|
||||||
err: None,
|
err: None,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue