Skip to content

Commit 871f723

Browse files
committed
Make more diagnostics in rustc_expand translatable
1 parent eee478c commit 871f723

File tree

3 files changed

+60
-21
lines changed

3 files changed

+60
-21
lines changed

compiler/rustc_expand/messages.ftl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ expand_collapse_debuginfo_illegal =
2727
expand_count_repetition_misplaced =
2828
`count` can not be placed inside the inner-most repetition
2929
30+
expand_custom_attribute_cannot_be_applied =
31+
custom attributes cannot be applied to {$kind ->
32+
[statement] statements
33+
*[expression] expressions
34+
}
35+
3036
expand_custom_attribute_panicked =
3137
custom attribute panicked
3238
.help = message: {$message}
@@ -118,6 +124,9 @@ expand_module_multiple_candidates =
118124
expand_must_repeat_once =
119125
this must repeat at least once
120126
127+
expand_non_inline_module_in_proc_macro_unstable =
128+
non-inline modules in proc macro input are unstable
129+
121130
expand_not_a_meta_item =
122131
not a meta item
123132

compiler/rustc_expand/src/errors.rs

Lines changed: 37 additions & 1 deletion
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, Subdiagnostic};
4+
use rustc_session::errors::FeatureGateSubdiagnostic;
45
use rustc_session::Limit;
56
use rustc_span::symbol::{Ident, MacroRulesNormalizedIdent};
67
use rustc_span::{Span, Symbol};
@@ -440,3 +441,38 @@ pub(crate) struct EmptyDelegationList {
440441
#[primary_span]
441442
pub span: Span,
442443
}
444+
445+
pub enum StatementOrExpression {
446+
Statement,
447+
Expression,
448+
}
449+
450+
impl IntoDiagArg for StatementOrExpression {
451+
fn into_diag_arg(self) -> rustc_errors::DiagArgValue {
452+
let s = match self {
453+
StatementOrExpression::Statement => "statement",
454+
StatementOrExpression::Expression => "expression",
455+
};
456+
457+
rustc_errors::DiagArgValue::Str(s.into())
458+
}
459+
}
460+
461+
#[derive(Diagnostic)]
462+
#[diag(expand_custom_attribute_cannot_be_applied, code = E0658)]
463+
pub struct CustomAttributesForbidden {
464+
#[primary_span]
465+
pub span: Span,
466+
#[subdiagnostic]
467+
pub subdiag: FeatureGateSubdiagnostic,
468+
pub kind: StatementOrExpression,
469+
}
470+
471+
#[derive(Diagnostic)]
472+
#[diag(expand_non_inline_module_in_proc_macro_unstable, code = E0658)]
473+
pub struct NonInlineModuleInProcMacroUnstable {
474+
#[primary_span]
475+
pub span: Span,
476+
#[subdiagnostic]
477+
pub subdiag: FeatureGateSubdiagnostic,
478+
}

compiler/rustc_expand/src/expand.rs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use crate::base::*;
22
use crate::config::StripUnconfigured;
33
use crate::errors::{
4-
EmptyDelegationList, IncompleteParse, RecursionLimitReached, RemoveExprNotSupported,
5-
RemoveNodeNotSupported, UnsupportedKeyValue, WrongFragmentKind,
4+
CustomAttributesForbidden, EmptyDelegationList, IncompleteParse,
5+
NonInlineModuleInProcMacroUnstable, RecursionLimitReached, RemoveExprNotSupported,
6+
RemoveNodeNotSupported, StatementOrExpression, UnsupportedKeyValue, WrongFragmentKind,
67
};
78
use crate::mbe::diagnostics::annotate_err_with_kind;
89
use crate::module::{mod_dir_path, parse_external_mod, DirOwnership, ParsedExternalMod};
@@ -29,7 +30,7 @@ use rustc_parse::parser::{
2930
use rustc_parse::validate_attr;
3031
use rustc_session::lint::builtin::{UNUSED_ATTRIBUTES, UNUSED_DOC_COMMENTS};
3132
use rustc_session::lint::BuiltinLintDiag;
32-
use rustc_session::parse::feature_err;
33+
use rustc_session::parse::get_feature_diagnostics;
3334
use rustc_session::{Limit, Session};
3435
use rustc_span::hygiene::SyntaxContext;
3536
use rustc_span::symbol::{sym, Ident};
@@ -796,7 +797,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
796797
})
797798
}
798799

799-
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
800800
fn gate_proc_macro_attr_item(&self, span: Span, item: &Annotatable) {
801801
let kind = match item {
802802
Annotatable::Item(_)
@@ -810,9 +810,9 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
810810
if stmt.is_item() {
811811
return;
812812
}
813-
"statements"
813+
StatementOrExpression::Statement
814814
}
815-
Annotatable::Expr(_) => "expressions",
815+
Annotatable::Expr(_) => StatementOrExpression::Expression,
816816
Annotatable::Arm(..)
817817
| Annotatable::ExprField(..)
818818
| Annotatable::PatField(..)
@@ -824,13 +824,11 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
824824
if self.cx.ecfg.features.proc_macro_hygiene {
825825
return;
826826
}
827-
feature_err(
828-
&self.cx.sess,
829-
sym::proc_macro_hygiene,
827+
self.cx.dcx().emit_err(CustomAttributesForbidden {
830828
span,
831-
format!("custom attributes cannot be applied to {kind}"),
832-
)
833-
.emit();
829+
subdiag: get_feature_diagnostics(&self.cx.sess, sym::proc_macro_hygiene),
830+
kind,
831+
});
834832
}
835833

836834
fn gate_proc_macro_input(&self, annotatable: &Annotatable) {
@@ -839,19 +837,15 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
839837
}
840838

841839
impl<'ast, 'a> Visitor<'ast> for GateProcMacroInput<'a> {
842-
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
843840
fn visit_item(&mut self, item: &'ast ast::Item) {
844841
match &item.kind {
845842
ItemKind::Mod(_, mod_kind)
846843
if !matches!(mod_kind, ModKind::Loaded(_, Inline::Yes, _)) =>
847844
{
848-
feature_err(
849-
self.sess,
850-
sym::proc_macro_hygiene,
851-
item.span,
852-
"non-inline modules in proc macro input are unstable",
853-
)
854-
.emit();
845+
self.sess.dcx().emit_err(NonInlineModuleInProcMacroUnstable {
846+
span: item.span,
847+
subdiag: get_feature_diagnostics(self.sess, sym::proc_macro_hygiene),
848+
});
855849
}
856850
_ => {}
857851
}

0 commit comments

Comments
 (0)