Skip to content

Commit 677d4d0

Browse files
committed
session: diagnostic migration lint on more fns
Apply the diagnostic migration lint to more functions on `Session`. Signed-off-by: David Wood <[email protected]>
1 parent 7d7cd17 commit 677d4d0

File tree

13 files changed

+122
-37
lines changed

13 files changed

+122
-37
lines changed

compiler/rustc_driver/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,9 @@ static DEFAULT_HOOK: LazyLock<Box<dyn Fn(&panic::PanicInfo<'_>) + Sync + Send +
12051205
///
12061206
/// When `install_ice_hook` is called, this function will be called as the panic
12071207
/// hook.
1208+
// FIXME(translation): need struct diagnostics implemented on `Handler`
1209+
#[allow(rustc::untranslatable_diagnostic)]
1210+
#[allow(rustc::diagnostic_outside_of_impl)]
12081211
pub fn report_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) {
12091212
let fallback_bundle =
12101213
rustc_errors::fallback_fluent_bundle(rustc_errors::DEFAULT_LOCALE_RESOURCES, false);

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

+10
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,13 @@ interface_rustc_error_unexpected_annotation =
4141
4242
interface_failed_writing_file =
4343
failed to write file {$path}: {$error}"
44+
45+
interface_unsupported_crate_type_for_target =
46+
dropping unsupported crate type `{$crate_type}` for target `{$target_triple}`
47+
48+
interface_multiple_output_types_adaption =
49+
due to multiple output types requested, the explicitly specified output file name will be adapted for each output type
50+
51+
interface_ignoring_extra_filename = ignoring -C extra-filename flag due to -o flag
52+
53+
interface_ignoring_out_dir = ignoring --out-dir flag due to -o flag

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

+6
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,9 @@ monomorphize_large_assignments =
2424
2525
monomorphize_requires_lang_item =
2626
requires `{$lang_item}` lang_item
27+
28+
monomorphize_encountered_error_while_instantiating =
29+
the above error was encountered while instantiating `{$formatted_item}`
30+
31+
monomorphize_unknown_cgu_collection_mode =
32+
unknown codegen-item collection mode '{$mode}', falling back to 'lazy' mode

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

+2
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,5 @@ session_crate_name_invalid = crate names cannot start with a `-`, but `{$s}` has
6666
session_crate_name_empty = crate name must not be empty
6767
6868
session_invalid_character_in_create_name = invalid character `{$character}` in crate name: `{$crate_name}`
69+
70+
session_optimization_fuel_exhausted = optimization-fuel-exhausted: {$msg}

compiler/rustc_errors/src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,7 @@ impl Handler {
936936
self.emit_diag_at_span(Diagnostic::new(Bug, msg), span);
937937
}
938938

939+
#[rustc_lint_diagnostics]
939940
pub fn span_note_without_error(
940941
&self,
941942
span: impl Into<MultiSpan>,
@@ -944,6 +945,7 @@ impl Handler {
944945
self.emit_diag_at_span(Diagnostic::new(Note, msg), span);
945946
}
946947

948+
#[rustc_lint_diagnostics]
947949
pub fn span_note_diag(
948950
&self,
949951
span: Span,
@@ -955,19 +957,23 @@ impl Handler {
955957
}
956958

957959
// NOTE: intentionally doesn't raise an error so rustc_codegen_ssa only reports fatal errors in the main thread
960+
#[rustc_lint_diagnostics]
958961
pub fn fatal(&self, msg: impl Into<DiagnosticMessage>) -> FatalError {
959962
self.inner.borrow_mut().fatal(msg)
960963
}
961964

965+
#[rustc_lint_diagnostics]
962966
pub fn err(&self, msg: impl Into<DiagnosticMessage>) -> ErrorGuaranteed {
963967
self.inner.borrow_mut().err(msg)
964968
}
965969

970+
#[rustc_lint_diagnostics]
966971
pub fn warn(&self, msg: impl Into<DiagnosticMessage>) {
967972
let mut db = DiagnosticBuilder::new(self, Warning(None), msg);
968973
db.emit();
969974
}
970975

976+
#[rustc_lint_diagnostics]
971977
pub fn note_without_error(&self, msg: impl Into<DiagnosticMessage>) {
972978
DiagnosticBuilder::new(self, Note, msg).emit();
973979
}

compiler/rustc_interface/Cargo.toml

+1-3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ rustc_plugin_impl = { path = "../rustc_plugin_impl" }
4545
rustc_privacy = { path = "../rustc_privacy" }
4646
rustc_query_impl = { path = "../rustc_query_impl" }
4747
rustc_resolve = { path = "../rustc_resolve" }
48+
rustc_target = { path = "../rustc_target" }
4849
rustc_trait_selection = { path = "../rustc_trait_selection" }
4950
rustc_ty_utils = { path = "../rustc_ty_utils" }
5051

@@ -54,9 +55,6 @@ libc = "0.2"
5455
[target.'cfg(windows)'.dependencies]
5556
winapi = { version = "0.3", features = ["libloaderapi"] }
5657

57-
[dev-dependencies]
58-
rustc_target = { path = "../rustc_target" }
59-
6058
[features]
6159
llvm = ['rustc_codegen_llvm']
6260
rustc_use_parallel_compiler = ['rayon', 'rustc-rayon-core', 'rustc_query_impl/rustc_use_parallel_compiler']

compiler/rustc_interface/src/errors.rs

+21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use rustc_macros::SessionDiagnostic;
2+
use rustc_session::config::CrateType;
23
use rustc_span::{Span, Symbol};
4+
use rustc_target::spec::TargetTriple;
35

46
use std::io;
57
use std::path::Path;
@@ -87,3 +89,22 @@ pub struct FailedWritingFile<'a> {
8789
pub path: &'a Path,
8890
pub error: io::Error,
8991
}
92+
93+
#[derive(SessionDiagnostic)]
94+
#[diag(interface::unsupported_crate_type_for_target)]
95+
pub struct UnsupportedCrateTypeForTarget<'a> {
96+
pub crate_type: CrateType,
97+
pub target_triple: &'a TargetTriple,
98+
}
99+
100+
#[derive(SessionDiagnostic)]
101+
#[diag(interface::multiple_output_types_adaption)]
102+
pub struct MultipleOutputTypesAdaption;
103+
104+
#[derive(SessionDiagnostic)]
105+
#[diag(interface::ignoring_extra_filename)]
106+
pub struct IgnoringExtraFilename;
107+
108+
#[derive(SessionDiagnostic)]
109+
#[diag(interface::ignoring_out_dir)]
110+
pub struct IgnoringOutDir;

compiler/rustc_interface/src/util.rs

+12-15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::errors;
12
use info;
23
use libloading::Library;
34
use rustc_ast as ast;
@@ -572,16 +573,15 @@ pub fn collect_crate_types(session: &Session, attrs: &[ast::Attribute]) -> Vec<C
572573
}
573574

