rustrust-compiler-plugin

How can I retrieve the full path of a trait bound in a Rust compiler plugin?


I'm working on a Rust plugin that needs to access the absolute path of a trait bound. In practice, this means that for the following code, I want to resolve the full path of Debug as std::fmt::Debug.

use std::fmt::*;

#[foo]
trait Foo: Debug {}

My current approach consist of taking the Annotatable that MultiItemDecorator provides for me and pattern matching it to Annotatable::Item, where I match .node to ItemKind::Trait. I then match .generic_bounds to a collection of GenericBound::Trait, where I retrieve .trait_ref.path.

This struct however only contains path(Debug), which is not enough information for me.


Solution

  • You can't.

    The Rustc Driver:

    […] the main phases of the compiler are:

    1. Parse Input: Initial crate parsing
    2. Configure and Expand: Resolve #[cfg] attributes, name resolution, and expand macros
    3. Run Analysis Passes: Run trait resolution, typechecking, region checking and other miscellaneous analysis passes on the crate
    4. Translate to LLVM: Translate to the in-memory form of LLVM IR and turn it into an executable/object files

    (emphasis is mine)

    Macros are expanded before trait resolution is done, so at the time your plugin is run, nothing is known about this Debug except the name given in the source code.