Skip to content

Commit f09112a

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 f09112a

File tree

3 files changed

+76
-89
lines changed

3 files changed

+76
-89
lines changed

src/driver.rs

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
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

1010
fn find_sysroot() -> String {
1111
if let Ok(sysroot) = ::std::env::var("MIRI_SYSROOT") {
@@ -27,34 +27,25 @@ fn find_sysroot() -> String {
2727
}
2828
}
2929

30-
pub fn call_with_crate_tcx(mut args: Vec<String>, rlib: String, f: Box<Cb>) {
30+
pub fn call_with_crate_tcx(mut args: Vec<String>, rlib: String, f: Box<Cb>) -> ! {
3131
println!("Reading rlib {}", rlib);
3232

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-
3933
args.push("-Cpanic=abort".to_string()); // otherwise we have to provide `eh_personality`
4034

4135
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);
36+
std::process::exit(rustc_driver::catch_with_exit_code(move || {
37+
let args: Vec<String> = std::env::args().collect();
38+
rustc_driver::RunCompiler::new(&args, &mut MyCompilerCalls(f)).run()
39+
}))
5140
}
5241

5342
struct MyFileLoader(String);
5443

55-
impl ::syntax::source_map::FileLoader for MyFileLoader {
44+
impl ::rustc_span::source_map::FileLoader for MyFileLoader {
5645
fn file_exists(&self, _: &Path) -> bool { true }
57-
fn abs_path(&self, _: &Path) -> Option<PathBuf> { None }
46+
fn read_binary_file(&self, _: &Path) -> std::io::Result<Vec<u8>> {
47+
Err(std::io::Error::new(std::io::ErrorKind::Unsupported, "should not need to load binary files"))
48+
}
5849
fn read_file(&self, _: &Path) -> Result<String, ::std::io::Error> {
5950
Ok(format!("
6051
#![feature(no_core)]
@@ -68,32 +59,32 @@ impl ::syntax::source_map::FileLoader for MyFileLoader {
6859
}
6960
}
7061

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

7364
struct MyCompilerCalls(Box<Cb>);
7465

7566
impl Callbacks for MyCompilerCalls {
76-
fn after_analysis(&mut self, compiler: &Compiler) -> bool {
77-
compiler.global_ctxt().unwrap().take().enter(|tcx| {
67+
fn after_analysis<'tcx>(&mut self, compiler: &Compiler, queries: &'tcx Queries<'tcx>) -> Compilation {
68+
queries.global_ctxt().unwrap().enter(|tcx| {
7869
(self.0)(tcx);
7970
});
80-
false
71+
Compilation::Stop
8172
}
8273

8374
}
8475

85-
pub fn with_crate_metadata<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, f: impl for<'b> FnOnce(&'b CrateMetadata)) {
76+
pub fn with_crate_metadata<'tcx>(tcx: TyCtxt<'tcx>, f: impl for<'b> FnOnce(&'b CrateMetadata)) {
8677
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);
78+
for &item_id in tcx.hir().root_module().item_ids {
79+
match tcx.hir().item(item_id).kind {
80+
::rustc_hir::ItemKind::ExternCrate(_) => {
81+
extern_crate = Some(item_id.hir_id());
9182
// Continue iterating to get the last `extern crate`
9283
}
9384
_ => {}
9485
}
9586
}
96-
let ext_cnum = tcx.extern_mod_stmt_cnum(extern_crate.unwrap().owner_def_id()).unwrap();
87+
let ext_cnum = tcx.extern_mod_stmt_cnum(extern_crate.unwrap().owner.def_id).unwrap();
9788
let crate_data = tcx.crate_data_as_rc_any(ext_cnum);
9889
f(crate_data.downcast_ref::<CrateMetadata>().unwrap());
9990
}

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: 49 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,9 @@ 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+
// let ext_crate_data = tcx.cstore_untracked().get_crate_data(dylib_deps.0);
168+
// let ext_crate_data = ext_crate_data.downcast_ref::<CrateMetadata>().unwrap();
169+
out_line!(tcx.crate_name(dylib_deps.0), "{:?}", dylib_deps.1);
169170
}
170171
}
171172

@@ -176,23 +177,23 @@ fn print_macros(tcx: TyCtxt, crate_data: &CrateMetadata, _matches: &ArgMatches)
176177
}
177178

