Doc comments can be passed to macros as literals #61001
Labels
A-frontend
Area: Compiler frontend (errors, parsing and HIR)
A-macros
Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)
C-enhancement
Category: An issue proposing an enhancement or a PR with one.
T-lang
Relevant to the language team, which will review and decide on the PR/issue.
Doc comments are the only tokens that cannot be passed to macros precisely, they are converted into a
#[doc = "text"]
form instead, which is a pretty big hack.This conversion may change escaping in the text irrecoverably and can also change semantics in corner cases (e.g.
doc
starts going through name resolution).Also, this is the single reason why doc comments may need a conversion at all (#60935).
Lexically doc comments are raw string literals with weird quotes (
/**
+*/
and///
+\n
).Syntactically they certainly can be interpreted as attribute literals, similarly to how strings or integers are expression literals, and how
!
would be a type literal if it wasn't a punctuation lexically.We can use this intuition for fixing the situation with passing doc comments to macros.
Declarative macros
The recently introduced
literal
matcher can start matching doc comments.I don't think there are going to be any implementation issues with that.
Proc macros
We cannot add a new variant to
TokenTree
backward compatibly, but the content ofTokenTree::Literal
is only available throughto_string()
and is open for additions.So,
literal.to_string()
can start returning things like/// Text
.Of course,
syn
and friends must be ready to get a result like this from stringifying a literal token.The text was updated successfully, but these errors were encountered: