Search code examples
rustcompiler-construction

How to extract nightly features used in a crate?


I want to extract all nightly features used by a crate for some research purposes.

Just to be clear, "nightly features" means codes like #![feature(...)]", but not crate provided optional features.

I tried using regex, and it works, but not accurate enough. cfg defined features may not be detected: #![cfg_attr(target_os = "macos", feature(...))].

So I turn to rustc compiler, I wish to modify some source code and print out what features crate uses. Now I'm still trying to find out how rustc deals with nightly features.

I hope someone can give me some tips and help about the process of rustc dealing with nightly features. I'm almost lost in codes.


Solution

  • Access to enabled features is done via rustc_session::Session::features. You can add code after they are set in rustc_interface, or look at how they're computed.

    If you are at a later stage, you probably have access to the Session of the compiled crate. For example, TyCtxt has a sess() method (but also a features() method for direct access using the query system), and InferCtxt has a tcx member to access the TyCtxt.