From de388882563652ab91106f3046a5e7ec93f3ba17 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 19 Mar 2024 13:31:28 +1100 Subject: [PATCH] Reduce `pub` usage in `rustc_session`. In particular, almost none of the errors in `errors.rs` are used outside the crate. --- compiler/rustc_session/src/config.rs | 54 ++++--- compiler/rustc_session/src/errors.rs | 208 +++++++++++++-------------- 2 files changed, 130 insertions(+), 132 deletions(-) diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index e56684808bb..e6eb1a3e83c 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -313,7 +313,7 @@ pub struct LocationDetail { } impl LocationDetail { - pub fn all() -> Self { + pub(crate) fn all() -> Self { Self { file: true, line: true, column: true } } } @@ -549,7 +549,7 @@ pub fn new(entries: &[(OutputType, Option)]) -> OutputTypes { OutputTypes(BTreeMap::from_iter(entries.iter().map(|&(k, ref v)| (k, v.clone())))) } - pub fn get(&self, key: &OutputType) -> Option<&Option> { + pub(crate) fn get(&self, key: &OutputType) -> Option<&Option> { self.0.get(key) } @@ -662,10 +662,6 @@ pub fn get(&self, key: &str) -> Option<&ExternEntry> { pub fn iter(&self) -> BTreeMapIter<'_, String, ExternEntry> { self.0.iter() } - - pub fn len(&self) -> usize { - self.0.len() - } } impl ExternEntry { @@ -854,13 +850,13 @@ pub fn overwrite(&self, content: &str, sess: &Session) { #[derive(Clone, Hash, Debug, HashStable_Generic, Encodable, Decodable)] pub struct OutputFilenames { - pub out_directory: PathBuf, + pub(crate) out_directory: PathBuf, /// Crate name. Never contains '-'. crate_stem: String, /// Typically based on `.rs` input file name. Any '-' is preserved. filestem: String, pub single_output_file: Option, - pub temps_directory: Option, + temps_directory: Option, pub outputs: OutputTypes, } @@ -898,7 +894,7 @@ pub fn path(&self, flavor: OutputType) -> OutFileName { /// Gets the output path where a compilation artifact of the given type /// should be placed on disk. - pub fn output_path(&self, flavor: OutputType) -> PathBuf { + fn output_path(&self, flavor: OutputType) -> PathBuf { let extension = flavor.extension(); match flavor { OutputType::Metadata => { @@ -1092,7 +1088,7 @@ pub fn build_dep_graph(&self) -> bool { || self.unstable_opts.query_dep_graph } - pub fn file_path_mapping(&self) -> FilePathMapping { + pub(crate) fn file_path_mapping(&self) -> FilePathMapping { file_path_mapping(self.remap_path_prefix.clone(), &self.unstable_opts) } @@ -1173,14 +1169,14 @@ pub enum Passes { } impl Passes { - pub fn is_empty(&self) -> bool { + fn is_empty(&self) -> bool { match *self { Passes::Some(ref v) => v.is_empty(), Passes::All => false, } } - pub fn extend(&mut self, passes: impl IntoIterator) { + pub(crate) fn extend(&mut self, passes: impl IntoIterator) { match *self { Passes::Some(ref mut v) => v.extend(passes), Passes::All => {} @@ -1206,7 +1202,7 @@ pub struct BranchProtection { pub pac_ret: Option, } -pub const fn default_lib_output() -> CrateType { +pub(crate) const fn default_lib_output() -> CrateType { CrateType::Rlib } @@ -1584,15 +1580,15 @@ pub fn build_target_config( } #[derive(Copy, Clone, PartialEq, Eq, Debug)] -pub enum OptionStability { +enum OptionStability { Stable, Unstable, } pub struct RustcOptGroup { pub apply: Box &mut getopts::Options>, - pub name: &'static str, - pub stability: OptionStability, + name: &'static str, + stability: OptionStability, } impl RustcOptGroup { @@ -1628,8 +1624,8 @@ mod opt { use super::RustcOptGroup; - pub type R = RustcOptGroup; - pub type S = &'static str; + type R = RustcOptGroup; + type S = &'static str; fn stable(name: S, f: F) -> R where @@ -1649,32 +1645,34 @@ fn longer(a: S, b: S) -> S { if a.len() > b.len() { a } else { b } } - pub fn opt_s(a: S, b: S, c: S, d: S) -> R { + pub(crate) fn opt_s(a: S, b: S, c: S, d: S) -> R { stable(longer(a, b), move |opts| opts.optopt(a, b, c, d)) } - pub fn multi_s(a: S, b: S, c: S, d: S) -> R { + pub(crate) fn multi_s(a: S, b: S, c: S, d: S) -> R { stable(longer(a, b), move |opts| opts.optmulti(a, b, c, d)) } - pub fn flag_s(a: S, b: S, c: S) -> R { + pub(crate) fn flag_s(a: S, b: S, c: S) -> R { stable(longer(a, b), move |opts| opts.optflag(a, b, c)) } - pub fn flagmulti_s(a: S, b: S, c: S) -> R { + pub(crate) fn flagmulti_s(a: S, b: S, c: S) -> R { stable(longer(a, b), move |opts| opts.optflagmulti(a, b, c)) } - pub fn opt(a: S, b: S, c: S, d: S) -> R { + fn opt(a: S, b: S, c: S, d: S) -> R { unstable(longer(a, b), move |opts| opts.optopt(a, b, c, d)) } - pub fn multi(a: S, b: S, c: S, d: S) -> R { + pub(crate) fn multi(a: S, b: S, c: S, d: S) -> R { unstable(longer(a, b), move |opts| opts.optmulti(a, b, c, d)) } } + static EDITION_STRING: LazyLock = LazyLock::new(|| { format!( "Specify which edition of the compiler to use when compiling code. \ The default is {DEFAULT_EDITION} and the latest stable edition is {LATEST_STABLE_EDITION}." ) }); + /// Returns the "short" subset of the rustc command line options, /// including metadata for each option, such as whether the option is /// part of the stable long-term interface for rustc. @@ -1864,9 +1862,9 @@ pub fn parse_color(early_dcx: &EarlyDiagCtxt, matches: &getopts::Matches) -> Col /// Possible json config files pub struct JsonConfig { pub json_rendered: HumanReadableErrorType, - pub json_artifact_notifications: bool, + json_artifact_notifications: bool, pub json_unused_externs: JsonUnusedExterns, - pub json_future_incompat: bool, + json_future_incompat: bool, } /// Report unused externs in event stream @@ -2992,7 +2990,7 @@ pub fn match_is_nightly_build(matches: &getopts::Matches) -> bool { is_nightly_build(matches.opt_str("crate-name").as_deref()) } - pub fn is_nightly_build(krate: Option<&str>) -> bool { + fn is_nightly_build(krate: Option<&str>) -> bool { UnstableFeatures::from_environment(krate).is_nightly_build() } @@ -3199,7 +3197,7 @@ pub(crate) mod dep_tracking { use std::num::NonZero; use std::path::PathBuf; - pub trait DepTrackingHash { + pub(crate) trait DepTrackingHash { fn hash( &self, hasher: &mut DefaultHasher, diff --git a/compiler/rustc_session/src/errors.rs b/compiler/rustc_session/src/errors.rs index d523da1ad7e..94dfbf16923 100644 --- a/compiler/rustc_session/src/errors.rs +++ b/compiler/rustc_session/src/errors.rs @@ -12,9 +12,9 @@ use crate::{config::CrateType, parse::ParseSess}; -pub struct FeatureGateError { - pub span: MultiSpan, - pub explain: DiagMessage, +pub(crate) struct FeatureGateError { + pub(crate) span: MultiSpan, + pub(crate) explain: DiagMessage, } impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for FeatureGateError { @@ -26,22 +26,22 @@ fn into_diag(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> { #[derive(Subdiagnostic)] #[note(session_feature_diagnostic_for_issue)] -pub struct FeatureDiagnosticForIssue { - pub n: NonZero, +pub(crate) struct FeatureDiagnosticForIssue { + pub(crate) n: NonZero, } #[derive(Subdiagnostic)] #[note(session_feature_suggest_upgrade_compiler)] -pub struct SuggestUpgradeCompiler { +pub(crate) struct SuggestUpgradeCompiler { date: &'static str, } impl SuggestUpgradeCompiler { - pub fn ui_testing() -> Self { + pub(crate) fn ui_testing() -> Self { Self { date: "YYYY-MM-DD" } } - pub fn new() -> Option { + pub(crate) fn new() -> Option { let date = option_env!("CFG_VER_DATE")?; Some(Self { date }) @@ -50,108 +50,108 @@ pub fn new() -> Option { #[derive(Subdiagnostic)] #[help(session_feature_diagnostic_help)] -pub struct FeatureDiagnosticHelp { - pub feature: Symbol, +pub(crate) struct FeatureDiagnosticHelp { + pub(crate) feature: Symbol, } #[derive(Subdiagnostic)] #[help(session_cli_feature_diagnostic_help)] -pub struct CliFeatureDiagnosticHelp { - pub feature: Symbol, +pub(crate) struct CliFeatureDiagnosticHelp { + pub(crate) feature: Symbol, } #[derive(Diagnostic)] #[diag(session_not_circumvent_feature)] -pub struct NotCircumventFeature; +pub(crate) struct NotCircumventFeature; #[derive(Diagnostic)] #[diag(session_linker_plugin_lto_windows_not_supported)] -pub struct LinkerPluginToWindowsNotSupported; +pub(crate) struct LinkerPluginToWindowsNotSupported; #[derive(Diagnostic)] #[diag(session_profile_use_file_does_not_exist)] -pub struct ProfileUseFileDoesNotExist<'a> { - pub path: &'a std::path::Path, +pub(crate) struct ProfileUseFileDoesNotExist<'a> { + pub(crate) path: &'a std::path::Path, } #[derive(Diagnostic)] #[diag(session_profile_sample_use_file_does_not_exist)] -pub struct ProfileSampleUseFileDoesNotExist<'a> { - pub path: &'a std::path::Path, +pub(crate) struct ProfileSampleUseFileDoesNotExist<'a> { + pub(crate) path: &'a std::path::Path, } #[derive(Diagnostic)] #[diag(session_target_requires_unwind_tables)] -pub struct TargetRequiresUnwindTables; +pub(crate) struct TargetRequiresUnwindTables; #[derive(Diagnostic)] #[diag(session_instrumentation_not_supported)] -pub struct InstrumentationNotSupported { - pub us: String, +pub(crate) struct InstrumentationNotSupported { + pub(crate) us: String, } #[derive(Diagnostic)] #[diag(session_sanitizer_not_supported)] -pub struct SanitizerNotSupported { - pub us: String, +pub(crate) struct SanitizerNotSupported { + pub(crate) us: String, } #[derive(Diagnostic)] #[diag(session_sanitizers_not_supported)] -pub struct SanitizersNotSupported { - pub us: String, +pub(crate) struct SanitizersNotSupported { + pub(crate) us: String, } #[derive(Diagnostic)] #[diag(session_cannot_mix_and_match_sanitizers)] -pub struct CannotMixAndMatchSanitizers { - pub first: String, - pub second: String, +pub(crate) struct CannotMixAndMatchSanitizers { + pub(crate) first: String, + pub(crate) second: String, } #[derive(Diagnostic)] #[diag(session_cannot_enable_crt_static_linux)] -pub struct CannotEnableCrtStaticLinux; +pub(crate) struct CannotEnableCrtStaticLinux; #[derive(Diagnostic)] #[diag(session_sanitizer_cfi_requires_lto)] -pub struct SanitizerCfiRequiresLto; +pub(crate) struct SanitizerCfiRequiresLto; #[derive(Diagnostic)] #[diag(session_sanitizer_cfi_requires_single_codegen_unit)] -pub struct SanitizerCfiRequiresSingleCodegenUnit; +pub(crate) struct SanitizerCfiRequiresSingleCodegenUnit; #[derive(Diagnostic)] #[diag(session_sanitizer_cfi_canonical_jump_tables_requires_cfi)] -pub struct SanitizerCfiCanonicalJumpTablesRequiresCfi; +pub(crate) struct SanitizerCfiCanonicalJumpTablesRequiresCfi; #[derive(Diagnostic)] #[diag(session_sanitizer_cfi_generalize_pointers_requires_cfi)] -pub struct SanitizerCfiGeneralizePointersRequiresCfi; +pub(crate) struct SanitizerCfiGeneralizePointersRequiresCfi; #[derive(Diagnostic)] #[diag(session_sanitizer_cfi_normalize_integers_requires_cfi)] -pub struct SanitizerCfiNormalizeIntegersRequiresCfi; +pub(crate) struct SanitizerCfiNormalizeIntegersRequiresCfi; #[derive(Diagnostic)] #[diag(session_split_lto_unit_requires_lto)] -pub struct SplitLtoUnitRequiresLto; +pub(crate) struct SplitLtoUnitRequiresLto; #[derive(Diagnostic)] #[diag(session_unstable_virtual_function_elimination)] -pub struct UnstableVirtualFunctionElimination; +pub(crate) struct UnstableVirtualFunctionElimination; #[derive(Diagnostic)] #[diag(session_unsupported_dwarf_version)] -pub struct UnsupportedDwarfVersion { - pub dwarf_version: u32, +pub(crate) struct UnsupportedDwarfVersion { + pub(crate) dwarf_version: u32, } #[derive(Diagnostic)] #[diag(session_target_stack_protector_not_supported)] -pub struct StackProtectorNotSupportedForTarget<'a> { - pub stack_protector: StackProtector, - pub target_triple: &'a TargetTriple, +pub(crate) struct StackProtectorNotSupportedForTarget<'a> { + pub(crate) stack_protector: StackProtector, + pub(crate) target_triple: &'a TargetTriple, } #[derive(Diagnostic)] @@ -160,58 +160,58 @@ pub struct StackProtectorNotSupportedForTarget<'a> { #[derive(Diagnostic)] #[diag(session_split_debuginfo_unstable_platform)] -pub struct SplitDebugInfoUnstablePlatform { - pub debuginfo: SplitDebuginfo, +pub(crate) struct SplitDebugInfoUnstablePlatform { + pub(crate) debuginfo: SplitDebuginfo, } #[derive(Diagnostic)] #[diag(session_file_is_not_writeable)] -pub struct FileIsNotWriteable<'a> { - pub file: &'a std::path::Path, +pub(crate) struct FileIsNotWriteable<'a> { + pub(crate) file: &'a std::path::Path, } #[derive(Diagnostic)] #[diag(session_file_write_fail)] pub(crate) struct FileWriteFail<'a> { - pub path: &'a std::path::Path, - pub err: String, + pub(crate) path: &'a std::path::Path, + pub(crate) err: String, } #[derive(Diagnostic)] #[diag(session_crate_name_does_not_match)] -pub struct CrateNameDoesNotMatch { +pub(crate) struct CrateNameDoesNotMatch { #[primary_span] - pub span: Span, - pub s: Symbol, - pub name: Symbol, + pub(crate) span: Span, + pub(crate) s: Symbol, + pub(crate) name: Symbol, } #[derive(Diagnostic)] #[diag(session_crate_name_invalid)] -pub struct CrateNameInvalid<'a> { - pub s: &'a str, +pub(crate) struct CrateNameInvalid<'a> { + pub(crate) s: &'a str, } #[derive(Diagnostic)] #[diag(session_crate_name_empty)] -pub struct CrateNameEmpty { +pub(crate) struct CrateNameEmpty { #[primary_span] - pub span: Option, + pub(crate) span: Option, } #[derive(Diagnostic)] #[diag(session_invalid_character_in_create_name)] -pub struct InvalidCharacterInCrateName { +pub(crate) struct InvalidCharacterInCrateName { #[primary_span] - pub span: Option, - pub character: char, - pub crate_name: Symbol, + pub(crate) span: Option, + pub(crate) character: char, + pub(crate) crate_name: Symbol, #[subdiagnostic] - pub crate_name_help: Option, + pub(crate) crate_name_help: Option, } #[derive(Subdiagnostic)] -pub enum InvalidCrateNameHelp { +pub(crate) enum InvalidCrateNameHelp { #[help(session_invalid_character_in_create_name_help)] AddCrateName, } @@ -220,9 +220,9 @@ pub enum InvalidCrateNameHelp { #[multipart_suggestion(session_expr_parentheses_needed, applicability = "machine-applicable")] pub struct ExprParenthesesNeeded { #[suggestion_part(code = "(")] - pub left: Span, + left: Span, #[suggestion_part(code = ")")] - pub right: Span, + right: Span, } impl ExprParenthesesNeeded { @@ -233,13 +233,13 @@ pub fn surrounding(s: Span) -> Self { #[derive(Diagnostic)] #[diag(session_skipping_const_checks)] -pub struct SkippingConstChecks { +pub(crate) struct SkippingConstChecks { #[subdiagnostic] - pub unleashed_features: Vec, + pub(crate) unleashed_features: Vec, } #[derive(Subdiagnostic)] -pub enum UnleashedFeatureHelp { +pub(crate) enum UnleashedFeatureHelp { #[help(session_unleashed_feature_help_named)] Named { #[primary_span] @@ -255,101 +255,101 @@ pub enum UnleashedFeatureHelp { #[derive(Diagnostic)] #[diag(session_invalid_literal_suffix)] -pub(crate) struct InvalidLiteralSuffix<'a> { +struct InvalidLiteralSuffix<'a> { #[primary_span] #[label] - pub span: Span, + span: Span, // FIXME(#100717) - pub kind: &'a str, - pub suffix: Symbol, + kind: &'a str, + suffix: Symbol, } #[derive(Diagnostic)] #[diag(session_invalid_int_literal_width)] #[help] -pub(crate) struct InvalidIntLiteralWidth { +struct InvalidIntLiteralWidth { #[primary_span] - pub span: Span, - pub width: String, + span: Span, + width: String, } #[derive(Diagnostic)] #[diag(session_invalid_num_literal_base_prefix)] #[note] -pub(crate) struct InvalidNumLiteralBasePrefix { +struct InvalidNumLiteralBasePrefix { #[primary_span] #[suggestion(applicability = "maybe-incorrect", code = "{fixed}")] - pub span: Span, - pub fixed: String, + span: Span, + fixed: String, } #[derive(Diagnostic)] #[diag(session_invalid_num_literal_suffix)] #[help] -pub(crate) struct InvalidNumLiteralSuffix { +struct InvalidNumLiteralSuffix { #[primary_span] #[label] - pub span: Span, - pub suffix: String, + span: Span, + suffix: String, } #[derive(Diagnostic)] #[diag(session_invalid_float_literal_width)] #[help] -pub(crate) struct InvalidFloatLiteralWidth { +struct InvalidFloatLiteralWidth { #[primary_span] - pub span: Span, - pub width: String, + span: Span, + width: String, } #[derive(Diagnostic)] #[diag(session_invalid_float_literal_suffix)] #[help] -pub(crate) struct InvalidFloatLiteralSuffix { +struct InvalidFloatLiteralSuffix { #[primary_span] #[label] - pub span: Span, - pub suffix: String, + span: Span, + suffix: String, } #[derive(Diagnostic)] #[diag(session_int_literal_too_large)] #[note] -pub(crate) struct IntLiteralTooLarge { +struct IntLiteralTooLarge { #[primary_span] - pub span: Span, - pub limit: String, + span: Span, + limit: String, } #[derive(Diagnostic)] #[diag(session_hexadecimal_float_literal_not_supported)] -pub(crate) struct HexadecimalFloatLiteralNotSupported { +struct HexadecimalFloatLiteralNotSupported { #[primary_span] #[label(session_not_supported)] - pub span: Span, + span: Span, } #[derive(Diagnostic)] #[diag(session_octal_float_literal_not_supported)] -pub(crate) struct OctalFloatLiteralNotSupported { +struct OctalFloatLiteralNotSupported { #[primary_span] #[label(session_not_supported)] - pub span: Span, + span: Span, } #[derive(Diagnostic)] #[diag(session_binary_float_literal_not_supported)] -pub(crate) struct BinaryFloatLiteralNotSupported { +struct BinaryFloatLiteralNotSupported { #[primary_span] #[label(session_not_supported)] - pub span: Span, + span: Span, } #[derive(Diagnostic)] #[diag(session_unsupported_crate_type_for_target)] -pub struct UnsupportedCrateTypeForTarget<'a> { - pub crate_type: CrateType, - pub target_triple: &'a TargetTriple, +pub(crate) struct UnsupportedCrateTypeForTarget<'a> { + pub(crate) crate_type: CrateType, + pub(crate) target_triple: &'a TargetTriple, } pub fn report_lit_error( @@ -431,16 +431,16 @@ fn fix_base_capitalisation(prefix: &str, suffix: &str) -> Option { #[derive(Diagnostic)] #[diag(session_optimization_fuel_exhausted)] -pub struct OptimisationFuelExhausted { - pub msg: String, +pub(crate) struct OptimisationFuelExhausted { + pub(crate) msg: String, } #[derive(Diagnostic)] #[diag(session_incompatible_linker_flavor)] #[note] -pub struct IncompatibleLinkerFlavor { - pub flavor: &'static str, - pub compatible_list: String, +pub(crate) struct IncompatibleLinkerFlavor { + pub(crate) flavor: &'static str, + pub(crate) compatible_list: String, } #[derive(Diagnostic)] @@ -453,6 +453,6 @@ pub struct IncompatibleLinkerFlavor { #[derive(Diagnostic)] #[diag(session_failed_to_create_profiler)] -pub struct FailedToCreateProfiler { - pub err: String, +pub(crate) struct FailedToCreateProfiler { + pub(crate) err: String, }