Edit: starting with 1.54.0, Attributes can invoke function-like macros, enabling the code proposed in the question. Original answer below :
The answer seems to be no.
Looking at the grammar for attributes, apart from parentheses, commas and equals signs, attributes can ultimately only contain literals. So on this level, there is no way Rust allows more here.
However, inverting the structure enables something like this, and the doc-comment
crate does this for doc comments. Instead of calling a macro from inside an attribute, use a macro to create an attribute; that macro is then not constrained to only taking literals*. The downside is, the item that the attribute should apply to must be part of the macro call. So this
#[doc=my_content!()]
struct Foo;
becomes this:
doc_comment!(
my_content!(),
struct Foo;
);
The definition of the macro is straight-forward:
#[macro_export]
macro_rules! doc_comment {
($x:expr, $($tt:tt)*) => {
#[doc = $x]
$($tt)*
};
}
(omitted a branch of the original macro that is not part of the core pattern)
(thanks to jonas-schlevink for pointing me to this)
*except for that last part (getting macro content into the attribute), the linked question's answer already does exactly that.