Repository navigation
Span issues with FromStr in unstable proc_macro #48944
Description
Activity
I should note that adding a
' '(space) character to the beginning of the string causes all tokens to have spans matching the second one.I think the cause of the first-span-being-different issue is
, which causes the first token to be advanced-to and peeked at before therust/src/libsyntax/parse/lexer/mod.rs
Lines 202 to 209 in 178becd
pub fn new(sess: &'a ParseSess, filemap: Lrc<syntax_pos::FileMap>) -> Self { let mut sr = StringReader::new_raw(sess, filemap); if sr.advance_token().is_err() { sr.emit_fatal_errors(); FatalError.raise(); } sr } override_spanis set here:rust/src/libsyntax/parse/mod.rs
Line 230 in 604d4ce
srdr.override_span = override_span; The cause of the issue where all parsed tokens have the line and column of the input file instead of lines and columns into a fake proc-macro object seems to be caused by the way that
override_spanis used - namely how it overrides the entire span instead of just the expansion context.- addedA-decl-macros-2-0Area: Declarative macros 2.0 (#39412)Area: Declarative macros 2.0 (#39412)
on Mar 14, 2018 - addedT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.C-bugCategory: This is a bug.Category: This is a bug.
on May 14, 2018 - addedE-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.Call for participation: An issue has been fixed and does not reproduce, but no test has been added.
on Jan 25, 2024 @rustbot claim
@KaiTomotake you still on it? happy to push if you are not on it
- added a commit that references this issue
on Sep 23, 2026 - added a commit that references this issue
on Sep 23, 2026
While writing improved tests for
proc_macro2, I noticed some surprising behaviour of theFromStrimpl forproc_macro::TokenStream.When parsing a string with
FromStr, a token parsed from the very beginning of the string can have a differentsource_file()than a token parsed elsewhere. For example:Will produce the output:
I would've expected both tokens to have the same context, and similar BytePos-es. Specifically I would have expected both of these tokens to have a span located in a fake ProcMacroSourceCode file, but set to resolve in the context of the enclosing scope (as if span.resolved_at(Span::call_site()) was called).