Skip to content

Commit

Permalink
Turn some assertions into proper proc macro errors
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Jun 5, 2024
1 parent 8bdb617 commit 950dbca
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions compiler/rustc_macros/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,17 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
let mut query_description_stream = quote! {};
let mut query_cached_stream = quote! {};
let mut feedable_queries = quote! {};
let mut errors = quote! {};

macro_rules! assert {
( $cond:expr, $span:expr, $( $tt:tt )+ ) => {
if !$cond {
errors.extend(
Error::new($span, format!($($tt)*)).into_compile_error(),
);
}
}
}

for query in queries.0 {
let Query { name, arg, modifiers, .. } = &query;
Expand Down Expand Up @@ -369,10 +380,15 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
[#attribute_stream] fn #name(#arg) #result,
});

if modifiers.feedable.is_some() {
assert!(modifiers.anon.is_none(), "Query {name} cannot be both `feedable` and `anon`.");
if let Some(feedable) = &modifiers.feedable {
assert!(
modifiers.anon.is_none(),
feedable.span(),
"Query {name} cannot be both `feedable` and `anon`."
);
assert!(
modifiers.eval_always.is_none(),
feedable.span(),
"Query {name} cannot be both `feedable` and `eval_always`."
);
feedable_queries.extend(quote! {
Expand Down Expand Up @@ -407,5 +423,6 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
use super::*;
#query_cached_stream
}
#errors
})
}

0 comments on commit 950dbca

Please sign in to comment.