tracel-ai/burn · error

Only literal is supported

Error message

Only literal is supported

What it means

Value-form guard in the same attribute parser: after extracting the attribute's value expression, only a literal (`Lit`) is accepted; computed expressions such as `#[attr(foo())]` or concatenations reach the fallback arm and panic at macro-expansion time. The failing input is a non-literal expression used as the attribute value.

Source

Thrown at crates/burn-derive/src/shared/attribute.rs:26

pub struct AttributeItem {
    pub value: syn::Lit,
}

impl AttributeAnalyzer {
    pub fn new(attr: Attribute) -> Self {
        Self { attr }
    }

    pub fn item(&self) -> AttributeItem {
        let value = match &self.attr.meta {
            Meta::List(val) => val.parse_args::<syn::MetaNameValue>().unwrap(),
            Meta::NameValue(meta) => meta.clone(),
            Meta::Path(_) => panic!("Path meta unsupported"),
        };

        let lit = match value.value {
            syn::Expr::Lit(lit) => lit.lit,
            _ => panic!("Only literal is supported"),
        };

        AttributeItem { value: lit }
    }

    pub fn has_name(&self, name: &str) -> bool {
        Self::path_syn_name(self.attr.path()) == name
    }

    fn path_syn_name(path: &syn::Path) -> String {
        let length = path.segments.len();
        let mut name = String::new();
        for (i, segment) in path.segments.iter().enumerate() {
            if i == length - 1 {
                name += segment.ident.to_string().as_str();
            } else {
                let tmp = segment.ident.to_string() + "::";
                name += tmp.as_str();

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Change the attribute value to a literal, e.g. #[my_attr = "value"] or #[my_attr(value)] where value parses to a syn::Lit, since AttributeAnalyzer::item only accepts syn::Expr::Lit.
  2. If you need complex expressions in the attribute, extend the match arm in AttributeAnalyzer::item to handle the required syn::Expr variants instead of panicking.
  3. Check for stray macro interpolation or concatenation in the attribute that produces a non-literal expression, and pre-compute the value so a plain literal appears in the source.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/burn-derive/src/shared/attribute.rs:26 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05). Data as JSON: /api/errors/ae1dc538fc48a5f0. Report an issue: GitHub.