Skip to content

Commit 350674b

Browse files
committed
Auto merge of #58250 - Zoxc:rustc-interface-1, r=oli-obk
Introduce rustc_interface and move some methods there Split out from #56732 r? @oli-obk
2 parents 1999a22 + 23a51f9 commit 350674b

File tree

29 files changed

+1334
-1055
lines changed

29 files changed

+1334
-1055
lines changed

Cargo.lock

+31
Original file line numberDiff line numberDiff line change
@@ -2699,6 +2699,7 @@ dependencies = [
26992699
"rustc_data_structures 0.0.0",
27002700
"rustc_errors 0.0.0",
27012701
"rustc_incremental 0.0.0",
2702+
"rustc_interface 0.0.0",
27022703
"rustc_lint 0.0.0",
27032704
"rustc_metadata 0.0.0",
27042705
"rustc_mir 0.0.0",
@@ -2751,6 +2752,36 @@ dependencies = [
27512752
"syntax_pos 0.0.0",
27522753
]
27532754

2755+
[[package]]
2756+
name = "rustc_interface"
2757+
version = "0.0.0"
2758+
dependencies = [
2759+
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
2760+
"rustc 0.0.0",
2761+
"rustc-rayon 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
2762+
"rustc_allocator 0.0.0",
2763+
"rustc_borrowck 0.0.0",
2764+
"rustc_codegen_utils 0.0.0",
2765+
"rustc_data_structures 0.0.0",
2766+
"rustc_errors 0.0.0",
2767+
"rustc_incremental 0.0.0",
2768+
"rustc_lint 0.0.0",
2769+
"rustc_metadata 0.0.0",
2770+
"rustc_mir 0.0.0",
2771+
"rustc_passes 0.0.0",
2772+
"rustc_plugin 0.0.0",
2773+
"rustc_privacy 0.0.0",
2774+
"rustc_resolve 0.0.0",
2775+
"rustc_traits 0.0.0",
2776+
"rustc_typeck 0.0.0",
2777+
"scoped-tls 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
2778+
"serialize 0.0.0",
2779+
"smallvec 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)",
2780+
"syntax 0.0.0",
2781+
"syntax_ext 0.0.0",
2782+
"syntax_pos 0.0.0",
2783+
]
2784+
27542785
[[package]]
27552786
name = "rustc_lint"
27562787
version = "0.0.0"

src/bootstrap/bin/rustc.rs

+2
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ fn main() {
107107
// actually downloaded, so we just always pass the `--sysroot` option.
108108
cmd.arg("--sysroot").arg(&sysroot);
109109

110+
cmd.arg("-Zexternal-macro-backtrace");
111+
110112
// When we build Rust dylibs they're all intended for intermediate
111113
// usage, so make sure we pass the -Cprefer-dynamic flag instead of
112114
// linking all deps statically into the dylib.

src/librustc/dep_graph/dep_node.rs

+1
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ define_dep_nodes!( <'tcx>
456456
[eval_always] CoherenceInherentImplOverlapCheck,
457457
[] CoherenceCheckTrait(DefId),
458458
[eval_always] PrivacyAccessLevels(CrateNum),
459+
[eval_always] Analysis(CrateNum),
459460

460461
// Represents the MIR for a fn; also used as the task node for
461462
// things read/modify that MIR.

src/librustc/session/config.rs

+7
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,13 @@ impl Input {
499499
Input::Str { ref mut input, .. } => Some(input),
500500
}
501501
}
502+
503+
pub fn source_name(&self) -> FileName {
504+
match *self {
505+
Input::File(ref ifile) => ifile.clone().into(),
506+
Input::Str { ref name, .. } => name.clone(),
507+
}
508+
}
502509
}
503510

504511
#[derive(Clone, Hash)]

src/librustc/session/mod.rs

+66-43
Original file line numberDiff line numberDiff line change
@@ -899,14 +899,14 @@ impl Session {
899899

900900
/// Returns the number of query threads that should be used for this
901901
/// compilation
902-
pub fn threads_from_opts(opts: &config::Options) -> usize {
903-
opts.debugging_opts.threads.unwrap_or(::num_cpus::get())
902+
pub fn threads_from_count(query_threads: Option<usize>) -> usize {
903+
query_threads.unwrap_or(::num_cpus::get())
904904
}
905905

906906
/// Returns the number of query threads that should be used for this
907907
/// compilation
908908
pub fn threads(&self) -> usize {
909-
Self::threads_from_opts(&self.opts)
909+
Self::threads_from_count(self.opts.debugging_opts.threads)
910910
}
911911

912912
/// Returns the number of codegen units that should be used for this
@@ -1023,16 +1023,67 @@ pub fn build_session(
10231023
local_crate_source_file,
10241024
registry,
10251025
Lrc::new(source_map::SourceMap::new(file_path_mapping)),
1026-
None,
1026+
DiagnosticOutput::Default,
1027+
Default::default(),
10271028
)
10281029
}
10291030

1031+
fn default_emitter(
1032+
sopts: &config::Options,
1033+
registry: errors::registry::Registry,
1034+
source_map: &Lrc<source_map::SourceMap>,
1035+
emitter_dest: Option<Box<dyn Write + Send>>,
1036+
) -> Box<dyn Emitter + sync::Send> {
1037+
match (sopts.error_format, emitter_dest) {
1038+
(config::ErrorOutputType::HumanReadable(color_config), None) => Box::new(
1039+
EmitterWriter::stderr(
1040+
color_config,
1041+
Some(source_map.clone()),
1042+
false,
1043+
sopts.debugging_opts.teach,
1044+
).ui_testing(sopts.debugging_opts.ui_testing),
1045+
),
1046+
(config::ErrorOutputType::HumanReadable(_), Some(dst)) => Box::new(
1047+
EmitterWriter::new(dst, Some(source_map.clone()), false, false)
1048+
.ui_testing(sopts.debugging_opts.ui_testing),
1049+
),
1050+
(config::ErrorOutputType::Json(pretty), None) => Box::new(
1051+
JsonEmitter::stderr(
1052+
Some(registry),
1053+
source_map.clone(),
1054+
pretty,
1055+
).ui_testing(sopts.debugging_opts.ui_testing),
1056+
),
1057+
(config::ErrorOutputType::Json(pretty), Some(dst)) => Box::new(
1058+
JsonEmitter::new(
1059+
dst,
1060+
Some(registry),
1061+
source_map.clone(),
1062+
pretty,
1063+
).ui_testing(sopts.debugging_opts.ui_testing),
1064+
),
1065+
(config::ErrorOutputType::Short(color_config), None) => Box::new(
1066+
EmitterWriter::stderr(color_config, Some(source_map.clone()), true, false),
1067+
),
1068+
(config::ErrorOutputType::Short(_), Some(dst)) => {
1069+
Box::new(EmitterWriter::new(dst, Some(source_map.clone()), true, false))
1070+
}
1071+
}
1072+
}
1073+
1074+
pub enum DiagnosticOutput {
1075+
Default,
1076+
Raw(Box<dyn Write + Send>),
1077+
Emitter(Box<dyn Emitter + Send + sync::Send>)
1078+
}
1079+
10301080
pub fn build_session_with_source_map(
10311081
sopts: config::Options,
10321082
local_crate_source_file: Option<PathBuf>,
10331083
registry: errors::registry::Registry,
10341084
source_map: Lrc<source_map::SourceMap>,
1035-
emitter_dest: Option<Box<dyn Write + Send>>,
1085+
diagnostics_output: DiagnosticOutput,
1086+
lint_caps: FxHashMap<lint::LintId, lint::Level>,
10361087
) -> Session {
10371088
// FIXME: This is not general enough to make the warning lint completely override
10381089
// normal diagnostic warnings, since the warning lint can also be denied and changed
@@ -1054,42 +1105,13 @@ pub fn build_session_with_source_map(
10541105

10551106
let external_macro_backtrace = sopts.debugging_opts.external_macro_backtrace;
10561107

1057-
let emitter: Box<dyn Emitter + sync::Send> =
1058-
match (sopts.error_format, emitter_dest) {
1059-
(config::ErrorOutputType::HumanReadable(color_config), None) => Box::new(
1060-
EmitterWriter::stderr(
1061-
color_config,
1062-
Some(source_map.clone()),
1063-
false,
1064-
sopts.debugging_opts.teach,
1065-
).ui_testing(sopts.debugging_opts.ui_testing),
1066-
),
1067-
(config::ErrorOutputType::HumanReadable(_), Some(dst)) => Box::new(
1068-
EmitterWriter::new(dst, Some(source_map.clone()), false, false)
1069-
.ui_testing(sopts.debugging_opts.ui_testing),
1070-
),
1071-
(config::ErrorOutputType::Json(pretty), None) => Box::new(
1072-
JsonEmitter::stderr(
1073-
Some(registry),
1074-
source_map.clone(),
1075-
pretty,
1076-
).ui_testing(sopts.debugging_opts.ui_testing),
1077-
),
1078-
(config::ErrorOutputType::Json(pretty), Some(dst)) => Box::new(
1079-
JsonEmitter::new(
1080-
dst,
1081-
Some(registry),
1082-
source_map.clone(),
1083-
pretty,
1084-
).ui_testing(sopts.debugging_opts.ui_testing),
1085-
),
1086-
(config::ErrorOutputType::Short(color_config), None) => Box::new(
1087-
EmitterWriter::stderr(color_config, Some(source_map.clone()), true, false),
1088-
),
1089-
(config::ErrorOutputType::Short(_), Some(dst)) => {
1090-
Box::new(EmitterWriter::new(dst, Some(source_map.clone()), true, false))
1091-
}
1092-
};
1108+
let emitter = match diagnostics_output {
1109+
DiagnosticOutput::Default => default_emitter(&sopts, registry, &source_map, None),
1110+
DiagnosticOutput::Raw(write) => {
1111+
default_emitter(&sopts, registry, &source_map, Some(write))
1112+
}
1113+
DiagnosticOutput::Emitter(emitter) => emitter,
1114+
};
10931115

10941116
let diagnostic_handler = errors::Handler::with_emitter_and_flags(
10951117
emitter,
@@ -1103,14 +1125,15 @@ pub fn build_session_with_source_map(
11031125
},
11041126
);
11051127

1106-
build_session_(sopts, local_crate_source_file, diagnostic_handler, source_map)
1128+
build_session_(sopts, local_crate_source_file, diagnostic_handler, source_map, lint_caps)
11071129
}
11081130

11091131
pub fn build_session_(
11101132
sopts: config::Options,
11111133
local_crate_source_file: Option<PathBuf>,
11121134
span_diagnostic: errors::Handler,
11131135
source_map: Lrc<source_map::SourceMap>,
1136+
driver_lint_caps: FxHashMap<lint::LintId, lint::Level>,
11141137
) -> Session {
11151138
let host_triple = TargetTriple::from_triple(config::host_triple());
11161139
let host = Target::search(&host_triple).unwrap_or_else(|e|
@@ -1235,7 +1258,7 @@ pub fn build_session_(
12351258
},
12361259
has_global_allocator: Once::new(),
12371260
has_panic_handler: Once::new(),
1238-
driver_lint_caps: Default::default(),
1261+
driver_lint_caps,
12391262
};
12401263

12411264
validate_commandline_args_with_session_available(&sess);

src/librustc/ty/context.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ use rustc_data_structures::stable_hasher::{HashStable, hash_stable_hashmap,
5050
StableVec};
5151
use arena::{TypedArena, SyncDroplessArena};
5252
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
53-
use rustc_data_structures::sync::{self, Lrc, Lock, WorkerLocal};
53+
use rustc_data_structures::sync::{Lrc, Lock, WorkerLocal};
5454
use std::any::Any;
5555
use std::borrow::Borrow;
5656
use std::cmp::Ordering;
@@ -1285,8 +1285,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
12851285

12861286
let gcx = arenas.global_ctxt.as_ref().unwrap();
12871287

1288-
sync::assert_send_val(&gcx);
1289-
12901288
let r = tls::enter_global(gcx, f);
12911289

12921290
gcx.queries.record_computed_queries(s);

src/librustc/ty/query/config.rs

+6
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,12 @@ impl<'tcx> QueryDescription<'tcx> for queries::extern_crate<'tcx> {
611611
}
612612
}
613613

614+
impl<'tcx> QueryDescription<'tcx> for queries::analysis<'tcx> {
615+
fn describe(_tcx: TyCtxt<'_, '_, '_>, _: CrateNum) -> Cow<'static, str> {
616+
"running analysis passes on this crate".into()
617+
}
618+
}
619+
614620
impl<'tcx> QueryDescription<'tcx> for queries::lint_levels<'tcx> {
615621
fn describe(_tcx: TyCtxt<'_, '_, '_>, _: CrateNum) -> Cow<'static, str> {
616622
"computing the lint levels for items in this crate".into()

src/librustc/ty/query/mod.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::mir::interpret::{ConstEvalRawResult, ConstEvalResult};
1919
use crate::mir::mono::CodegenUnit;
2020
use crate::mir;
2121
use crate::mir::interpret::GlobalId;
22-
use crate::session::{CompileResult, CrateDisambiguator};
22+
use crate::session::CrateDisambiguator;
2323
use crate::session::config::{EntryFnType, OutputFilenames, OptLevel};
2424
use crate::traits::{self, Vtable};
2525
use crate::traits::query::{
@@ -99,6 +99,9 @@ pub use self::on_disk_cache::OnDiskCache;
9999
// as they will raise an fatal error on query cycles instead.
100100
define_queries! { <'tcx>
101101
Other {
102+
/// Run analysis passes on the crate
103+
[] fn analysis: Analysis(CrateNum) -> Result<(), ErrorReported>,
104+
102105
/// Records the type of every item.
103106
[] fn type_of: TypeOfItem(DefId) -> Ty<'tcx>,
104107

@@ -290,7 +293,8 @@ define_queries! { <'tcx>
290293
},
291294

292295
TypeChecking {
293-
[] fn typeck_item_bodies: typeck_item_bodies_dep_node(CrateNum) -> CompileResult,
296+
[] fn typeck_item_bodies:
297+
typeck_item_bodies_dep_node(CrateNum) -> Result<(), ErrorReported>,
294298

295299
[] fn typeck_tables_of: TypeckTables(DefId) -> &'tcx ty::TypeckTables<'tcx>,
296300
},

src/librustc/ty/query/plumbing.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1357,6 +1357,7 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
13571357
DepKind::CrateHash => { force!(crate_hash, krate!()); }
13581358
DepKind::OriginalCrateName => { force!(original_crate_name, krate!()); }
13591359
DepKind::ExtraFileName => { force!(extra_filename, krate!()); }
1360+
DepKind::Analysis => { force!(analysis, krate!()); }
13601361

13611362
DepKind::AllTraitImplementations => {
13621363
force!(all_trait_implementations, krate!());

src/librustc_driver/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ rustc_save_analysis = { path = "../librustc_save_analysis" }
3333
rustc_traits = { path = "../librustc_traits" }
3434
rustc_codegen_utils = { path = "../librustc_codegen_utils" }
3535
rustc_typeck = { path = "../librustc_typeck" }
36+
rustc_interface = { path = "../librustc_interface" }
3637
serialize = { path = "../libserialize" }
3738
syntax = { path = "../libsyntax" }
3839
smallvec = { version = "0.6.7", features = ["union", "may_dangle"] }

0 commit comments

Comments
 (0)