Skip to content

annotation: Warn instead of panicking on unparsable atom values - #1186

Open
tk1475 wants to merge 1 commit into
mozilla:mainfrom
tk1475:fix-invalid-annotation-atom-panic
Open

tk1475 wants to merge 1 commit into
mozilla:mainfrom
tk1475:fix-invalid-annotation-atom-panic

Conversation

@tk1475

@tk1475 tk1475 commented Sep 28, 2026

Copy link
Copy Markdown

An annotation with a value that doesn't parse, such as

/// cbindgen:rename-all=not-a-valid-rename-rule
#[repr(C)]
pub struct Trigger { pub field: u8 }

made cbindgen panic with called `Option::unwrap()` on a `None` value, because AnnotationSet::parse_atom did y.parse::<T>().ok().unwrap().

With this change parse_atom logs a warning with the existing warn! macro and returns None, so the annotation is ignored and the callers fall back to the configured default (for example structure.rename_fields for rename-all), the same as when the annotation is absent:

WARN: Couldn't parse value "not-a-valid-rename-rule" of annotation "cbindgen:rename-all", ignoring it.

This covers every parse_atom user (rename-all, rename-variant-name-fields, rename-associated-constant). An empty value (cbindgen:rename-all=) still maps to T::default() as before.

Fixes the rename-all panic from #1184. The other reproducers in that issue (generic arity mismatches, malformed lock file, recursive #[path] module) are separate and not touched here, so the issue should stay open.

How tested

  • Added unit tests parse_atom_valid and parse_atom_invalid in src/bindgen/ir/annotation.rs. parse_atom_invalid panicked before the fix and passes after.
  • Ran the issue's reproducer with cbindgen --lang c: it panicked before, and now prints the warning and generates the header.
  • CBINDGEN_TEST_VERIFY=1 cargo +nightly test: unit tests, profile and all 161 tests/rust cases pass with no expectation changes. I don't have Cython or CMake locally, so I ran the expectation suite with CBINDGEN_TEST_NO_COMPILE=1, and the depfile tests (which need CMake) could not run.
  • cargo +stable fmt --check and cargo +stable clippy --workspace -- -D warnings: clean.

AI disclosure

This change was drafted with Claude Code, then reviewed and tested by me.

🤖 Generated with Claude Code

`AnnotationSet::parse_atom` unwrapped the result of `FromStr`, so an
annotation such as `cbindgen:rename-all=not-a-valid-rename-rule` made
cbindgen panic. Log a warning and ignore the annotation instead, so the
configured default is used.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
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