proc_macro::Literal question


#1

Trying to write a proc macro function in rust 2018. In iterating through the TokenStream items, I have a TokenTree::Literal which wraps a proc_macro::Literal. When i print the value of the token in question via “{:?}”, I get:

Literal { lit: Float(1.2), suffix: None, span: #0 bytes(73…76) }

And i want to match against Literal.lit, but Literal’s fields are private. What gives? How do I get at this?


#2

The API for interpreting TokenStreams is mostly “stringly typed” right now. You can call to_string on the Literal and then you can parse it manually. syn can do this for you.


#3

What is the point of the Literal distinguishing internally then? Seems like its doing extra work that I cannot take advantage of… Is it just for internal validation?


#4

The proc_macro API is based on the internal lexer in the compiler. The compiler internals can use the more detailed information, but this is not part of the public API. Since anything in the public API has to remain indefinitely, a conservative approach was taken to stabilising only what’s absolutely necessary.