Skip to content

Commit

Permalink
Make more diagnostics in rustc_expand translatable
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiretza committed Apr 12, 2024
1 parent d0ae2db commit 6d0b4f2
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 25 deletions.
18 changes: 18 additions & 0 deletions compiler/rustc_expand/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,27 @@ expand_attributes_wrong_form =
expand_cannot_be_name_of_macro =
`{$trait_ident}` cannot be a name of {$macro_type} macro
expand_cfg_attr_no_attributes =
`#[cfg_attr]` does not expand to any attributes
expand_collapse_debuginfo_illegal =
illegal value for attribute #[collapse_debuginfo(no|external|yes)]
expand_count_repetition_misplaced =
`count` can not be placed inside the inner-most repetition
expand_crate_name_in_cfg_attr_deprecated =
`crate_name` within an `#![cfg_attr]` attribute is deprecated
expand_crate_type_in_cfg_attr_deprecated =
`crate_type` within an `#![cfg_attr]` attribute is deprecated
expand_custom_attribute_cannot_be_applied =
custom attributes cannot be applied to {$kind ->
[statement] statements
*[expression] expressions
}
expand_custom_attribute_panicked =
custom attribute panicked
.help = message: {$message}
Expand Down Expand Up @@ -118,6 +133,9 @@ expand_module_multiple_candidates =
expand_must_repeat_once =
this must repeat at least once
expand_non_inline_module_in_proc_macro_unstable =
non-inline modules in proc macro input are unstable
expand_not_a_meta_item =
not a meta item
Expand Down
8 changes: 3 additions & 5 deletions compiler/rustc_expand/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ impl<'a> StripUnconfigured<'a> {
/// Gives a compiler warning when the `cfg_attr` contains no attributes and
/// is in the original source file. Gives a compiler error if the syntax of
/// the attribute is incorrect.
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
pub(crate) fn expand_cfg_attr(&self, attr: &Attribute, recursive: bool) -> Vec<Attribute> {
let Some((cfg_predicate, expanded_attrs)) =
rustc_parse::parse_cfg_attr(attr, &self.sess.psess)
Expand All @@ -253,7 +252,7 @@ impl<'a> StripUnconfigured<'a> {
rustc_lint_defs::builtin::UNUSED_ATTRIBUTES,
attr.span,
ast::CRATE_NODE_ID,
"`#[cfg_attr]` does not expand to any attributes",
crate::fluent_generated::expand_cfg_attr_no_attributes,
);
}

Expand All @@ -274,7 +273,6 @@ impl<'a> StripUnconfigured<'a> {
}
}

#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
fn expand_cfg_attr_item(
&self,
attr: &Attribute,
Expand Down Expand Up @@ -337,15 +335,15 @@ impl<'a> StripUnconfigured<'a> {
rustc_lint_defs::builtin::DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME,
attr.span,
ast::CRATE_NODE_ID,
"`crate_type` within an `#![cfg_attr]` attribute is deprecated",
crate::fluent_generated::expand_crate_type_in_cfg_attr_deprecated,
);
}
if attr.has_name(sym::crate_name) {
self.sess.psess.buffer_lint(
rustc_lint_defs::builtin::DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME,
attr.span,
ast::CRATE_NODE_ID,
"`crate_name` within an `#![cfg_attr]` attribute is deprecated",
crate::fluent_generated::expand_crate_name_in_cfg_attr_deprecated,
);
}
attr
Expand Down
38 changes: 37 additions & 1 deletion compiler/rustc_expand/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use rustc_ast::ast;
use rustc_errors::codes::*;
use rustc_errors::{codes::*, IntoDiagArg};
use rustc_macros::Diagnostic;
use rustc_session::errors::FeatureGateSubdiagnostic;
use rustc_session::Limit;
use rustc_span::symbol::{Ident, MacroRulesNormalizedIdent};
use rustc_span::{Span, Symbol};
Expand Down Expand Up @@ -456,3 +457,38 @@ pub struct ExpectedParenOrBrace<'a> {
pub span: Span,
pub token: Cow<'a, str>,
}

