Skip to content

Report a non-literal #[doc = ...] instead of skipping it - #71

Merged
Manishearth merged 2 commits into
yaahc:masterfrom
bercedev:fix/non-literal-doc-error
Oct 22, 2026
Merged

Manishearth merged 2 commits into
yaahc:masterfrom
bercedev:fix/non-literal-doc-error

Conversation

@bercedev

Copy link
Copy Markdown
Contributor

Fixes #61.

A #[doc = ...] whose value isn't a string literal, such as #[doc = include_str!("...")], used to make the derive panic with "not implemented". Since #69, which I wrote to skip #[doc(alias = "...")] and #[doc(hidden)], it's skipped silently instead. That's arguably worse here: a struct then gets no Display impl at all, and the error only shows up wherever the type is formatted, as "Foo doesn't implement std::fmt::Display".

display() now returns a compile error, spanned on the value, when a #[doc = ...] value isn't a string literal:

error: displaydoc can only read doc comments that are string literals; give the format with #[displaydoc("...")] instead
 --> tests/ui/non_string_doc_expr.rs:4:9
  |
4 | #[doc = include_str!("non_string_doc_expr.txt")]
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

A type with #[displaydoc("...")] is unaffected, since that attribute is read first. #69 isn't released yet, so compared with 0.2.7 this replaces the panic with a proper error.

Tests:

  • New UI test tests/ui/non_string_doc_expr.rs with its .stderr, added to compile_tests.rs. On master the file compiles, because the derive skips the attribute and emits nothing, so the compile_fail case fails there.
  • cargo test on stable 1.99.1 passes (the UI tests are ignored off nightly). The UI tests pass on nightly-2026-10-22.
  • cargo fmt --check and cargo clippy --all-targets are clean.
  • CHANGELOG.md has an entry under Unreleased.

Comment thread src/attr.rs Outdated
{
return Err(syn::Error::new_spanned(
value,
"displaydoc can only read doc comments that are string literals; give the format with #[displaydoc(\"...\")] instead",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"displaydoc can only read doc comments that are string literals; give the format with #[displaydoc(\"...\")] instead",
"displaydoc can only read doc comments that are string literals; supply the Display text with #[displaydoc(\"...\")] instead",

@bercedev

Copy link
Copy Markdown
Contributor Author

applied it, and updated tests/ui/non_string_doc_expr.stderr to match. the ui tests pass on nightly

@Manishearth
Manishearth merged commit 6f11002 into yaahc:master Oct 22, 2026
8 checks passed
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.

Error messaging for non Lit::str doc comments

2 participants