about summary refs log tree commit diff
path: root/compiler/rustc_session
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-12-07 11:57:14 +0000
committerbors <bors@rust-lang.org>2023-12-07 11:57:14 +0000
commit7df0c211ace4157009eebd015f1a083490faa0bc (patch)
treeee984fa1292d242a89bd1d41c390f4e9fd6babe5 /compiler/rustc_session
parentb9540b7db9489630094760287dad621e3730d6b0 (diff)
parent618409901ac354b57e6f3ea9fdbd6c4e88a10ef9 (diff)
downloadrust-7df0c211ace4157009eebd015f1a083490faa0bc.tar.gz
rust-7df0c211ace4157009eebd015f1a083490faa0bc.zip
Auto merge of #118635 - nnethercote:fewer-early-errors, r=davidtwco
Fewer early errors

r? `@davidtwco`
Diffstat (limited to 'compiler/rustc_session')
-rw-r--r--compiler/rustc_session/messages.ftl3
-rw-r--r--compiler/rustc_session/src/errors.rs6
-rw-r--r--compiler/rustc_session/src/session.rs37
3 files changed, 28 insertions, 18 deletions
diff --git a/compiler/rustc_session/messages.ftl b/compiler/rustc_session/messages.ftl
index 3a7959c332e..f2e646c70f5 100644
--- a/compiler/rustc_session/messages.ftl
+++ b/compiler/rustc_session/messages.ftl
@@ -16,6 +16,8 @@ session_crate_name_invalid = crate names cannot start with a `-`, but `{$s}` has
 
 session_expr_parentheses_needed = parentheses are required to parse this as an expression
 
+session_failed_to_create_profiler = failed to create profiler: {$err}
+
 session_feature_diagnostic_for_issue =
     see issue #{$n} <https://github.com/rust-lang/rust/issues/{$n}> for more information
 
@@ -73,6 +75,7 @@ session_not_supported = not supported
 session_nul_in_c_str = null characters in C string literals are not supported
 
 session_octal_float_literal_not_supported = octal float literal is not supported
+
 session_optimization_fuel_exhausted = optimization-fuel-exhausted: {$msg}
 
 session_profile_sample_use_file_does_not_exist = file `{$path}` passed to `-C profile-sample-use` does not exist.
diff --git a/compiler/rustc_session/src/errors.rs b/compiler/rustc_session/src/errors.rs
index 72c013eb549..7eed59709c8 100644
--- a/compiler/rustc_session/src/errors.rs
+++ b/compiler/rustc_session/src/errors.rs
@@ -444,3 +444,9 @@ pub(crate) struct FunctionReturnRequiresX86OrX8664;
 #[derive(Diagnostic)]
 #[diag(session_function_return_thunk_extern_requires_non_large_code_model)]
 pub(crate) struct FunctionReturnThunkExternRequiresNonLargeCodeModel;
+
+#[derive(Diagnostic)]
+#[diag(session_failed_to_create_profiler)]
+pub struct FailedToCreateProfiler {
+    pub err: String,
+}
diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs
index e9648d0d622..123e9c788f5 100644
--- a/compiler/rustc_session/src/session.rs
+++ b/compiler/rustc_session/src/session.rs
@@ -24,7 +24,7 @@ use rustc_errors::registry::Registry;
 use rustc_errors::{
     error_code, fallback_fluent_bundle, DiagnosticBuilder, DiagnosticId, DiagnosticMessage,
     ErrorGuaranteed, FluentBundle, Handler, IntoDiagnostic, LazyFallbackBundle, MultiSpan, Noted,
-    SubdiagnosticMessage, TerminalUrl,
+    TerminalUrl,
 };
 use rustc_macros::HashStable_Generic;
 pub use rustc_span::def_id::StableCrateId;
@@ -1349,7 +1349,7 @@ fn default_emitter(
 // JUSTIFICATION: literally session construction
 #[allow(rustc::bad_opt_access)]
 pub fn build_session(
-    handler: &EarlyErrorHandler,
+    early_handler: EarlyErrorHandler,
     sopts: config::Options,
     io: CompilerIO,
     bundle: Option<Lrc<rustc_errors::FluentBundle>>,
@@ -1379,12 +1379,13 @@ pub fn build_session(
         None => filesearch::get_or_default_sysroot().expect("Failed finding sysroot"),
     };
 
-    let target_cfg = config::build_target_config(handler, &sopts, target_override, &sysroot);
+    let target_cfg = config::build_target_config(&early_handler, &sopts, target_override, &sysroot);
     let host_triple = TargetTriple::from_triple(config::host_triple());
-    let (host, target_warnings) = Target::search(&host_triple, &sysroot)
-        .unwrap_or_else(|e| handler.early_error(format!("Error loading host specification: {e}")));
+    let (host, target_warnings) = Target::search(&host_triple, &sysroot).unwrap_or_else(|e| {
+        early_handler.early_error(format!("Error loading host specification: {e}"))
+    });
     for warning in target_warnings.warning_messages() {
-        handler.early_warn(warning)
+        early_handler.early_warn(warning)
     }
 
     let loader = file_loader.unwrap_or_else(|| Box::new(RealFileLoader));
@@ -1413,6 +1414,10 @@ pub fn build_session(
         span_diagnostic = span_diagnostic.with_ice_file(ice_file);
     }
 
+    // Now that the proper handler has been constructed, drop the early handler
+    // to prevent accidental use.
+    drop(early_handler);
+
     let self_profiler = if let SwitchWithOptPath::Enabled(ref d) = sopts.unstable_opts.self_profile
     {
         let directory =
@@ -1427,7 +1432,7 @@ pub fn build_session(
         match profiler {
             Ok(profiler) => Some(Arc::new(profiler)),
             Err(e) => {
-                handler.early_warn(format!("failed to create profiler: {e}"));
+                span_diagnostic.emit_warning(errors::FailedToCreateProfiler { err: e.to_string() });
                 None
             }
         }
@@ -1471,7 +1476,13 @@ pub fn build_session(
 
     // Check jobserver before getting `jobserver::client`.
     jobserver::check(|err| {
-        handler.early_warn_with_note(err, "the build environment is likely misconfigured")
+        #[allow(rustc::untranslatable_diagnostic)]
+        #[allow(rustc::diagnostic_outside_of_impl)]
+        parse_sess
+            .span_diagnostic
+            .struct_warn(err)
+            .note("the build environment is likely misconfigured")
+            .emit()
     });
 
     let sess = Session {
@@ -1781,16 +1792,6 @@ impl EarlyErrorHandler {
     pub fn early_warn(&self, msg: impl Into<DiagnosticMessage>) {
         self.handler.struct_warn(msg).emit()
     }
-
-    #[allow(rustc::untranslatable_diagnostic)]
-    #[allow(rustc::diagnostic_outside_of_impl)]
-    pub fn early_warn_with_note(
-        &self,
-        msg: impl Into<DiagnosticMessage>,
-        note: impl Into<SubdiagnosticMessage>,
-    ) {
-        self.handler.struct_warn(msg).note(note).emit()
-    }
 }
 
 fn mk_emitter(output: ErrorOutputType) -> Box<DynEmitter> {