11# Initial Integration
22
3- In order to use ` stable_mir ` in your crate, you will need to do the following:
3+ For an example of how to use ` rustc_public ` , see the [ ` demo/ ` ] directory of the project-stable-mir repo.
4+ For a tutorial, read on!
45
5- 1 . Use a nightly toolchain that includes the ` stable_mir ` crate.
6+ In order to use ` rustc_public ` in your crate, you will need to do the following:
7+
8+ 1 . Use a nightly toolchain that includes the ` rustc_public ` crate.
692 . Install at least the following rustup components: "rustc-dev" and "llvm-tools"
7- 3 . Declare ` stable_mir ` as an external crate at the top of your crate:
10+ 3 . Declare ` rustc_public ` as an external crate at the top of your crate:
811
912``` rust
10- extern crate stable_mir;
13+ #![feature(rustc_private)]
14+ extern crate rustc_public;
1115```
1216
1317For 1 and 2, we highly recommend adding a "rust-toolchain.toml" file to your project.
1418We also recommend to pin down a specific nightly version, to ensure all users and tests are using the same compiler
1519version.
16- Therefore, the same ` stable_mir ` crate version. E.g.:
20+ Therefore, the same ` rustc_public ` crate version. E.g.:
1721
1822``` toml
1923# Example of a rust-toolchain.toml
2024[toolchain ]
2125# Update the date to change the toolchain version.
22- channel = " nightly-2024-06-17 "
26+ channel = " nightly-2025-10-27 "
2327components = [" llvm-tools" , " rustc-dev" , " rust-src" ]
2428```
2529
@@ -29,28 +33,28 @@ There's currently no stable way to initialize the Rust compiler and StableMIR.
2933See [ #0069 ] ( https://github.com/rust-lang/project-stable-mir/issues/69 ) for more details.
3034
3135Instead, StableMIR includes two unstable workarounds to give you a quick start.
32- The ` run ` and ` run_with_tcx ` macros, both from present in the ` rustc_smir ` crate.
36+ The ` run ` and ` run_with_tcx ` macros, both from present in the ` rustc_public ` crate.
3337
3438In order to use the ` run ` macro, you first need to declare the following external crates:
3539
3640``` rust
41+ #![feature(rustc_private)]
42+ extern crate rustc_public;
43+
3744extern crate rustc_driver;
3845extern crate rustc_interface;
39- #[macro_use]
40- extern crate rustc_smir;
41- // This one you should know already. =)
42- extern crate stable_mir;
46+ extern crate rustc_middle;
4347```
4448
4549Then start the driver using the ` run!() ` macro:
4650
4751``` rust
48- let result = run! (rustc_args , callback_fn );
52+ let result = rustc_public :: run! (rustc_args , callback_fn );
4953```
5054
5155This macro takes two arguments:
5256
53- 1 . A vector with the command arguments to the compiler.
57+ 1 . A ` &[String] ` with the command line arguments to the compiler.
54582 . A callback function, which can either be a closure expression or a function ident.
5559 - This callback function shouldn't take any argument, and it should return a ` ControlFlow<B,C> ` .
5660
@@ -63,18 +67,6 @@ The second option is the `run_with_tcx!()` macro, which is very similar to the `
6367The main difference is that this macro passes a copy of the Rust compiler context (` TyCtxt ` ) to the callback,
6468which allows the user to also make calls to internal compiler APIs.
6569
66- Note that this option also requires the declaration of the ` rustc_middle ` external crate, i.e., you should now have the
67- following declarations:
68-
69- ``` rust
70- extern crate rustc_driver;
71- extern crate rustc_interface;
72- extern crate rustc_middle; // This one is new!
73- #[macro_use]
74- extern crate rustc_smir;
75- extern crate stable_mir;
76- ```
77-
7870## Scope of StableMIR objects
7971
8072StableMIR objects should not be used outside the scope of the callback function.
@@ -86,19 +78,19 @@ is running:
8678``` rust
8779fn print_items (rustc_args : Vec <String >) {
8880 let _result = run! (rustc_args , || {
89- for item in stable_mir :: all_local_items () {
81+ for item in rustc_public :: all_local_items () {
9082 // Using items inside the callback!
9183 println! (" - {}" , item . name ())
9284 }
9385 });
9486}
9587```
9688
97- However, the following usage isn't valid, and ` stable_mir ` will panic when we invoke the ` name() ` function.
89+ However, the following usage isn't valid, and ` rustc_public ` will panic when we invoke the ` name() ` function.
9890
9991``` rust
10092fn broken_print_items (rustc_args : Vec <String >) {
101- let result = run! (rustc_args , || { ControlFlow :: Continue (stable_mir :: all_local_items ())});
93+ let result = run! (rustc_args , || { ControlFlow :: Continue (rustc_public :: all_local_items ())});
10294 if let ControlFlow :: Continue (items ) = result {
10395 for item in items {
10496 // Using item outside the callback function is wrong!
116108
117109## Analyzing monomorphic instances
118110
119- TODO
111+ TODO
112+
113+ [ ` demo/ ` ] ( https://github.com/rust-lang/project-stable-mir/tree/main/demo )
0 commit comments