rust-lang/rust · error · syn::Error
attributes not supported on queries
Error message
attributes not supported on queries
What it means
Emitted by the check_attributes function inside the rustc_queries! proc macro (rustc_macros/src/query.rs). The macro validates every attribute on a query or non_query declaration and only permits #[doc] attributes (doc comments). If any other attribute is present (e.g., #[inline], #[cfg(...)], #[allow(...)]), this syn::Error is produced at that attribute's span. The error surfaces as a compile-time error in the crate invoking rustc_queries!.
Solutions
- Remove the non-doc attribute from the query declaration — query modifiers (eval_always, arena_cache, etc.) go inside the { } modifier block, not as attributes.
- If you need conditional compilation, apply it at a higher level (the module or the rustc_queries! invocation site), not on individual queries.
- Move lint allows to the enclosing module with #[allow(...)] on the mod or use #![allow(...)] at crate level.
Example fix
// before
rustc_queries! {
#[inline]
/// Computes the type of an expression.
query type_of(key: DefId) -> Ty<'tcx> { desc { "type of `{}`", key } }
}
// after: remove the non-doc attribute
rustc_queries! {
/// Computes the type of an expression.
query type_of(key: DefId) -> Ty<'tcx> { desc { "type of `{}`", key } }
} Defensive patterns
Strategy: validation
Validate before calling
// In a test or CI lint, verify that query declarations only have #[doc] attributes. // This is a code-review/lint concern — the proc macro itself enforces it at compile time. // You can grep your query declarations: // rg -A5 'query |non_query ' --type rust | grep -v 'doc\|//' | grep '#\[' // Any hit indicates a non-doc attribute on a query.
Try / catch
// This is a compile-time error from a proc macro. It cannot be caught. // Fix by removing the offending attribute from the query declaration. // The error points to the exact attribute span.
Prevention
- Only use /// doc comments on query and non_query declarations inside rustc_queries!.
- Query behavior modifiers go inside the { } block, not as attributes.
- Add a code review checklist item: 'no non-doc attributes on queries'.
- Use rust-analyzer or rustdoc to preview the macro expansion if unsure.
When it happens
Trigger: A compiler developer adds a query declaration inside a rustc_queries! { } block and annotates it with a non-doc attribute such as #[inline], #[allow(dead_code)], #[cfg(feature = "x")], or any other helper attribute.
Common situations: Adding a new query to rustc and trying to suppress a lint or add an optimization hint via attributes; copy-pasting from a non-query context where attributes are allowed.
Related errors
- attributes must be outer attributes (`///`), not inner…
- duplicate modifier
- Expected a string literal
- unknown query modifier
- CFG_RELEASE env var
AI-assisted analysis of rust-lang/rust@7088e4b63a (2026-08-10).
Data as JSON: /api/errors/0662efebdee5d35f.
Report an issue: GitHub.
Appendix: source
Thrown at compiler/rustc_macros/src/query.rs:21
use quote::{quote, quote_spanned};
use syn::parse::{Parse, ParseStream, Result};
use syn::punctuated::Punctuated;
use syn::spanned::Spanned;
use syn::{
AttrStyle, Attribute, Error, Expr, Ident, Pat, ReturnType, Token, Type, braced, parenthesized,
parse_macro_input, token,
};
mod kw {
syn::custom_keyword!(non_query);
syn::custom_keyword!(query);
}
/// Ensures only doc comment attributes are used
fn check_attributes(attrs: Vec<Attribute>) -> Result<Vec<Attribute>> {
let inner = |attr: Attribute| {
if !attr.path().is_ident("doc") {
Err(Error::new(attr.span(), "attributes not supported on queries"))
} else if attr.style != AttrStyle::Outer {
Err(Error::new(
attr.span(),
"attributes must be outer attributes (`///`), not inner attributes",
))
} else {
Ok(attr)
}
};
attrs.into_iter().map(inner).collect()
}
/// Declaration of a compiler query.
///
/// ```ignore (illustrative)
/// /// Doc comment for `my_query`.
/// // ^^^^^^^^^^^^^^^^^^^^^^^^^^^ doc_comments
/// query my_query(key: DefId) -> Value { anon }View on GitHub (pinned to 7088e4b63a)