translations(rustc_session): migrate output.rs

This commit is contained in:
Luis Cardoso 2022-09-01 08:03:47 +02:00
parent b0cfeec293
commit 329d5014b6
7 changed files with 119 additions and 28 deletions

View file

@ -56,3 +56,13 @@ session_target_invalid_bits_size = {$err}
session_target_stack_protector_not_supported = `-Z stack-protector={$stack_protector}` is not supported for target {$target_triple} and will be ignored session_target_stack_protector_not_supported = `-Z stack-protector={$stack_protector}` is not supported for target {$target_triple} and will be ignored
session_split_debuginfo_unstable_platform = `-Csplit-debuginfo={$debuginfo}` is unstable on this platform session_split_debuginfo_unstable_platform = `-Csplit-debuginfo={$debuginfo}` is unstable on this platform
session_file_is_not_writeable = output file {$file} is not writeable -- check its permissions
session_crate_name_does_not_match = `--crate-name` and `#[crate_name]` are required to match, but `{$s}` != `{$name}`
session_crate_name_invalid = crate names cannot start with a `-`, but `{$s}` has a leading hyphen
session_crate_name_empty = crate name must not be empty
session_invalid_character_in_create_name = invalid character `{$character}` in crate name: `{$crate_name}`

View file

@ -13,9 +13,9 @@ rustc_serialize = { path = "../rustc_serialize" }
rustc_span = { path = "../rustc_span" } rustc_span = { path = "../rustc_span" }
rustc_macros = { path = "../rustc_macros" } rustc_macros = { path = "../rustc_macros" }
rustc_data_structures = { path = "../rustc_data_structures" } rustc_data_structures = { path = "../rustc_data_structures" }
rustc_target = { path = "../rustc_target" }
rustc_hir = { path = "../rustc_hir" } rustc_hir = { path = "../rustc_hir" }
rustc_lint_defs = { path = "../rustc_lint_defs" } rustc_lint_defs = { path = "../rustc_lint_defs" }
rustc_target = { path = "../rustc_target" }
unicode-width = "0.1.4" unicode-width = "0.1.4"
atty = "0.2" atty = "0.2"
termcolor = "1.0" termcolor = "1.0"

View file

@ -1,7 +1,6 @@
//! Contains infrastructure for configuring the compiler, including parsing //! Contains infrastructure for configuring the compiler, including parsing
//! command-line options. //! command-line options.
use crate::errors::TargetDataLayoutParseError;
pub use crate::options::*; pub use crate::options::*;
use crate::search_paths::SearchPath; use crate::search_paths::SearchPath;
@ -899,7 +898,7 @@ fn default_configuration(sess: &Session) -> CrateConfig {
let max_atomic_width = sess.target.max_atomic_width(); let max_atomic_width = sess.target.max_atomic_width();
let atomic_cas = sess.target.atomic_cas; let atomic_cas = sess.target.atomic_cas;
let layout = TargetDataLayout::parse(&sess.target).unwrap_or_else(|err| { let layout = TargetDataLayout::parse(&sess.target).unwrap_or_else(|err| {
sess.emit_fatal(TargetDataLayoutParseError { err }); sess.emit_fatal(err);
}); });
let mut ret = CrateConfig::default(); let mut ret = CrateConfig::default();

View file

@ -2,7 +2,7 @@ use std::num::NonZeroU32;
use crate::cgu_reuse_tracker::CguReuse; use crate::cgu_reuse_tracker::CguReuse;
use crate::{self as rustc_session, SessionDiagnostic}; use crate::{self as rustc_session, SessionDiagnostic};
use rustc_errors::{fluent, DiagnosticBuilder, Handler, MultiSpan}; use rustc_errors::{fluent, DiagnosticBuilder, ErrorGuaranteed, Handler, MultiSpan};
use rustc_macros::SessionDiagnostic; use rustc_macros::SessionDiagnostic;
use rustc_span::{Span, Symbol}; use rustc_span::{Span, Symbol};
use rustc_target::abi::TargetDataLayoutErrors; use rustc_target::abi::TargetDataLayoutErrors;
@ -170,3 +170,52 @@ pub struct StackProtectorNotSupportedForTarget<'a> {
pub struct SplitDebugInfoUnstablePlatform { pub struct SplitDebugInfoUnstablePlatform {
pub debuginfo: SplitDebuginfo, pub debuginfo: SplitDebuginfo,
} }
#[derive(SessionDiagnostic)]
#[diag(session::file_is_not_writeable)]
pub struct FileIsNotWriteable<'a> {
pub file: &'a std::path::Path,
}
#[derive(SessionDiagnostic)]
#[diag(session::crate_name_does_not_match)]
pub struct CrateNameDoesNotMatch<'a> {
#[primary_span]
pub span: Span,
pub s: &'a str,
pub name: Symbol,
}
#[derive(SessionDiagnostic)]
#[diag(session::crate_name_invalid)]
pub struct CrateNameInvalid<'a> {
pub s: &'a str,
}
#[derive(SessionDiagnostic)]
#[diag(session::crate_name_empty)]
pub struct CrateNameEmpty {
#[primary_span]
pub span: Option<Span>,
}
pub struct InvalidCharacterInCrateName<'a> {
pub span: Option<Span>,
pub character: char,
pub crate_name: &'a str,
}
impl crate::SessionDiagnostic<'_> for InvalidCharacterInCrateName<'_> {
fn into_diagnostic(
self,
sess: &Handler,
) -> rustc_errors::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
}
}

View file

@ -9,6 +9,8 @@
#![feature(map_many_mut)] #![feature(map_many_mut)]
#![recursion_limit = "256"] #![recursion_limit = "256"]
#![allow(rustc::potential_query_instability)] #![allow(rustc::potential_query_instability)]
#![deny(rustc::untranslatable_diagnostic)]
#![deny(rustc::diagnostic_outside_of_impl)]
#[macro_use] #[macro_use]
extern crate rustc_macros; extern crate rustc_macros;

View file

@ -1,5 +1,9 @@
//! Related to out filenames of compilation (e.g. save analysis, binaries). //! Related to out filenames of compilation (e.g. save analysis, binaries).
use crate::config::{CrateType, Input, OutputFilenames, OutputType}; use crate::config::{CrateType, Input, OutputFilenames, OutputType};
use crate::errors::{
CrateNameDoesNotMatch, CrateNameEmpty, CrateNameInvalid, FileIsNotWriteable,
InvalidCharacterInCrateName,
};
use crate::Session; use crate::Session;
use rustc_ast as ast; use rustc_ast as ast;
use rustc_span::symbol::sym; use rustc_span::symbol::sym;
@ -30,11 +34,7 @@ pub fn out_filename(
/// read-only file. We should be consistent. /// read-only file. We should be consistent.
pub fn check_file_is_writeable(file: &Path, sess: &Session) { pub fn check_file_is_writeable(file: &Path, sess: &Session) {
if !is_writeable(file) { if !is_writeable(file) {
sess.fatal(&format!( sess.emit_fatal(FileIsNotWriteable { file });
"output file {} is not writeable -- check its \
permissions",
file.display()
));
} }
} }
@ -61,11 +61,7 @@ pub fn find_crate_name(sess: &Session, attrs: &[ast::Attribute], input: &Input)
if let Some(ref s) = sess.opts.crate_name { if let Some(ref s) = sess.opts.crate_name {
if let Some((attr, name)) = attr_crate_name { if let Some((attr, name)) = attr_crate_name {
if name.as_str() != s { if name.as_str() != s {
let msg = format!( sess.emit_err(CrateNameDoesNotMatch { span: attr.span, s, name });
"`--crate-name` and `#[crate_name]` are \
required to match, but `{s}` != `{name}`"
);
sess.span_err(attr.span, &msg);
} }
} }
return validate(s.clone(), None); return validate(s.clone(), None);
@ -77,11 +73,7 @@ pub fn find_crate_name(sess: &Session, attrs: &[ast::Attribute], input: &Input)
if let Input::File(ref path) = *input { if let Input::File(ref path) = *input {
if let Some(s) = path.file_stem().and_then(|s| s.to_str()) { if let Some(s) = path.file_stem().and_then(|s| s.to_str()) {
if s.starts_with('-') { if s.starts_with('-') {
let msg = format!( sess.emit_err(CrateNameInvalid { s });
"crate names cannot start with a `-`, but \
`{s}` has a leading hyphen"
);
sess.err(&msg);
} else { } else {
return validate(s.replace('-', "_"), None); return validate(s.replace('-', "_"), None);
} }
@ -94,15 +86,9 @@ pub fn find_crate_name(sess: &Session, attrs: &[ast::Attribute], input: &Input)
pub fn validate_crate_name(sess: &Session, s: &str, sp: Option<Span>) { pub fn validate_crate_name(sess: &Session, s: &str, sp: Option<Span>) {
let mut err_count = 0; let mut err_count = 0;
{ {
let mut say = |s: &str| {
match sp {
Some(sp) => sess.span_err(sp, s),
None => sess.err(s),
};
err_count += 1;
};
if s.is_empty() { if s.is_empty() {
say("crate name must not be empty"); err_count += 1;
sess.emit_err(CrateNameEmpty { span: sp });
} }
for c in s.chars() { for c in s.chars() {
if c.is_alphanumeric() { if c.is_alphanumeric() {
@ -111,7 +97,8 @@ pub fn validate_crate_name(sess: &Session, s: &str, sp: Option<Span>) {
if c == '_' { if c == '_' {
continue; continue;
} }
say(&format!("invalid character `{c}` in crate name: `{s}`")); err_count += 1;
sess.emit_err(InvalidCharacterInCrateName { span: sp, character: c, crate_name: s });
} }
} }

View file

@ -297,6 +297,8 @@ impl Session {
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[allow(rustc::untranslatable_diagnostic)]
#[allow(rustc::diagnostic_outside_of_impl)]
pub fn struct_span_warn<S: Into<MultiSpan>>( pub fn struct_span_warn<S: Into<MultiSpan>>(
&self, &self,
sp: S, sp: S,
@ -305,6 +307,8 @@ impl Session {
self.diagnostic().struct_span_warn(sp, msg) self.diagnostic().struct_span_warn(sp, msg)
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[allow(rustc::untranslatable_diagnostic)]
#[allow(rustc::diagnostic_outside_of_impl)]
pub fn struct_span_warn_with_expectation<S: Into<MultiSpan>>( pub fn struct_span_warn_with_expectation<S: Into<MultiSpan>>(
&self, &self,
sp: S, sp: S,
@ -314,6 +318,8 @@ impl Session {
self.diagnostic().struct_span_warn_with_expectation(sp, msg, id) self.diagnostic().struct_span_warn_with_expectation(sp, msg, id)
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[allow(rustc::untranslatable_diagnostic)]
#[allow(rustc::diagnostic_outside_of_impl)]
pub fn struct_span_warn_with_code<S: Into<MultiSpan>>( pub fn struct_span_warn_with_code<S: Into<MultiSpan>>(
&self, &self,
sp: S, sp: S,
@ -323,10 +329,14 @@ impl Session {
self.diagnostic().struct_span_warn_with_code(sp, msg, code) self.diagnostic().struct_span_warn_with_code(sp, msg, code)
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[allow(rustc::untranslatable_diagnostic)]
#[allow(rustc::diagnostic_outside_of_impl)]
pub fn struct_warn(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, ()> { pub fn struct_warn(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, ()> {
self.diagnostic().struct_warn(msg) self.diagnostic().struct_warn(msg)
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[allow(rustc::untranslatable_diagnostic)]
#[allow(rustc::diagnostic_outside_of_impl)]
pub fn struct_warn_with_expectation( pub fn struct_warn_with_expectation(
&self, &self,
msg: impl Into<DiagnosticMessage>, msg: impl Into<DiagnosticMessage>,
@ -335,6 +345,8 @@ impl Session {
self.diagnostic().struct_warn_with_expectation(msg, id) self.diagnostic().struct_warn_with_expectation(msg, id)
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[allow(rustc::untranslatable_diagnostic)]
#[allow(rustc::diagnostic_outside_of_impl)]
pub fn struct_span_allow<S: Into<MultiSpan>>( pub fn struct_span_allow<S: Into<MultiSpan>>(
&self, &self,
sp: S, sp: S,
@ -343,10 +355,14 @@ impl Session {
self.diagnostic().struct_span_allow(sp, msg) self.diagnostic().struct_span_allow(sp, msg)
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[allow(rustc::untranslatable_diagnostic)]
#[allow(rustc::diagnostic_outside_of_impl)]
pub fn struct_allow(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, ()> { pub fn struct_allow(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, ()> {
self.diagnostic().struct_allow(msg) self.diagnostic().struct_allow(msg)
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[allow(rustc::untranslatable_diagnostic)]
#[allow(rustc::diagnostic_outside_of_impl)]
pub fn struct_expect( pub fn struct_expect(
&self, &self,
msg: impl Into<DiagnosticMessage>, msg: impl Into<DiagnosticMessage>,
@ -355,6 +371,8 @@ impl Session {
self.diagnostic().struct_expect(msg, id) self.diagnostic().struct_expect(msg, id)
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[allow(rustc::untranslatable_diagnostic)]
#[allow(rustc::diagnostic_outside_of_impl)]
pub fn struct_span_err<S: Into<MultiSpan>>( pub fn struct_span_err<S: Into<MultiSpan>>(
&self, &self,
sp: S, sp: S,
@ -363,6 +381,8 @@ impl Session {
self.diagnostic().struct_span_err(sp, msg) self.diagnostic().struct_span_err(sp, msg)
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[allow(rustc::untranslatable_diagnostic)]
#[allow(rustc::diagnostic_outside_of_impl)]
pub fn struct_span_err_with_code<S: Into<MultiSpan>>( pub fn struct_span_err_with_code<S: Into<MultiSpan>>(
&self, &self,
sp: S, sp: S,
@ -373,6 +393,8 @@ impl Session {
} }
// FIXME: This method should be removed (every error should have an associated error code). // FIXME: This method should be removed (every error should have an associated error code).
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[allow(rustc::untranslatable_diagnostic)]
#[allow(rustc::diagnostic_outside_of_impl)]
pub fn struct_err( pub fn struct_err(
&self, &self,
msg: impl Into<DiagnosticMessage>, msg: impl Into<DiagnosticMessage>,
@ -380,6 +402,8 @@ impl Session {
self.parse_sess.struct_err(msg) self.parse_sess.struct_err(msg)
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[allow(rustc::untranslatable_diagnostic)]
#[allow(rustc::diagnostic_outside_of_impl)]
pub fn struct_err_with_code( pub fn struct_err_with_code(
&self, &self,
msg: impl Into<DiagnosticMessage>, msg: impl Into<DiagnosticMessage>,
@ -388,6 +412,8 @@ impl Session {
self.diagnostic().struct_err_with_code(msg, code) self.diagnostic().struct_err_with_code(msg, code)
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[allow(rustc::untranslatable_diagnostic)]
#[allow(rustc::diagnostic_outside_of_impl)]
pub fn struct_warn_with_code( pub fn struct_warn_with_code(
&self, &self,
msg: impl Into<DiagnosticMessage>, msg: impl Into<DiagnosticMessage>,
@ -396,6 +422,8 @@ impl Session {
self.diagnostic().struct_warn_with_code(msg, code) self.diagnostic().struct_warn_with_code(msg, code)
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[allow(rustc::untranslatable_diagnostic)]
#[allow(rustc::diagnostic_outside_of_impl)]
pub fn struct_span_fatal<S: Into<MultiSpan>>( pub fn struct_span_fatal<S: Into<MultiSpan>>(
&self, &self,
sp: S, sp: S,
@ -404,6 +432,8 @@ impl Session {
self.diagnostic().struct_span_fatal(sp, msg) self.diagnostic().struct_span_fatal(sp, msg)
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[allow(rustc::untranslatable_diagnostic)]
#[allow(rustc::diagnostic_outside_of_impl)]
pub fn struct_span_fatal_with_code<S: Into<MultiSpan>>( pub fn struct_span_fatal_with_code<S: Into<MultiSpan>>(
&self, &self,
sp: S, sp: S,
@ -413,15 +443,21 @@ impl Session {
self.diagnostic().struct_span_fatal_with_code(sp, msg, code) self.diagnostic().struct_span_fatal_with_code(sp, msg, code)
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[allow(rustc::untranslatable_diagnostic)]
#[allow(rustc::diagnostic_outside_of_impl)]
pub fn struct_fatal(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, !> { pub fn struct_fatal(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, !> {
self.diagnostic().struct_fatal(msg) self.diagnostic().struct_fatal(msg)
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[allow(rustc::untranslatable_diagnostic)]
#[allow(rustc::diagnostic_outside_of_impl)]
pub fn span_fatal<S: Into<MultiSpan>>(&self, sp: S, msg: impl Into<DiagnosticMessage>) -> ! { pub fn span_fatal<S: Into<MultiSpan>>(&self, sp: S, msg: impl Into<DiagnosticMessage>) -> ! {
self.diagnostic().span_fatal(sp, msg) self.diagnostic().span_fatal(sp, msg)
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[allow(rustc::untranslatable_diagnostic)]
#[allow(rustc::diagnostic_outside_of_impl)]
pub fn span_fatal_with_code<S: Into<MultiSpan>>( pub fn span_fatal_with_code<S: Into<MultiSpan>>(
&self, &self,
sp: S, sp: S,
@ -431,10 +467,14 @@ impl Session {
self.diagnostic().span_fatal_with_code(sp, msg, code) self.diagnostic().span_fatal_with_code(sp, msg, code)
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[allow(rustc::untranslatable_diagnostic)]
#[allow(rustc::diagnostic_outside_of_impl)]
pub fn fatal(&self, msg: impl Into<DiagnosticMessage>) -> ! { pub fn fatal(&self, msg: impl Into<DiagnosticMessage>) -> ! {
self.diagnostic().fatal(msg).raise() self.diagnostic().fatal(msg).raise()
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[allow(rustc::untranslatable_diagnostic)]
#[allow(rustc::diagnostic_outside_of_impl)]
pub fn span_err_or_warn<S: Into<MultiSpan>>( pub fn span_err_or_warn<S: Into<MultiSpan>>(
&self, &self,
is_warning: bool, is_warning: bool,
@ -448,6 +488,8 @@ impl Session {
} }
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[allow(rustc::untranslatable_diagnostic)]
#[allow(rustc::diagnostic_outside_of_impl)]
pub fn span_err<S: Into<MultiSpan>>( pub fn span_err<S: Into<MultiSpan>>(
&self, &self,
sp: S, sp: S,
@ -456,6 +498,8 @@ impl Session {
self.diagnostic().span_err(sp, msg) self.diagnostic().span_err(sp, msg)
} }
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[allow(rustc::untranslatable_diagnostic)]
#[allow(rustc::diagnostic_outside_of_impl)]
pub fn span_err_with_code<S: Into<MultiSpan>>( pub fn span_err_with_code<S: Into<MultiSpan>>(
&self, &self,
sp: S, sp: S,