Skip to content

rustfmt panics on quote!(Self(#var)) pattern in Rust 1.93.0 #6779

@natepiano

Description

@natepiano

Summary

rustfmt panics when formatting code containing quote!(Self(#interpolated_var)). This is a regression from Rust 1.92.0.

Reproduction

Repository: https://github.com/natepiano/rustfmt_bug_repro

# Cargo.toml
[package]
name = "rustfmt_bug_repro"
version = "0.1.0"
edition = "2024"

[dependencies]
quote = "1"
// src/lib.rs
use quote::quote;

fn f() {
    let y = 0;
    quote!(Self(#y));
}

// Workaround that doesn't panic:
// fn f_workaround() {
//     let y = 0;
//     let self_ty = quote!(Self);
//     quote!(#self_ty(#y));
// }

Command: rustfmt src/lib.rs

Error Output

thread 'main' panicked at compiler/rustc_parse/src/parser/item.rs:400:32:
called `Result::unwrap()` on an `Err` value: Some(DiagInner { level: Error, messages: [(FluentIdentifier("parse_expected_identifier_found_keyword_str", None), NoStyle)], ... args: {"token": Str("Self")}, ... })

The panic occurs in rustfmt_nightly::parse::macros::parse_macro_arg when parsing the Self keyword followed by parentheses containing an interpolated variable.

Version Info

Fails on:

  • rustc 1.93.0 (254b59607 2026-01-19)
  • rustfmt 1.8.0-stable (254b59607d 2026-01-19)

Works on:

  • rustc 1.92.0 (ded5c06cf 2025-12-08)

Impact

This affects real-world code including Bevy's bevy_reflect_derive crate which uses this pattern.

Workaround

Pre-construct Self as a separate token stream:

let self_ty = quote!(Self);
quote!(#self_ty(#y));  // instead of quote!(Self(#y))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions