Skip to content

Commit 6d0b4f2

Browse files
committed
Make more diagnostics in rustc_expand translatable
1 parent d0ae2db commit 6d0b4f2

File tree

4 files changed

+71
-25
lines changed

4 files changed

+71
-25
lines changed

compiler/rustc_expand/messages.ftl

+18
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,27 @@ expand_attributes_wrong_form =
2121
expand_cannot_be_name_of_macro =
2222
`{$trait_ident}` cannot be a name of {$macro_type} macro
2323
24+
expand_cfg_attr_no_attributes =
25+
`#[cfg_attr]` does not expand to any attributes
26+
2427
expand_collapse_debuginfo_illegal =
2528
illegal value for attribute #[collapse_debuginfo(no|external|yes)]
2629
2730
expand_count_repetition_misplaced =
2831
`count` can not be placed inside the inner-most repetition
2932
33+
expand_crate_name_in_cfg_attr_deprecated =
34+
`crate_name` within an `#![cfg_attr]` attribute is deprecated
35+
36+
expand_crate_type_in_cfg_attr_deprecated =
37+
`crate_type` within an `#![cfg_attr]` attribute is deprecated
38+
39+
expand_custom_attribute_cannot_be_applied =
40+
custom attributes cannot be applied to {$kind ->
41+
[statement] statements
42+
*[expression] expressions
43+
}
44+
3045
expand_custom_attribute_panicked =
3146
custom attribute panicked
3247
.help = message: {$message}
@@ -118,6 +133,9 @@ expand_module_multiple_candidates =
118133
expand_must_repeat_once =
119134
this must repeat at least once
120135
136+
expand_non_inline_module_in_proc_macro_unstable =
137+
non-inline modules in proc macro input are unstable
138+
121139
expand_not_a_meta_item =
122140
not a meta item
123141

compiler/rustc_expand/src/config.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ impl<'a> StripUnconfigured<'a> {
239239
/// Gives a compiler warning when the `cfg_attr` contains no attributes and
240240
/// is in the original source file. Gives a compiler error if the syntax of
241241
/// the attribute is incorrect.
242-
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
243242
pub(crate) fn expand_cfg_attr(&self, attr: &Attribute, recursive: bool) -> Vec<Attribute> {
244243
let Some((cfg_predicate, expanded_attrs)) =
245244
rustc_parse::parse_cfg_attr(attr, &self.sess.psess)
@@ -253,7 +252,7 @@ impl<'a> StripUnconfigured<'a> {
253252
rustc_lint_defs::builtin::UNUSED_ATTRIBUTES,
254253
attr.span,
255254
ast::CRATE_NODE_ID,
256-
"`#[cfg_attr]` does not expand to any attributes",
255+
crate::fluent_generated::expand_cfg_attr_no_attributes,
257256
);
258257
}
259258

@@ -274,7 +273,6 @@ impl<'a> StripUnconfigured<'a> {
274273
}
275274
}
276275

277-
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
278276
fn expand_cfg_attr_item(
279277
&self,
280278
attr: &Attribute,
@@ -337,15 +335,15 @@ impl<'a> StripUnconfigured<'a> {
337335
rustc_lint_defs::builtin::DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME,
338336
attr.span,
339337
ast::CRATE_NODE_ID,
340-
"`crate_type` within an `#![cfg_attr]` attribute is deprecated",
338+
crate::fluent_generated::expand_crate_type_in_cfg_attr_deprecated,
341339
);
342340
}
343341
if attr.has_name(sym::crate_name) {
344342
self.sess.psess.buffer_lint(
345343
rustc_lint_defs::builtin::DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME,
346344
attr.span,
347345
ast::CRATE_NODE_ID,
348-
"`crate_name` within an `#![cfg_attr]` attribute is deprecated",
346+
crate::fluent_generated::expand_crate_name_in_cfg_attr_deprecated,
349347
);
350348
}
351349
attr

compiler/rustc_expand/src/errors.rs

+37-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use rustc_ast::ast;
2-
use rustc_errors::codes::*;
2+
use rustc_errors::{codes::*, IntoDiagArg};
33
use rustc_macros::Diagnostic;
4+
use rustc_session::errors::FeatureGateSubdiagnostic;
45
use rustc_session::Limit;
56
use rustc_span::symbol::{Ident, MacroRulesNormalizedIdent};
67
use rustc_span::{Span, Symbol};
@@ -456,3 +457,38 @@ pub struct ExpectedParenOrBrace<'a> {
456457
pub span: Span,
457458
pub token: Cow<'a, str>,
458459
}
460+
461+
pub enum StatementOrExpression {
462+
Statement,
463+
Expression,
464+
}
465+
466+
impl IntoDiagArg for StatementOrExpression {
467+
fn into_diag_arg(self) -> rustc_errors::DiagArgValue {
468+
let s = match self {
469+
StatementOrExpression::Statement => "statement",
470+
StatementOrExpression::Expression => "expression",
471+
};
472+
473+
rustc_errors::DiagArgValue::Str(s.into())
474+
}
475+
}
476+
477+
#[derive(Diagnostic)]
478+
#[diag(expand_custom_attribute_cannot_be_applied)]
479+
pub struct CustomAttributesForbidden {
480+
#[primary_span]
481+
pub span: Span,
482+
#[subdiagnostic]
483+
pub subdiag: FeatureGateSubdiagnostic,
484+
pub kind: StatementOrExpression,
485+
}
486+
487+
#[derive(Diagnostic)]
488+
#[diag(expand_non_inline_module_in_proc_macro_unstable)]
489+
pub struct NonInlineModuleInProcMacroUnstable {
490+
#[primary_span]
491+
pub span: Span,
492+
#[subdiagnostic]
493+
pub subdiag: FeatureGateSubdiagnostic,
494+
}

compiler/rustc_expand/src/expand.rs

+13-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use crate::base::*;
22
use crate::config::StripUnconfigured;
33
use crate::errors::{
4-
IncompleteParse, RecursionLimitReached, RemoveExprNotSupported, RemoveNodeNotSupported,
4+
CustomAttributesForbidden, IncompleteParse, NonInlineModuleInProcMacroUnstable,
5+
RecursionLimitReached, RemoveExprNotSupported, RemoveNodeNotSupported, StatementOrExpression,
56
UnsupportedKeyValue, WrongFragmentKind,
67
};
78
use crate::hygiene::SyntaxContext;
@@ -30,7 +31,7 @@ use rustc_parse::parser::{
3031
use rustc_parse::validate_attr;
3132
use rustc_session::lint::builtin::{UNUSED_ATTRIBUTES, UNUSED_DOC_COMMENTS};
3233
use rustc_session::lint::BuiltinLintDiag;
33-
use rustc_session::parse::feature_err;
34+
use rustc_session::parse::get_feature_diagnostics;
3435
use rustc_session::{Limit, Session};
3536
use rustc_span::symbol::{sym, Ident};
3637
use rustc_span::{ErrorGuaranteed, FileName, LocalExpnId, Span};
@@ -792,7 +793,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
792793
})
793794
}
794795

795-
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
796796
fn gate_proc_macro_attr_item(&self, span: Span, item: &Annotatable) {
797797
let kind = match item {
798798
Annotatable::Item(_)
@@ -806,9 +806,9 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
806806
if stmt.is_item() {
807807
return;
808808
}
809-
"statements"
809+
StatementOrExpression::Statement
810810
}
811-
Annotatable::Expr(_) => "expressions",
811+
Annotatable::Expr(_) => StatementOrExpression::Expression,
812812
Annotatable::Arm(..)
813813
| Annotatable::ExprField(..)
814814
| Annotatable::PatField(..)
@@ -820,13 +820,11 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
820820
if self.cx.ecfg.features.proc_macro_hygiene {
821821
return;
822822
}
823-
feature_err(
824-
&self.cx.sess,
825-
sym::proc_macro_hygiene,
823+
self.cx.dcx().emit_err(CustomAttributesForbidden {
826824
span,
827-
format!("custom attributes cannot be applied to {kind}"),
828-
)
829-
.emit();
825+
subdiag: get_feature_diagnostics(&self.cx.sess, sym::proc_macro_hygiene),
826+
kind,
827+
});
830828
}
831829

832830
fn gate_proc_macro_input(&self, annotatable: &Annotatable) {
@@ -835,19 +833,15 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
835833
}
836834

837835
impl<'ast, 'a> Visitor<'ast> for GateProcMacroInput<'a> {
838-
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
839836
fn visit_item(&mut self, item: &'ast ast::Item) {
840837
match &item.kind {
841838
ItemKind::Mod(_, mod_kind)
842839
if !matches!(mod_kind, ModKind::Loaded(_, Inline::Yes, _)) =>
843840
{
844-
feature_err(
845-
self.sess,
846-
sym::proc_macro_hygiene,
847-
item.span,
848-
"non-inline modules in proc macro input are unstable",
849-
)
850-
.emit();
841+
self.sess.dcx().emit_err(NonInlineModuleInProcMacroUnstable {
842+
span: item.span,
843+
subdiag: get_feature_diagnostics(self.sess, sym::proc_macro_hygiene),
844+
});
851845
}
852846
_ => {}
853847
}

0 commit comments

Comments
 (0)