Skip to content

Migrate rustc_driver to SessionDiagnostic #100890

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Start moving rustc_driver to SessionDiagnostic
  • Loading branch information
adriantombu committed Aug 25, 2022
commit dbe39d835e93d784076b96564176825e556e8824
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3820,6 +3820,7 @@ dependencies = [
"rustc_interface",
"rustc_lint",
"rustc_log",
"rustc_macros",
"rustc_metadata",
"rustc_middle",
"rustc_parse",
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_driver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ rustc_errors = { path = "../rustc_errors" }
rustc_feature = { path = "../rustc_feature" }
rustc_hir = { path = "../rustc_hir" }
rustc_hir_pretty = { path = "../rustc_hir_pretty" }
rustc_macros = { path = "../rustc_macros" }
rustc_metadata = { path = "../rustc_metadata" }
rustc_parse = { path = "../rustc_parse" }
rustc_plugin_impl = { path = "../rustc_plugin_impl" }
Expand Down
29 changes: 24 additions & 5 deletions compiler/rustc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#![feature(once_cell)]
#![recursion_limit = "256"]
#![allow(rustc::potential_query_instability)]
#![deny(rustc::untranslatable_diagnostic)]
#![deny(rustc::diagnostic_outside_of_impl)]

#[macro_use]
extern crate tracing;
Expand Down Expand Up @@ -56,6 +58,9 @@ use std::time::Instant;

pub mod args;
pub mod pretty;
mod session_diagnostics;

use crate::session_diagnostics::{RlinkNotAFile, RlinkUnableToDeserialize, RlinkUnableToRead};

/// Exit status code used for successful compilation and help output.
pub const EXIT_SUCCESS: i32 = 0;
Expand Down Expand Up @@ -545,7 +550,11 @@ fn handle_explain(registry: Registry, code: &str, output: ErrorOutputType) {

fn show_content_with_pager(content: &str) {
let pager_name = env::var_os("PAGER").unwrap_or_else(|| {
if cfg!(windows) { OsString::from("more.com") } else { OsString::from("less") }
if cfg!(windows) {
OsString::from("more.com")
} else {
OsString::from("less")
}
});

let mut fallback_to_println = false;
Expand Down Expand Up @@ -581,18 +590,24 @@ pub fn try_process_rlink(sess: &Session, compiler: &interface::Compiler) -> Comp
sess.init_crate_types(collect_crate_types(sess, &[]));
let outputs = compiler.build_output_filenames(sess, &[]);
let rlink_data = fs::read(file).unwrap_or_else(|err| {
sess.fatal(&format!("failed to read rlink file: {}", err));
sess.fatal(RlinkUnableToRead {
span: Default::default(),
error_message: err.to_string(),
});
});
let codegen_results = match CodegenResults::deserialize_rlink(rlink_data) {
Ok(codegen) => codegen,
Err(error) => {
sess.fatal(&format!("Could not deserialize .rlink file: {error}"));
sess.fatal(RlinkUnableToDeserialize {
span: Default::default(),
error_message: error.to_string(),
});
}
};
let result = compiler.codegen_backend().link(sess, codegen_results, &outputs);
abort_on_err(result, sess);
} else {
sess.fatal("rlink must be a file")
sess.fatal(RlinkNotAFile { span: Default::default() })
}
Compilation::Stop
} else {
Expand Down Expand Up @@ -1116,7 +1131,11 @@ fn extra_compiler_flags() -> Option<(Vec<String>, bool)> {
}
}

if !result.is_empty() { Some((result, excluded_cargo_defaults)) } else { None }
if !result.is_empty() {
Some((result, excluded_cargo_defaults))
} else {
None
}
}

/// Runs a closure and catches unwinds triggered by fatal errors.
Expand Down
25 changes: 25 additions & 0 deletions compiler/rustc_driver/src/session_diagnostics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use rustc_macros::SessionDiagnostic;
use rustc_span::Span;

#[derive(SessionDiagnostic)]
#[error(driver::rlink_unable_to_read)]
pub(crate) struct RlinkUnableToRead {
#[primary_span]
pub span: Span,
pub error_message: String,
}

#[derive(SessionDiagnostic)]
#[error(driver::rlink_unable_to_deserialize)]
pub(crate) struct RlinkUnableToDeserialize {
#[primary_span]
pub span: Span,
pub error_message: String,
}

#[derive(SessionDiagnostic)]
#[error(driver::rlink_no_a_file)]
pub(crate) struct RlinkNotAFile {
#[primary_span]
pub span: Span,
}
5 changes: 5 additions & 0 deletions compiler/rustc_error_messages/locales/en-US/driver.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
driver_rlink_unable_to_read = failed to read rlink file: `{$error_message}`

driver_rlink_unable_to_deserialize = could not deserialize .rlink file: `{$error_message}`

driver_rlink_no_a_file = rlink must be a file
1 change: 1 addition & 0 deletions compiler/rustc_error_messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ fluent_messages! {
borrowck => "../locales/en-US/borrowck.ftl",
builtin_macros => "../locales/en-US/builtin_macros.ftl",
const_eval => "../locales/en-US/const_eval.ftl",
driver => "../locales/en-US/driver.ftl",
expand => "../locales/en-US/expand.ftl",
interface => "../locales/en-US/interface.ftl",
lint => "../locales/en-US/lint.ftl",
Expand Down