574575
base.retain(|crate_type| {
575-
let res = !output::invalid_output_for_target(session, *crate_type);
576-
577-
if !res {
578-
session.warn(&format!(
579-
"dropping unsupported crate type `{}` for target `{}`",
580-
*crate_type, session.opts.target_triple
581-
));
576+
if output::invalid_output_for_target(session, *crate_type) {
577+
session.emit_warning(errors::UnsupportedCrateTypeForTarget {
578+
crate_type: *crate_type,
579+
target_triple: &session.opts.target_triple,
580+
});
581+
false
582+
} else {
583+
true
582584
}
583-
584-
res
585585
});
586586

587587
base
@@ -624,19 +624,16 @@ pub fn build_output_filenames(
624624
let unnamed_output_types =
625625
sess.opts.output_types.values().filter(|a| a.is_none()).count();
626626
let ofile = if unnamed_output_types > 1 {
627-
sess.warn(
628-
"due to multiple output types requested, the explicitly specified \
629-
output file name will be adapted for each output type",
630-
);
627+
sess.emit_warning(errors::MultipleOutputTypesAdaption);
631628
None
632629
} else {
633630
if !sess.opts.cg.extra_filename.is_empty() {
634-
sess.warn("ignoring -C extra-filename flag due to -o flag");
631+
sess.emit_warning(errors::IgnoringExtraFilename);
635632
}
636633
Some(out_file.clone())
637634
};
638635
if *odir != None {
639-
sess.warn("ignoring --out-dir flag due to -o flag");
636+
sess.emit_warning(errors::IgnoringOutDir);
640637
}
641638

642639
OutputFilenames::new(

compiler/rustc_monomorphize/src/collector.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,10 @@ use std::iter;
201201
use std::ops::Range;
202202
use std::path::PathBuf;
203203

204-
use crate::errors::{LargeAssignmentsLint, RecursionLimit, RequiresLangItem, TypeLengthLimit};
204+
use crate::errors::{
205+
EncounteredErrorWhileInstantiating, LargeAssignmentsLint, RecursionLimit, RequiresLangItem,
206+
TypeLengthLimit,
207+
};
205208

206209
#[derive(PartialEq)]
207210
pub enum MonoItemCollectionMode {
@@ -524,10 +527,10 @@ fn collect_items_rec<'tcx>(
524527
&& starting_point.node.is_user_defined()
525528
{
526529
let formatted_item = with_no_trimmed_paths!(starting_point.node.to_string());
527-
tcx.sess.span_note_without_error(
528-
starting_point.span,
529-
&format!("the above error was encountered while instantiating `{}`", formatted_item),
530-
);
530+
tcx.sess.emit_note(EncounteredErrorWhileInstantiating {
531+
span: starting_point.span,
532+
formatted_item,
533+
});
531534
}
532535
inlining_map.lock_mut().record_accesses(starting_point.node, &neighbors.items);
533536

compiler/rustc_monomorphize/src/errors.rs

+14
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,17 @@ pub struct SymbolAlreadyDefined {
8383
pub span: Option<Span>,
8484
pub symbol: String,
8585
}
86+
87+
#[derive(SessionDiagnostic)]
88+
#[diag(monomorphize::encountered_error_while_instantiating)]
89+
pub struct EncounteredErrorWhileInstantiating {
90+
#[primary_span]
91+
pub span: Span,
92+
pub formatted_item: String,
93+
}
94+
95+
#[derive(SessionDiagnostic)]
96+
#[diag(monomorphize::unknown_cgu_collection_mode)]
97+
pub struct UnknownCguCollectionMode<'a> {
98+
pub mode: &'a str,
99+
}

compiler/rustc_monomorphize/src/partitioning/mod.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ use rustc_span::symbol::Symbol;
108108

109109
use crate::collector::InliningMap;
110110
use crate::collector::{self, MonoItemCollectionMode};
111-
use crate::errors::{SymbolAlreadyDefined, UnknownPartitionStrategy};
111+
use crate::errors::{SymbolAlreadyDefined, UnknownCguCollectionMode, UnknownPartitionStrategy};
112112

113113
pub struct PartitioningCx<'a, 'tcx> {
114114
tcx: TyCtxt<'tcx>,
@@ -345,18 +345,13 @@ fn collect_and_partition_mono_items<'tcx>(
345345
) -> (&'tcx DefIdSet, &'tcx [CodegenUnit<'tcx>]) {
346346
let collection_mode = match tcx.sess.opts.unstable_opts.print_mono_items {
347347
Some(ref s) => {
348-
let mode_string = s.to_lowercase();
349-
let mode_string = mode_string.trim();
350-
if mode_string == "eager" {
348+
let mode = s.to_lowercase();
349+
let mode = mode.trim();
350+
if mode == "eager" {
351351
MonoItemCollectionMode::Eager
352352
} else {
353-
if mode_string != "lazy" {
354-
let message = format!(
355-
"Unknown codegen-item collection mode '{}'. \
356-
Falling back to 'lazy' mode.",
357-
mode_string
358-
);
359-
tcx.sess.warn(&message);
353+
if mode != "lazy" {
354+
tcx.sess.emit_warning(UnknownCguCollectionMode { mode });
360355
}
361356

362357
MonoItemCollectionMode::Lazy

compiler/rustc_session/src/errors.rs

+6
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,9 @@ impl crate::SessionDiagnostic<'_> for InvalidCharacterInCrateName<'_> {
219219
diag
220220
}
221221
}
222+
223+
#[derive(SessionDiagnostic)]
224+
#[diag(session::optimization_fuel_exhausted)]
225+
pub struct OptimisationFuelExhausted {
226+
pub msg: String,
227+
}

compiler/rustc_session/src/session.rs

+27-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ pub use crate::code_stats::{DataTypeKind, FieldInfo, SizeKind, VariantInfo};
44
use crate::config::{self, CrateType, InstrumentCoverage, OptLevel, OutputType, SwitchWithOptPath};
55
use crate::errors::{
66
CannotEnableCrtStaticLinux, CannotMixAndMatchSanitizers, LinkerPluginToWindowsNotSupported,
7-
NotCircumventFeature, ProfileSampleUseFileDoesNotExist, ProfileUseFileDoesNotExist,
8-
SanitizerCfiEnabled, SanitizerNotSupported, SanitizersNotSupported,
7+
NotCircumventFeature, OptimisationFuelExhausted, ProfileSampleUseFileDoesNotExist,
8+
ProfileUseFileDoesNotExist, SanitizerCfiEnabled, SanitizerNotSupported, SanitizersNotSupported,
99
SplitDebugInfoUnstablePlatform, StackProtectorNotSupportedForTarget,
1010
TargetRequiresUnwindTables, UnstableVirtualFunctionElimination, UnsupportedDwarfVersion,
1111
};
@@ -510,6 +510,8 @@ impl Session {
510510
self.diagnostic().span_err_with_code(sp, msg, code)
511511
}
512512
#[rustc_lint_diagnostics]
513+
#[allow(rustc::untranslatable_diagnostic)]
514+
#[allow(rustc::diagnostic_outside_of_impl)]
513515
pub fn err(&self, msg: impl Into<DiagnosticMessage>) -> ErrorGuaranteed {
514516
self.diagnostic().err(msg)
515517
}
@@ -595,11 +597,19 @@ impl Session {
595597
Err(ErrorGuaranteed::unchecked_claim_error_was_emitted())
596598
}
597599
}
600+
601+
// #[allow(rustc::untranslatable_diagnostic)]
602+
// #[allow(rustc::diagnostic_outside_of_impl)]
603+
#[rustc_lint_diagnostics]
598604
#[allow(rustc::untranslatable_diagnostic)]
599605
#[allow(rustc::diagnostic_outside_of_impl)]
600606
pub fn span_warn<S: Into<MultiSpan>>(&self, sp: S, msg: impl Into<DiagnosticMessage>) {
601607
self.diagnostic().span_warn(sp, msg)
602608
}
609+
610+
// #[allow(rustc::untranslatable_diagnostic)]
611+
// #[allow(rustc::diagnostic_outside_of_impl)]
612+
#[rustc_lint_diagnostics]
603613
#[allow(rustc::untranslatable_diagnostic)]
604614
#[allow(rustc::diagnostic_outside_of_impl)]
605615
pub fn span_warn_with_code<S: Into<MultiSpan>>(
@@ -610,6 +620,10 @@ impl Session {
610620
) {
611621
self.diagnostic().span_warn_with_code(sp, msg, code)
612622
}
623+
624+
#[rustc_lint_diagnostics]
625+
#[allow(rustc::untranslatable_diagnostic)]
626+
#[allow(rustc::diagnostic_outside_of_impl)]
613627
pub fn warn(&self, msg: impl Into<DiagnosticMessage>) {
614628
self.diagnostic().warn(msg)
615629
}
@@ -640,16 +654,26 @@ impl Session {
640654
self.diagnostic().delay_good_path_bug(msg)
641655
}
642656

657+
#[rustc_lint_diagnostics]
658+
#[allow(rustc::untranslatable_diagnostic)]
659+
#[allow(rustc::diagnostic_outside_of_impl)]
643660
pub fn note_without_error(&self, msg: impl Into<DiagnosticMessage>) {
644661
self.diagnostic().note_without_error(msg)
645662
}
663+
#[rustc_lint_diagnostics]
664+
#[allow(rustc::untranslatable_diagnostic)]
665+
#[allow(rustc::diagnostic_outside_of_impl)]
646666
pub fn span_note_without_error<S: Into<MultiSpan>>(
647667
&self,
648668
sp: S,
649669
msg: impl Into<DiagnosticMessage>,
650670
) {
651671
self.diagnostic().span_note_without_error(sp, msg)
652672
}
673+
674+
// #[allow(rustc::untranslatable_diagnostic)]
675+
// #[allow(rustc::diagnostic_outside_of_impl)]
676+
#[rustc_lint_diagnostics]
653677
#[allow(rustc::untranslatable_diagnostic)]
654678
#[allow(rustc::diagnostic_outside_of_impl)]
655679
pub fn struct_note_without_error(
@@ -879,7 +903,7 @@ impl Session {
879903
// We only call `msg` in case we can actually emit warnings.
880904
// Otherwise, this could cause a `delay_good_path_bug` to
881905
// trigger (issue #79546).
882-
self.warn(&format!("optimization-fuel-exhausted: {}", msg()));
906+
self.emit_warning(OptimisationFuelExhausted { msg: msg() });
883907
}
884908
fuel.out_of_fuel = true;
885909
} else if fuel.remaining > 0 {

0 commit comments

Comments
 (0)