178179
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) => {
180+
for_each_export(tcx, &crate_data, tcx.sess, &|def_id| {
181+
use rustc_span::MacroKind;
182+
match tcx.def_kind(def_id) {
183+
DefKind::Macro(macro_kind) => {
183184
Some(match macro_kind {
184185
MacroKind::Bang => "macro!",
185186
MacroKind::Attr => "#[macro]",
186187
MacroKind::Derive => "#[derive(Macro)]",
187-
MacroKind::ProcMacroStub => "<proc-macro-stub>",
188+
// MacroKind::ProcMacroStub => "<proc-macro-stub>",
188189
})
189190
}
190191
_ => None
191192
}
192193
});
193194
}
194195

195-
fn print_symbols<'a, 'tcx: 'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>, crate_data: &CrateMetadata, _matches: &ArgMatches) {
196+
fn print_symbols<'tcx>(tcx: TyCtxt<'tcx>, crate_data: &CrateMetadata, _matches: &ArgMatches) {
196197
header!("Exported symbols:");
197198
if crate_data.proc_macros.is_none() {
198199
for (exported_symbol, export_level) in crate_data.exported_symbols(tcx).into_iter().take(50) {
@@ -215,34 +216,34 @@ fn print_symbols<'a, 'tcx: 'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>, crate_data: &CrateMe
215216
}
216217

217218
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"),
219+
for_each_export(tcx, &crate_data, tcx.sess, &|def_id| {
220+
match tcx.def_kind(def_id) {
221+
DefKind::Struct |
222+
DefKind::Union |
223+
DefKind::Enum |
224+
DefKind::Trait |
225+
DefKind::ForeignTy => Some(tcx.def_kind(def_id).descr(def_id)),
226+
DefKind::TyAlias => Some("type"),
226227
_ => None
227228
}
228229
});
229230

230231
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()),
232+
for_each_export(tcx, &crate_data, tcx.sess, &|def_id| {
233+
match tcx.def_kind(def_id) {
234+
DefKind::Fn |
235+
DefKind::Struct |
236+
DefKind::Union |
237+
DefKind::Enum |
238+
DefKind::Trait |
239+
DefKind::TyAlias |
240+
DefKind::ForeignTy |
241+
DefKind::Macro(_) => None, // Already handled
242+
DefKind::Ctor(..) |
243+
DefKind::Variant => None, // Not very useful
244+
DefKind::Const => Some("const"),
245+
DefKind::Static(_) => Some("static"),
246+
other => Some(other.descr(def_id)),
246247
}
247248
});
248249
}
@@ -252,29 +253,28 @@ fn print_mir(tcx: TyCtxt, _crate_data: &CrateMetadata, matches: &ArgMatches) {
252253
let def_id = parse_defid_from_str(def_id);
253254
println!("mir for {}:\n", tcx.def_path_str(def_id));
254255
let mut mir = ::std::io::Cursor::new(Vec::new());
255-
::rustc_mir::util::write_mir_pretty(tcx, Some(def_id), &mut mir).unwrap();
256+
::rustc::mir::write_mir_pretty(tcx, Some(def_id), &mut mir).unwrap();
256257
let mir = String::from_utf8(mir.into_inner()).unwrap();
257258
println!("{}", mir);
258259
}
259260

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) => {
261+
fn for_each_export<F: Fn(DefId) -> Option<&'static str>>(tcx: TyCtxt, crate_data: &CrateMetadata, sess: &Session, callback: &F) {
262+
fn each_export_inner<F: Fn(DefId) -> Option<&'static str>>(tcx: TyCtxt, crate_data: &CrateMetadata, id: DefIndex, callback: &F, sess: &Session) {
263+
crate_data.each_child_of_item(id, |def_id| {
264+
match tcx.def_kind(def_id) {
265+
DefKind::Mod => {
266266
//println!("mod {}", tcx.def_path_str(def_id));
267267
each_export_inner(tcx, crate_data, def_id.index, callback, sess);
268268
},
269269
_ => {
270-
if let Some(name) = callback(e) {
271-
println!(" {}{:<10}{} {}", Fg(Cyan), name, Fg(Reset), tcx.def_path_str(e.def.def_id()));
270+
if let Some(name) = callback(def_id) {
271+
println!(" {}{:<10}{} {}", Fg(Cyan), name, Fg(Reset), tcx.def_path_str(def_id));
272272
}
273273
},
274274
}
275275
}, sess);
276276
}
277-
each_export_inner(tcx, crate_data, ::rustc::hir::def_id::CRATE_DEF_INDEX, callback, sess);
277+
each_export_inner(tcx, crate_data, CRATE_DEF_INDEX, callback, sess);
278278
}
279279

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

0 commit comments

Comments
 (0)