Skip to content

Commit 8ef38b5

Browse files
committed
Make more diagnostics in rustc_expand translatable
1 parent adecd66 commit 8ef38b5

File tree

3 files changed

+58
-21
lines changed

3 files changed

+58
-21
lines changed

compiler/rustc_expand/messages.ftl

+6
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}

compiler/rustc_expand/src/errors.rs

+37
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ use std::borrow::Cow;
22

33
use rustc_ast::ast;
44
use rustc_errors::codes::*;
5+
use rustc_errors::IntoDiagArg;
56
use rustc_macros::{Diagnostic, Subdiagnostic};
7+
use rustc_session::errors::FeatureGateSubdiagnostic;
68
use rustc_session::Limit;
79
use rustc_span::symbol::{Ident, MacroRulesNormalizedIdent};
810
use rustc_span::{Span, Symbol};
@@ -485,3 +487,38 @@ pub struct ProcMacroBackCompat {
485487
pub crate_name: String,
486488
pub fixed_version: String,
487489
}
490+
491+
pub enum StatementOrExpression {
492+
Statement,
493+
Expression,
494+
}
495+
496+
impl IntoDiagArg for StatementOrExpression {
497+
fn into_diag_arg(self) -> rustc_errors::DiagArgValue {
498+
let s = match self {
499+
StatementOrExpression::Statement => "statement",
500+
StatementOrExpression::Expression => "expression",
501+
};
502+
503+
rustc_errors::DiagArgValue::Str(s.into())
504+
}
505+
}
506+
507+
#[derive(Diagnostic)]
508+
#[diag(expand_custom_attribute_cannot_be_applied, code = E0658)]
509+
pub struct CustomAttributesForbidden {
510+
#[primary_span]
511+
pub span: Span,
512+
#[subdiagnostic]
513+
pub subdiag: FeatureGateSubdiagnostic,
514+
pub kind: StatementOrExpression,
515+
}
516+
517+
#[derive(Diagnostic)]
518+
#[diag(expand_non_inline_modules_in_proc_macro_input_are_unstable, code = E0658)]
519+
pub struct NonInlineModuleInProcMacroUnstable {
520+
#[primary_span]
521+
pub span: Span,
522+
#[subdiagnostic]
523+
pub subdiag: FeatureGateSubdiagnostic,
524+
}

compiler/rustc_expand/src/expand.rs

+15-21
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use rustc_parse::parser::{
2525
use rustc_parse::validate_attr;
2626
use rustc_session::lint::builtin::{UNUSED_ATTRIBUTES, UNUSED_DOC_COMMENTS};
2727
use rustc_session::lint::BuiltinLintDiag;
28-
use rustc_session::parse::feature_err;
28+
use rustc_session::parse::get_feature_diagnostics;
2929
use rustc_session::{Limit, Session};
3030
use rustc_span::hygiene::SyntaxContext;
3131
use rustc_span::symbol::{sym, Ident};
@@ -35,11 +35,11 @@ use smallvec::SmallVec;
3535
use crate::base::*;
3636
use crate::config::StripUnconfigured;
3737
use crate::errors::{
38-
EmptyDelegationMac, GlobDelegationOutsideImpls, GlobDelegationTraitlessQpath, IncompleteParse,
39-
RecursionLimitReached, RemoveExprNotSupported, RemoveNodeNotSupported, UnsupportedKeyValue,
40-
WrongFragmentKind,
38+
CustomAttributesForbidden, EmptyDelegationMac, GlobDelegationOutsideImpls,
39+
GlobDelegationTraitlessQpath, IncompleteParse, NonInlineModuleInProcMacroUnstable,
40+
RecursionLimitReached, RemoveExprNotSupported, RemoveNodeNotSupported, StatementOrExpression,
41+
UnsupportedKeyValue, WrongFragmentKind,
4142
};
42-
use crate::fluent_generated;
4343
use crate::mbe::diagnostics::annotate_err_with_kind;
4444
use crate::module::{mod_dir_path, parse_external_mod, DirOwnership, ParsedExternalMod};
4545
use crate::placeholders::{placeholder, PlaceholderExpander};
@@ -841,7 +841,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
841841
})
842842
}
843843

844-
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
845844
fn gate_proc_macro_attr_item(&self, span: Span, item: &Annotatable) {
846845
let kind = match item {
847846
Annotatable::Item(_)
@@ -854,9 +853,9 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
854853
if stmt.is_item() {
855854
return;
856855
}
857-
"statements"
856+
StatementOrExpression::Statement
858857
}
859-
Annotatable::Expr(_) => "expressions",
858+
Annotatable::Expr(_) => StatementOrExpression::Expression,
860859
Annotatable::Arm(..)
861860
| Annotatable::ExprField(..)
862861
| Annotatable::PatField(..)
@@ -868,13 +867,11 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
868867
if self.cx.ecfg.features.proc_macro_hygiene {
869868
return;
870869
}
871-
feature_err(
872-
&self.cx.sess,
873-
sym::proc_macro_hygiene,
870+
self.cx.dcx().emit_err(CustomAttributesForbidden {
874871
span,
875-
format!("custom attributes cannot be applied to {kind}"),
876-
)
877-
.emit();
872+
subdiag: get_feature_diagnostics(&self.cx.sess, sym::proc_macro_hygiene),
873+
kind,
874+
});
878875
}
879876

880877
fn gate_proc_macro_input(&self, annotatable: &Annotatable) {
@@ -888,13 +885,10 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
888885
ItemKind::Mod(_, mod_kind)
889886
if !matches!(mod_kind, ModKind::Loaded(_, Inline::Yes, _)) =>
890887
{
891-
feature_err(
892-
self.sess,
893-
sym::proc_macro_hygiene,
894-
item.span,
895-
fluent_generated::expand_non_inline_modules_in_proc_macro_input_are_unstable,
896-
)
897-
.emit();
888+
self.sess.dcx().emit_err(NonInlineModuleInProcMacroUnstable {
889+
span: item.span,
890+
subdiag: get_feature_diagnostics(self.sess, sym::proc_macro_hygiene),
891+
});
898892
}
899893
_ => {}
900894
}

0 commit comments

Comments
 (0)