pub enum StatementOrExpression {
Statement,
Expression,
}

impl IntoDiagArg for StatementOrExpression {
fn into_diag_arg(self) -> rustc_errors::DiagArgValue {
let s = match self {
StatementOrExpression::Statement => "statement",
StatementOrExpression::Expression => "expression",
};

rustc_errors::DiagArgValue::Str(s.into())
}
}

#[derive(Diagnostic)]
#[diag(expand_custom_attribute_cannot_be_applied)]
pub struct CustomAttributesForbidden {
#[primary_span]
pub span: Span,
#[subdiagnostic]
pub subdiag: FeatureGateSubdiagnostic,
pub kind: StatementOrExpression,
}

#[derive(Diagnostic)]
#[diag(expand_non_inline_module_in_proc_macro_unstable)]
pub struct NonInlineModuleInProcMacroUnstable {
#[primary_span]
pub span: Span,
#[subdiagnostic]
pub subdiag: FeatureGateSubdiagnostic,
}
32 changes: 13 additions & 19 deletions compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::base::*;
use crate::config::StripUnconfigured;
use crate::errors::{
IncompleteParse, RecursionLimitReached, RemoveExprNotSupported, RemoveNodeNotSupported,
CustomAttributesForbidden, IncompleteParse, NonInlineModuleInProcMacroUnstable,
RecursionLimitReached, RemoveExprNotSupported, RemoveNodeNotSupported, StatementOrExpression,
UnsupportedKeyValue, WrongFragmentKind,
};
use crate::hygiene::SyntaxContext;
Expand Down Expand Up @@ -30,7 +31,7 @@ use rustc_parse::parser::{
use rustc_parse::validate_attr;
use rustc_session::lint::builtin::{UNUSED_ATTRIBUTES, UNUSED_DOC_COMMENTS};
use rustc_session::lint::BuiltinLintDiag;
use rustc_session::parse::feature_err;
use rustc_session::parse::get_feature_diagnostics;
use rustc_session::{Limit, Session};
use rustc_span::symbol::{sym, Ident};
use rustc_span::{ErrorGuaranteed, FileName, LocalExpnId, Span};
Expand Down Expand Up @@ -792,7 +793,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
})
}

#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
fn gate_proc_macro_attr_item(&self, span: Span, item: &Annotatable) {
let kind = match item {
Annotatable::Item(_)
Expand All @@ -806,9 +806,9 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
if stmt.is_item() {
return;
}
"statements"
StatementOrExpression::Statement
}
Annotatable::Expr(_) => "expressions",
Annotatable::Expr(_) => StatementOrExpression::Expression,
Annotatable::Arm(..)
| Annotatable::ExprField(..)
| Annotatable::PatField(..)
Expand All @@ -820,13 +820,11 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
if self.cx.ecfg.features.proc_macro_hygiene {
return;
}
feature_err(
&self.cx.sess,
sym::proc_macro_hygiene,
self.cx.dcx().emit_err(CustomAttributesForbidden {
span,
format!("custom attributes cannot be applied to {kind}"),
)
.emit();
subdiag: get_feature_diagnostics(&self.cx.sess, sym::proc_macro_hygiene),
kind,
});
}

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

impl<'ast, 'a> Visitor<'ast> for GateProcMacroInput<'a> {
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
fn visit_item(&mut self, item: &'ast ast::Item) {
match &item.kind {
ItemKind::Mod(_, mod_kind)
if !matches!(mod_kind, ModKind::Loaded(_, Inline::Yes, _)) =>
{
feature_err(
self.sess,
sym::proc_macro_hygiene,
item.span,
"non-inline modules in proc macro input are unstable",
)
.emit();
self.sess.dcx().emit_err(NonInlineModuleInProcMacroUnstable {
span: item.span,
subdiag: get_feature_diagnostics(self.sess, sym::proc_macro_hygiene),
});
}
_ => {}
}
Expand Down

0 comments on commit 6d0b4f2

Please sign in to comment.