Skip to content

Fix build compatibility with shadowed defmt macros#109

Open
WillaWillNot wants to merge 1 commit into
hecatia-elegua:mainfrom
WillaWillNot:fix_defmt_macro_use_compatibility
Open

Fix build compatibility with shadowed defmt macros#109
WillaWillNot wants to merge 1 commit into
hecatia-elegua:mainfrom
WillaWillNot:fix_defmt_macro_use_compatibility

Conversation

@WillaWillNot

@WillaWillNot WillaWillNot commented May 22, 2026

Copy link
Copy Markdown

Despite deprecation, it remains common practice to use defmt with #[macro_use] enabled in embedded projects, to save code size by shadowing core library printing functions. (It ensures that dependencies don't bring in any core::fmt functions via their own use of macros like panic!().)

It's currently impossible to mix that usage of defmt with bilge in the same crate, because several of bilge's proc macros utilize macros that might be shadowed by defmt without fully qualified names. This change adds qualified names to all of those uses, so that bilge can coexist with shadowed printing macros. Specifically, the macros used that needed qualified names were assert!, panic!, quote!, todo!, unreachable!, and write!.

Proc macros, if they ever interact with stdout, are meant to interact with the compiling host's stdout, not the target runtime stdout. So fully qualifying print macros to use the core library version is correct.

This change doesn't add actual defmt::Format support, that's a much larger scope. It's just meant to allow the crates to coexist. Defmt is added exclusively as a dev-dependency for the defmt-compatibility test.

The error produced when trying to build a crate with both bilge usage and shadowed defmt macros before this change is:

error: expected string literal                                                                                             
  --> src\main.rs:56:1
   |
56 | #[bitsize(1)]
   | ^^^^^^^^^^^^^
   |
   = note: this error originates in the attribute macro `bitsize` (in Nightly builds, run with -Z macro-backtrace for more info)

… inside proc macros are done with the fully qualified core library names (affects assert!, panic!, quote!, todo!, unreachable!, and write!). Add test that compilation with both bilge macro usage and defmt #[macro_use] succeeds. Fix expected stderr strings in failing examples.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant