1
Fork 0

session: use derive more

Signed-off-by: David Wood <david.wood@huawei.com>
This commit is contained in:
David Wood 2022-10-14 13:25:43 +01:00
parent 21d3bbd8b6
commit f8b628bce4
3 changed files with 46 additions and 32 deletions

View file

@ -54,3 +54,7 @@ session_crate_name_empty = crate name must not be empty
session_invalid_character_in_create_name = invalid character `{$character}` in crate name: `{$crate_name}` session_invalid_character_in_create_name = invalid character `{$character}` in crate name: `{$crate_name}`
session_expr_parentheses_needed = parentheses are required to parse this as an expression session_expr_parentheses_needed = parentheses are required to parse this as an expression
session_skipping_const_checks = skipping const checks
session_unleashed_feature_help_named = skipping check for `{$gate}` feature
session_unleashed_feature_help_unnamed = skipping check that does not even have a feature gate

View file

@ -1,9 +1,7 @@
use std::num::NonZeroU32; use std::num::NonZeroU32;
use crate::cgu_reuse_tracker::CguReuse; use crate::cgu_reuse_tracker::CguReuse;
use rustc_errors::{ use rustc_errors::MultiSpan;
fluent, DiagnosticBuilder, ErrorGuaranteed, Handler, IntoDiagnostic, MultiSpan,
};
use rustc_macros::Diagnostic; use rustc_macros::Diagnostic;
use rustc_span::{Span, Symbol}; use rustc_span::{Span, Symbol};
use rustc_target::spec::{SplitDebuginfo, StackProtector, TargetTriple}; use rustc_target::spec::{SplitDebuginfo, StackProtector, TargetTriple};
@ -148,24 +146,15 @@ pub struct CrateNameEmpty {
pub span: Option<Span>, pub span: Option<Span>,
} }
#[derive(Diagnostic)]
#[diag(session::invalid_character_in_create_name)]
pub struct InvalidCharacterInCrateName<'a> { pub struct InvalidCharacterInCrateName<'a> {
#[primary_span]
pub span: Option<Span>, pub span: Option<Span>,
pub character: char, pub character: char,
pub crate_name: &'a str, pub crate_name: &'a str,
} }
impl IntoDiagnostic<'_> for InvalidCharacterInCrateName<'_> {
fn into_diagnostic(self, sess: &Handler) -> DiagnosticBuilder<'_, ErrorGuaranteed> {
let mut diag = sess.struct_err(fluent::session::invalid_character_in_create_name);
if let Some(sp) = self.span {
diag.set_span(sp);
}
diag.set_arg("character", self.character);
diag.set_arg("crate_name", self.crate_name);
diag
}
}
#[derive(Subdiagnostic)] #[derive(Subdiagnostic)]
#[multipart_suggestion(session::expr_parentheses_needed, applicability = "machine-applicable")] #[multipart_suggestion(session::expr_parentheses_needed, applicability = "machine-applicable")]
pub struct ExprParenthesesNeeded { pub struct ExprParenthesesNeeded {
@ -180,3 +169,25 @@ impl ExprParenthesesNeeded {
ExprParenthesesNeeded { left: s.shrink_to_lo(), right: s.shrink_to_hi() } ExprParenthesesNeeded { left: s.shrink_to_lo(), right: s.shrink_to_hi() }
} }
} }
#[derive(Diagnostic)]
#[diag(session::skipping_const_checks)]
pub struct SkippingConstChecks {
#[subdiagnostic(eager)]
pub unleashed_features: Vec<UnleashedFeatureHelp>,
}
#[derive(Subdiagnostic)]
pub enum UnleashedFeatureHelp {
#[help(session::unleashed_feature_help_named)]
Named {
#[primary_span]
span: Span,
gate: Symbol,
},
#[help(session::unleashed_feature_help_unnamed)]
Unnamed {
#[primary_span]
span: Span,
},
}

View file

@ -5,9 +5,10 @@ use crate::config::{self, CrateType, InstrumentCoverage, OptLevel, OutputType, S
use crate::errors::{ use crate::errors::{
CannotEnableCrtStaticLinux, CannotMixAndMatchSanitizers, LinkerPluginToWindowsNotSupported, CannotEnableCrtStaticLinux, CannotMixAndMatchSanitizers, LinkerPluginToWindowsNotSupported,
NotCircumventFeature, ProfileSampleUseFileDoesNotExist, ProfileUseFileDoesNotExist, NotCircumventFeature, ProfileSampleUseFileDoesNotExist, ProfileUseFileDoesNotExist,
SanitizerCfiEnabled, SanitizerNotSupported, SanitizersNotSupported, SanitizerCfiEnabled, SanitizerNotSupported, SanitizersNotSupported, SkippingConstChecks,
SplitDebugInfoUnstablePlatform, StackProtectorNotSupportedForTarget, SplitDebugInfoUnstablePlatform, StackProtectorNotSupportedForTarget,
TargetRequiresUnwindTables, UnstableVirtualFunctionElimination, UnsupportedDwarfVersion, TargetRequiresUnwindTables, UnleashedFeatureHelp, UnstableVirtualFunctionElimination,
UnsupportedDwarfVersion,
}; };
use crate::parse::{add_feature_diagnostics, ParseSess}; use crate::parse::{add_feature_diagnostics, ParseSess};
use crate::search_paths::{PathKind, SearchPath}; use crate::search_paths::{PathKind, SearchPath};
@ -233,21 +234,19 @@ impl Session {
if !unleashed_features.is_empty() { if !unleashed_features.is_empty() {
let mut must_err = false; let mut must_err = false;
// Create a diagnostic pointing at where things got unleashed. // Create a diagnostic pointing at where things got unleashed.
// FIXME(#100717): needs eager translation/lists self.emit_warning(SkippingConstChecks {
#[allow(rustc::untranslatable_diagnostic)] unleashed_features: unleashed_features
#[allow(rustc::diagnostic_outside_of_impl)] .iter()
let mut diag = self.struct_warn("skipping const checks"); .map(|(span, gate)| {
for &(span, feature_gate) in unleashed_features.iter() { gate.map(|gate| {
// FIXME: `span_label` doesn't do anything, so we use "help" as a hack. must_err = true;
if let Some(gate) = feature_gate { UnleashedFeatureHelp::Named { span: *span, gate }
diag.span_help(span, &format!("skipping check for `{gate}` feature")); })
// The unleash flag must *not* be used to just "hack around" feature gates. .unwrap_or(UnleashedFeatureHelp::Unnamed { span: *span })
must_err = true; })
} else { .collect(),
diag.span_help(span, "skipping check that does not even have a feature gate"); });
}
}
diag.emit();
// If we should err, make sure we did. // If we should err, make sure we did.
if must_err && self.has_errors().is_none() { if must_err && self.has_errors().is_none() {
// We have skipped a feature gate, and not run into other errors... reject. // We have skipped a feature gate, and not run into other errors... reject.