Stream: t-compiler/help

Topic: Using rustc_ap_rustc_parse::parse_crate_from_file


Dzmitry Malyshau (May 06 2020 at 23:55, on Zulip):

I'm trying to figure out how this function works, and I'm missing something. If we pass a "lib.rs" of a valid library crate, I'd expect it to contain all the modules. However, running it on a simple project that has crate::submodule::subsubmodule path, it appears that the reflected crate doesn't contain it:

crate
    A
    submodule
    tests

I'm printing this with the following code:

fn print_module(indent: usize, name: &str, module: &rustc_ast::ast::Mod) {
    for _ in 0 .. indent {
        print!("\t");
    }
    println!("{}", name);
    for item in module.items.iter() {
        if let ItemKind::Mod(ref child) = item.kind  {
            let name = item.ident.to_string();
            print_module(indent + 1, &name, child);
        }
    }
}
Dzmitry Malyshau (May 06 2020 at 23:56, on Zulip):

Screen-Shot-2020-05-06-at-19.53.12.png

Dzmitry Malyshau (May 06 2020 at 23:56, on Zulip):

The submodule/mod.rs clearly contains the "subsubmodule":

pub mod subsubmodule;

use crate::A;
use ::chrono;
use std::fmt::Write;
use A::{self as OtherA, B};
use self::subsubmodule::C;
Dzmitry Malyshau (May 07 2020 at 00:11, on Zulip):

looking further in the submodule it has only items for use statements

simulacrum (May 07 2020 at 00:26, on Zulip):

iirc mod statements are "inlined" during expansion

simulacrum (May 07 2020 at 00:26, on Zulip):

so you won't actually parse the submodule (or the subsubmodule) until expansion runs

Dzmitry Malyshau (May 07 2020 at 00:57, on Zulip):

how do I run the expansion pass?

simulacrum (May 07 2020 at 02:03, on Zulip):

https://rustc-dev-guide.rust-lang.org/macro-expansion.html may be helpful but I don't have a quick answer I'm afraid

Dzmitry Malyshau (May 07 2020 at 02:14, on Zulip):

Wait, this doesn't yet make sense. So I'm telling rustc_parse to parse some crate. It goes through the tokens, and then it sees "mod xx", and then what? Just ignores it? If it's not expected to see nested modules at this stage, why not return an error that there is an unexpected?

simulacrum (May 07 2020 at 02:18, on Zulip):

it's just stored as "mod xx;"

simulacrum (May 07 2020 at 02:18, on Zulip):

similar to how foo!(bar); would be stored

Dzmitry Malyshau (May 07 2020 at 02:20, on Zulip):

Where would it be stored? I'm not seeing anything like that in the item tree returned

Dzmitry Malyshau (May 07 2020 at 16:05, on Zulip):

I think I understand better what's going on here. @simulacrum correct me if that's wrong please - the parse functions get a single string or file on the input, and that's the scope of their parsing. So if I have other modules in other files, it simply doesn't care to look at them.
In this case, I need some higher level rustc function to load the whole file tree of the crate.

simulacrum (May 07 2020 at 16:19, on Zulip):

yeah that's my understanding at least

Last update: Sep 28 2020 at 16:45UTC