Skip to content

Commit d9197db

Browse files
committed
UPDATE - migrate write.rs to new diagnostics infra
1 parent 086e70f commit d9197db

File tree

3 files changed

+75
-22
lines changed

3 files changed

+75
-22
lines changed

compiler/rustc_codegen_ssa/src/back/write.rs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ use super::link::{self, ensure_removed};
22
use super::lto::{self, SerializedModule};
33
use super::symbol_export::symbol_name_for_instance_in_crate;
44

5+
use crate::errors;
6+
use crate::traits::*;
57
use crate::{
68
CachedModuleCodegen, CodegenResults, CompiledModule, CrateInfo, ModuleCodegen, ModuleKind,
79
};
8-
9-
use crate::traits::*;
1010
use jobserver::{Acquired, Client};
1111
use rustc_data_structures::fx::FxHashMap;
1212
use rustc_data_structures::memmap::Mmap;
@@ -530,7 +530,7 @@ fn produce_final_output_artifacts(
530530
// Produce final compile outputs.
531531
let copy_gracefully = |from: &Path, to: &Path| {
532532
if let Err(e) = fs::copy(from, to) {
533-
sess.err(&format!("could not copy {:?} to {:?}: {}", from, to, e));
533+
sess.emit_err(errors::CopyPath::new(from, to, e));
534534
}
535535
};
536536

@@ -546,7 +546,7 @@ fn produce_final_output_artifacts(
546546
ensure_removed(sess.diagnostic(), &path);
547547
}
548548
} else {
549-
let ext = crate_output
549+
let extension = crate_output
550550
.temp_path(output_type, None)
551551
.extension()
552552
.unwrap()
@@ -557,19 +557,11 @@ fn produce_final_output_artifacts(
557557
if crate_output.outputs.contains_key(&output_type) {
558558
// 2) Multiple codegen units, with `--emit foo=some_name`. We have
559559
// no good solution for this case, so warn the user.
560-
sess.warn(&format!(
561-
"ignoring emit path because multiple .{} files \
562-
were produced",
563-
ext
564-
));
560+
sess.emit_warning(errors::IgnoringEmitPath { extension });
565561
} else if crate_output.single_output_file.is_some() {
566562
// 3) Multiple codegen units, with `-o some_name`. We have
567563
// no good solution for this case, so warn the user.
568-
sess.warn(&format!(
569-
"ignoring -o because multiple .{} files \
570-
were produced",
571-
ext
572-
));
564+
sess.emit_warning(errors::IgnoringOutput { extension });
573565
} else {
574566
// 4) Multiple codegen units, but no explicit name. We
575567
// just leave the `foo.0.x` files in place.
@@ -880,14 +872,19 @@ fn execute_copy_from_cache_work_item<B: ExtraBackendMethods>(
880872
);
881873
match link_or_copy(&source_file, &output_path) {
882874
Ok(_) => Some(output_path),
883-
Err(err) => {
884-
let diag_handler = cgcx.create_diag_handler();
885-
diag_handler.err(&format!(
886-
"unable to copy {} to {}: {}",
887-
source_file.display(),
888-
output_path.display(),
889-
err
890-
));
875+
Err(_) => {
876+
// FIXME:
877+
// Should we add Translations support in Handler, or should we pass a session here ?
878+
//
879+
// As Luis Cardoso mentioned here https://github.com/rust-lang/rust/pull/100753#discussion_r952975345,
880+
// Translations support in Handler is tricky because SessionDiagnostic is not a trait,
881+
// and we can't implement it in Handler because rustc_errors cannot depend on rustc_session.
882+
//
883+
// As for passing a session here, my understanding is that all these errors should be reported via
884+
// the Shared Handler, which leads us to probably having to support Translations in another way.
885+
886+
// let diag_handler = cgcx.create_diag_handler();
887+
// diag_handler.emit_err(errors::CopyPathBuf { source_file, output_path, error });
891888
None
892889
}
893890
}

compiler/rustc_codegen_ssa/src/errors.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
//! Errors emitted by codegen_ssa
22
3+
use rustc_errors::{DiagnosticArgValue, IntoDiagnosticArg};
34
use rustc_macros::SessionDiagnostic;
5+
use std::borrow::Cow;
46
use std::io::Error;
7+
use std::path::{Path, PathBuf};
58

69
#[derive(SessionDiagnostic)]
710
#[diag(codegen_ssa::missing_native_static_library)]
@@ -56,3 +59,48 @@ pub struct L4BenderExportingSymbolsUnimplemented;
5659
pub struct NoNatvisDirectory {
5760
pub error: Error,
5861
}
62+
63+
#[derive(SessionDiagnostic)]
64+
#[diag(codegen_ssa::copy_path_buf)]
65+
pub struct CopyPathBuf {
66+
pub source_file: PathBuf,
67+
pub output_path: PathBuf,
68+
pub error: Error,
69+
}
70+
71+
// Reports Paths using `Debug` implementation rather than Path's `Display` implementation.
72+
#[derive(SessionDiagnostic)]
73+
#[diag(codegen_ssa::copy_path)]
74+
pub struct CopyPath<'a> {
75+
from: DebugArgPath<'a>,
76+
to: DebugArgPath<'a>,
77+
error: Error,
78+
}
79+
80+
impl<'a> CopyPath<'a> {
81+
pub fn new(from: &'a Path, to: &'a Path, error: Error) -> CopyPath<'a> {
82+
CopyPath { from: DebugArgPath { path: from }, to: DebugArgPath { path: to }, error }
83+
}
84+
}
85+
86+
struct DebugArgPath<'a> {
87+
pub path: &'a Path,
88+
}
89+
90+
impl IntoDiagnosticArg for DebugArgPath<'_> {
91+
fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue<'static> {
92+
DiagnosticArgValue::Str(Cow::Owned(format!("{:?}", self.path)))
93+
}
94+
}
95+
96+
#[derive(SessionDiagnostic)]
97+
#[diag(codegen_ssa::ignoring_emit_path)]
98+
pub struct IgnoringEmitPath {
99+
pub extension: String,
100+
}
101+
102+
#[derive(SessionDiagnostic)]
103+
#[diag(codegen_ssa::ignoring_output)]
104+
pub struct IgnoringOutput {
105+
pub extension: String,
106+
}

compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,11 @@ codegen_ssa_linker_unsupported_modifier = `as-needed` modifier not supported for
1919
codegen_ssa_L4Bender_exporting_symbols_unimplemented = exporting symbols not implemented yet for L4Bender
2020
2121
codegen_ssa_no_natvis_directory = error enumerating natvis directory: {$error}
22+
23+
codegen_ssa_copy_path = could not copy {$from} to {$to}: {$error}
24+
25+
codegen_ssa_copy_path_buf = unable to copy {$source_file} to {$output_path}: {$error}
26+
27+
codegen_ssa_ignoring_emit_path = ignoring emit path because multiple .{$extension} files were produced
28+
29+
codegen_ssa_ignoring_output = ignoring -o because multiple .{$extension} files were produced

0 commit comments

Comments
 (0)