Skip to content

Commit 4ceae76

Browse files
committed
[wip] update to rustc_private 2023-06-10
current errors: ``` 1 error[E0432]: unresolved import `rustc_hir::def_id::DefIndexAddressSpace` --> src/print.rs:8:52 2 error[E0603]: module `rmeta` is private --> src/driver.rs:5:21 --> /home/jyn/.local/lib/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/compiler/rustc_metadata/src/lib.rs:36:1 3 error[E0603]: module `rmeta` is private --> src/print.rs:9:21 --> /home/jyn/.local/lib/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/compiler/rustc_metadata/src/lib.rs:36:1 4 error[E0599]: no method named `crate_data_as_rc_any` found for struct `TyCtxt<'tcx>` in the current scope --> src/driver.rs:88:26 5 error[E0618]: expected function, found `impl for<'b> FnOnce(&'b CrateMetadata)` --> src/driver.rs:89:5 6 error[E0599]: no function or associated item named `from_array_index` found for struct `DefIndex` in the current scope --> src/print.rs:312:31 ``` some of these are just because `CrateMetadata` is private now and its methods are on TyCtxt, but some of them are real - `DefIndex` has no public constructors anymore and `CrateRoot` is private to `rustc_metadata`.
1 parent ebfec0f commit 4ceae76

File tree

3 files changed

+74
-109
lines changed

3 files changed

+74
-109
lines changed

src/driver.rs

Lines changed: 22 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,31 @@
1-
use std::path::{Path, PathBuf};
1+
use std::path::Path;
22

3-
use rustc::session::Session;
43
use rustc::ty::TyCtxt;
54

6-
use rustc_metadata::cstore::CrateMetadata;
5+
use rustc_metadata::rmeta::decoder::CrateMetadata;
76
use rustc_driver::{Compilation, Callbacks};
87
use rustc_interface::interface::Compiler;
8+
use rustc_interface::Queries;
99

10-
fn find_sysroot() -> String {
11-
if let Ok(sysroot) = ::std::env::var("MIRI_SYSROOT") {
12-
return sysroot;
13-
}
14-
15-
// Taken from https://github.com/Manishearth/rust-clippy/pull/911.
16-
let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
17-
let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
18-
match (home, toolchain) {
19-
(Some(home), Some(toolchain)) => format!("{}/toolchains/{}", home, toolchain),
20-
_ => {
21-
option_env!("RUST_SYSROOT")
22-
.expect(
23-
"need to specify RUST_SYSROOT env var or use rustup or multirust",
24-
)
25-
.to_owned()
26-
}
27-
}
28-
}
29-
30-
pub fn call_with_crate_tcx(mut args: Vec<String>, rlib: String, f: Box<Cb>) {
10+
pub fn call_with_crate_tcx(mut args: Vec<String>, rlib: String, f: Box<Cb>) -> ! {
3111
println!("Reading rlib {}", rlib);
3212

33-
let sysroot_flag = String::from("--sysroot");
34-
if !args.contains(&sysroot_flag) {
35-
args.push(sysroot_flag);
36-
args.push(find_sysroot());
37-
}
38-
3913
args.push("-Cpanic=abort".to_string()); // otherwise we have to provide `eh_personality`
4014

4115
println!("Rust args: {:?}", args);
42-
let result = rustc_driver::report_ices_to_stderr_if_any(move || {
43-
rustc_driver::run_compiler(
44-
&args,
45-
&mut MyCompilerCalls(f),
46-
Some(Box::new(MyFileLoader(rlib.to_string())) as Box<_>),
47-
None
48-
)
49-
}).and_then(|result| result);
50-
std::process::exit(result.is_err() as i32);
16+
std::process::exit(rustc_driver::catch_with_exit_code(move || {
17+
let args: Vec<String> = std::env::args().collect();
18+
rustc_driver::RunCompiler::new(&args, &mut MyCompilerCalls(f)).run()
19+
}))
5120
}
5221

5322
struct MyFileLoader(String);
5423

55-
impl ::syntax::source_map::FileLoader for MyFileLoader {
24+
impl ::rustc_span::source_map::FileLoader for MyFileLoader {
5625
fn file_exists(&self, _: &Path) -> bool { true }
57-
fn abs_path(&self, _: &Path) -> Option<PathBuf> { None }
26+
fn read_binary_file(&self, _: &Path) -> std::io::Result<Vec<u8>> {
27+
Err(std::io::Error::new(std::io::ErrorKind::Unsupported, "should not need to load binary files"))
28+
}
5829
fn read_file(&self, _: &Path) -> Result<String, ::std::io::Error> {
5930
Ok(format!("
6031
#![feature(no_core)]
@@ -68,32 +39,32 @@ impl ::syntax::source_map::FileLoader for MyFileLoader {
6839
}
6940
}
7041

71-
type Cb = for<'a, 'tcx> Fn(TyCtxt<'a, 'tcx, 'tcx>) + Send;
42+
type Cb = dyn for<'tcx> Fn(TyCtxt<'tcx>) + Send;
7243

7344
struct MyCompilerCalls(Box<Cb>);
7445

7546
impl Callbacks for MyCompilerCalls {
76-
fn after_analysis(&mut self, compiler: &Compiler) -> bool {
77-
compiler.global_ctxt().unwrap().take().enter(|tcx| {
47+
fn after_analysis<'tcx>(&mut self, compiler: &Compiler, queries: &'tcx Queries<'tcx>) -> Compilation {
48+
queries.global_ctxt().unwrap().enter(|tcx| {
7849
(self.0)(tcx);
7950
});
80-
false
51+
Compilation::Stop
8152
}
8253

8354
}
8455

85-
pub fn with_crate_metadata<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, f: impl for<'b> FnOnce(&'b CrateMetadata)) {
56+
pub fn with_crate_metadata<'tcx>(tcx: TyCtxt<'tcx>, f: impl for<'b> FnOnce(&'b CrateMetadata)) {
8657
let mut extern_crate = None;
87-
for item in tcx.hir().krate().items.values() {
88-
match item.node {
89-
::rustc::hir::ItemKind::ExternCrate(_) => {
90-
extern_crate = Some(item.hir_id);
58+
for &item_id in tcx.hir().root_module().item_ids {
59+
match tcx.hir().item(item_id).kind {
60+
::rustc_hir::ItemKind::ExternCrate(_) => {
61+
extern_crate = Some(item_id.hir_id());
9162
// Continue iterating to get the last `extern crate`
9263
}
9364
_ => {}
9465
}
9566
}
96-
let ext_cnum = tcx.extern_mod_stmt_cnum(extern_crate.unwrap().owner_def_id()).unwrap();
67+
let ext_cnum = tcx.extern_mod_stmt_cnum(extern_crate.unwrap().owner.def_id).unwrap();
9768
let crate_data = tcx.crate_data_as_rc_any(ext_cnum);
9869
f(crate_data.downcast_ref::<CrateMetadata>().unwrap());
9970
}

src/main.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
#![feature(rustc_private, concat_idents)]
22

3-
extern crate getopts;
4-
extern crate owning_ref;
5-
extern crate flate2;
63
extern crate termion;
74
extern crate clap;
85
extern crate regex;
96

10-
extern crate syntax;
117
extern crate rustc_data_structures;
128

13-
extern crate rustc;
14-
extern crate rustc_errors;
9+
extern crate rustc_middle as rustc;
1510
extern crate rustc_metadata;
16-
extern crate rustc_incremental;
1711
extern crate rustc_driver;
1812
extern crate rustc_interface;
19-
extern crate rustc_mir;
13+
extern crate rustc_session;
14+
extern crate rustc_hir;
15+
extern crate rustc_span;
16+
extern crate rustc_expand;
2017

2118
use clap::{Arg, App};
2219

@@ -39,7 +36,6 @@ fn main() {
3936
.subcommands(print::subcommands())
4037
.get_matches();
4138
let rlib = matches.value_of("CRATE").unwrap().to_string();
42-
//let args = ::std::env::args().collect::<Vec<_>>();
4339
let args = vec![rlib.clone(), "/some_nonexistent_dummy_path".to_string()];
4440
driver::call_with_crate_tcx(args, rlib, Box::new(move |tcx| {
4541
driver::with_crate_metadata(tcx, |metadata| {

src/print.rs

Lines changed: 47 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use std::fmt;
22

33
use rustc::ty::TyCtxt;
4-
use rustc::session::Session;
5-
use rustc::middle::lang_items::LangItem;
4+
use rustc_session::Session;
5+
use rustc_hir::lang_items::LangItem;
66
use rustc::middle::exported_symbols::ExportedSymbol;
7-
use rustc::hir::def::{Export, Def};
8-
use rustc::hir::def_id::{DefId, CrateNum, DefIndex, DefIndexAddressSpace};
9-
use rustc_metadata::cstore::{CrateMetadata, NativeLibraryKind};
7+
use rustc_hir::def::DefKind;
8+
use rustc_hir::def_id::{DefId, CrateNum, DefIndex, DefIndexAddressSpace};
9+
use rustc_metadata::rmeta::decoder::CrateMetadata;
10+
use rustc_span::def_id::CRATE_DEF_INDEX;
1011

1112
use termion::color::*;
1213
use clap::{App, SubCommand, Arg, ArgMatches};
@@ -48,7 +49,7 @@ macro_rules! commands {
4849
]
4950
}
5051

51-
pub fn print_for_matches<'a, 'tcx: 'a>(matches: &ArgMatches, tcx: TyCtxt<'a, 'tcx, 'tcx>, crate_data: &CrateMetadata) {
52+
pub fn print_for_matches<'tcx>(matches: &ArgMatches, tcx: TyCtxt<'tcx>, crate_data: &CrateMetadata) {
5253
let cmd = matches.subcommand_name();
5354
match cmd.unwrap() {
5455
$(
@@ -149,7 +150,7 @@ fn print_deps(tcx: TyCtxt, crate_data: &CrateMetadata, _matches: &ArgMatches) {
149150
}
150151

151152
header!("Native libraries:");
152-
for native_lib in crate_data.get_native_libraries(tcx.sess) {
153+
for native_lib in tcx.native_libraries(crate_data.cnum) {
153154
/*out_line!(native_lib.name,
154155
"{}",
155156
match native_lib.kind {
@@ -163,9 +164,7 @@ fn print_deps(tcx: TyCtxt, crate_data: &CrateMetadata, _matches: &ArgMatches) {
163164

164165
header!("Dylib dependency formats:");
165166
for dylib_deps in crate_data.get_dylib_dependency_formats() {
166-
let ext_crate_data = tcx.crate_data_as_rc_any(dylib_deps.0);
167-
let ext_crate_data = ext_crate_data.downcast_ref::<CrateMetadata>().unwrap();
168-
out_line!(ext_crate_data.name, "{:?}", dylib_deps.1);
167+
out_line!(tcx.crate_name(dylib_deps.0), "{:?}", dylib_deps.1);
169168
}
170169
}
171170

@@ -176,23 +175,23 @@ fn print_macros(tcx: TyCtxt, crate_data: &CrateMetadata, _matches: &ArgMatches)
176175
}
177176

178177
header!("Macros:");
179-
for_each_export(tcx, &crate_data, tcx.sess, &|export| {
180-
use syntax::ext::base::MacroKind;
181-
match export.def {
182-
Def::Macro(_def_id, macro_kind) => {
178+
for_each_export(tcx, &crate_data, tcx.sess, &|def_id| {
179+
use rustc_span::MacroKind;
180+
match tcx.def_kind(def_id) {
181+
DefKind::Macro(macro_kind) => {
183182
Some(match macro_kind {
184183
MacroKind::Bang => "macro!",
185184
MacroKind::Attr => "#[macro]",
186185
MacroKind::Derive => "#[derive(Macro)]",
187-
MacroKind::ProcMacroStub => "<proc-macro-stub>",
186+
// MacroKind::ProcMacroStub => "<proc-macro-stub>",
188187
})
189188
}
190189
_ => None
191190
}
192191
});
193192
}
194193

195-
fn print_symbols<'a, 'tcx: 'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>, crate_data: &CrateMetadata, _matches: &ArgMatches) {
194+
fn print_symbols<'tcx>(tcx: TyCtxt<'tcx>, crate_data: &CrateMetadata, _matches: &ArgMatches) {
196195
header!("Exported symbols:");
197196
if crate_data.proc_macros.is_none() {
198197
for (exported_symbol, export_level) in crate_data.exported_symbols(tcx).into_iter().take(50) {
@@ -215,34 +214,34 @@ fn print_symbols<'a, 'tcx: 'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>, crate_data: &CrateMe
215214
}
216215

217216
header!("Types:");
218-
for_each_export(tcx, &crate_data, tcx.sess, &|export| {
219-
match export.def {
220-
Def::Struct(_) |
221-
Def::Union(_) |
222-
Def::Enum(_) |
223-
Def::Trait(_) |
224-
Def::ForeignTy(_) => Some(export.def.kind_name()),
225-
Def::TyAlias(_) => Some("type"),
217+
for_each_export(tcx, &crate_data, tcx.sess, &|def_id| {
218+
match tcx.def_kind(def_id) {
219+
DefKind::Struct |
220+
DefKind::Union |
221+
DefKind::Enum |
222+
DefKind::Trait |
223+
DefKind::ForeignTy => Some(tcx.def_kind(def_id).descr(def_id)),
224+
DefKind::TyAlias => Some("type"),
226225
_ => None
227226
}
228227
});
229228

230229
header!("Items:");
231-
for_each_export(tcx, &crate_data, tcx.sess, &|export| {
232-
match export.def {
233-
Def::Fn(_) |
234-
Def::Struct(_) |
235-
Def::Union(_) |
236-
Def::Enum(_) |
237-
Def::Trait(_) |
238-
Def::TyAlias(_) |
239-
Def::ForeignTy(_) |
240-
Def::Macro(_, _) => None, // Already handled
241-
Def::Ctor(..) |
242-
Def::Variant(_) => None, // Not very useful
243-
Def::Const(_) => Some("const"),
244-
Def::Static(_, _) => Some("static"),
245-
_ => Some(export.def.kind_name()),
230+
for_each_export(tcx, &crate_data, tcx.sess, &|def_id| {
231+
match tcx.def_kind(def_id) {
232+
DefKind::Fn |
233+
DefKind::Struct |
234+
DefKind::Union |
235+
DefKind::Enum |
236+
DefKind::Trait |
237+
DefKind::TyAlias |
238+
DefKind::ForeignTy |
239+
DefKind::Macro(_) => None, // Already handled
240+
DefKind::Ctor(..) |
241+
DefKind::Variant => None, // Not very useful
242+
DefKind::Const => Some("const"),
243+
DefKind::Static(_) => Some("static"),
244+
other => Some(other.descr(def_id)),
246245
}
247246
});
248247
}
@@ -252,29 +251,28 @@ fn print_mir(tcx: TyCtxt, _crate_data: &CrateMetadata, matches: &ArgMatches) {
252251
let def_id = parse_defid_from_str(def_id);
253252
println!("mir for {}:\n", tcx.def_path_str(def_id));
254253
let mut mir = ::std::io::Cursor::new(Vec::new());
255-
::rustc_mir::util::write_mir_pretty(tcx, Some(def_id), &mut mir).unwrap();
254+
::rustc::mir::write_mir_pretty(tcx, Some(def_id), &mut mir).unwrap();
256255
let mir = String::from_utf8(mir.into_inner()).unwrap();
257256
println!("{}", mir);
258257
}
259258

260-
fn for_each_export<F: Fn(Export) -> Option<&'static str>>(tcx: TyCtxt, crate_data: &CrateMetadata, sess: &Session, callback: &F) {
261-
use rustc::hir::def_id::DefIndex;
262-
fn each_export_inner<F: Fn(Export) -> Option<&'static str>>(tcx: TyCtxt, crate_data: &CrateMetadata, id: DefIndex, callback: &F, sess: &Session) {
263-
crate_data.each_child_of_item(id, |e| {
264-
match e.def {
265-
Def::Mod(def_id) => {
259+
fn for_each_export<F: Fn(DefId) -> Option<&'static str>>(tcx: TyCtxt, crate_data: &CrateMetadata, sess: &Session, callback: &F) {
260+
fn each_export_inner<F: Fn(DefId) -> Option<&'static str>>(tcx: TyCtxt, crate_data: &CrateMetadata, id: DefIndex, callback: &F, sess: &Session) {
261+
crate_data.each_child_of_item(id, |def_id| {
262+
match tcx.def_kind(def_id) {
263+
DefKind::Mod => {
266264
//println!("mod {}", tcx.def_path_str(def_id));
267265
each_export_inner(tcx, crate_data, def_id.index, callback, sess);
268266
},
269267
_ => {
270-
if let Some(name) = callback(e) {
271-
println!(" {}{:<10}{} {}", Fg(Cyan), name, Fg(Reset), tcx.def_path_str(e.def.def_id()));
268+
if let Some(name) = callback(def_id) {
269+
println!(" {}{:<10}{} {}", Fg(Cyan), name, Fg(Reset), tcx.def_path_str(def_id));
272270
}
273271
},
274272
}
275273
}, sess);
276274
}
277-
each_export_inner(tcx, crate_data, ::rustc::hir::def_id::CRATE_DEF_INDEX, callback, sess);
275+
each_export_inner(tcx, crate_data, CRATE_DEF_INDEX, callback, sess);
278276
}
279277

280278
/*fn for_each_export<F: Fn(Export) -> Option<&'static str>>(tcx: TyCtxt, crate_data: &CrateMetadata, sess: &Session, callback: &F) {

0 commit comments

Comments
 (0)