Skip to content

The inner span for an empty module should point to the module's file #26755

Closed
@nrc

Description

@nrc

Assume you have mod foo where foo.rs is an empty file. There will be an ItemMod for the module and it's inner span will be a dummy span. It would be more useful if that span could be a zero-length span pointing into foo.rs's FileMap. (This is possible with a PR which should land soon). This would be useful since it gives tools an easy to deal with empty modules, at the moment they have to do all kinds of hacks to handle them.

I experimented a bit with this, but I couldn't get it to work. Here is what I found:

The parser for the outer file will create a new parser to parse foo.rs, that will wrap a TTReader which has a single Eof token. The TTReader is created by running a StringReader over foo.rs (a StringReader being pretty much Rust's lexer). I believe, although I haven't confirmed, that for an empty file, that Eof token comes from syntax::ext::tt::transcribe::tt_next_token (line 217 today). However, there we don't have the FileMap.

My attempt to fix this was to check the result in syntax::parse::filemap_to_parser, if the token of the parser is Eof and the span is DUMMY_SP, then I replaced the span with the zero_length span at the end_pos of the FileMap. This broke sub-module lookup in the parser (which surprised me), the parser's span field was incorrect (one module too far out). At this stage I decided to give up.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-parserArea: The lexing & parsing of Rust source code to an AST

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions