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 Aug 23, 2024
1 parent 2227c6a commit 9b86f39
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 21 deletions.
6 changes: 6 additions & 0 deletions compiler/rustc_expand/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ expand_collapse_debuginfo_illegal =
expand_count_repetition_misplaced =
`count` can not be placed inside the inner-most repetition
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
37 changes: 37 additions & 0 deletions compiler/rustc_expand/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use std::borrow::Cow;

use rustc_ast::ast;
use rustc_errors::codes::*;
use rustc_errors::IntoDiagArg;
use rustc_macros::{Diagnostic, Subdiagnostic};
use rustc_session::errors::FeatureGateSubdiagnostic;
use rustc_session::Limit;
use rustc_span::symbol::{Ident, MacroRulesNormalizedIdent};
use rustc_span::{Span, Symbol};
Expand Down Expand Up @@ -485,3 +487,38 @@ pub struct ProcMacroBackCompat {
pub crate_name: String,
pub fixed_version: String,
}

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, code = E0658)]
pub struct CustomAttributesForbidden {
#[primary_span]
pub span: Span,
#[subdiagnostic]
pub subdiag: FeatureGateSubdiagnostic,
pub kind: StatementOrExpression,
}

#[derive(Diagnostic)]
#[diag(expand_non_inline_modules_in_proc_macro_input_are_unstable, code = E0658)]
pub struct NonInlineModuleInProcMacroUnstable {
#[primary_span]
pub span: Span,
#[subdiagnostic]
pub subdiag: FeatureGateSubdiagnostic,
}
36 changes: 15 additions & 21 deletions compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,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::hygiene::SyntaxContext;
use rustc_span::symbol::{sym, Ident};
Expand All @@ -35,11 +35,11 @@ use smallvec::SmallVec;
use crate::base::*;
use crate::config::StripUnconfigured;
use crate::errors::{
EmptyDelegationMac, GlobDelegationOutsideImpls, GlobDelegationTraitlessQpath, IncompleteParse,
RecursionLimitReached, RemoveExprNotSupported, RemoveNodeNotSupported, UnsupportedKeyValue,
WrongFragmentKind,
CustomAttributesForbidden, EmptyDelegationMac, GlobDelegationOutsideImpls,
GlobDelegationTraitlessQpath, IncompleteParse, NonInlineModuleInProcMacroUnstable,
RecursionLimitReached, RemoveExprNotSupported, RemoveNodeNotSupported, StatementOrExpression,
UnsupportedKeyValue, WrongFragmentKind,
};
use crate::fluent_generated;
use crate::mbe::diagnostics::annotate_err_with_kind;
use crate::module::{mod_dir_path, parse_external_mod, DirOwnership, ParsedExternalMod};
use crate::placeholders::{placeholder, PlaceholderExpander};
Expand Down Expand Up @@ -841,7 +841,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 @@ -854,9 +853,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 @@ -868,13 +867,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 @@ -888,13 +885,10 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
ItemKind::Mod(_, mod_kind)
if !matches!(mod_kind, ModKind::Loaded(_, Inline::Yes, _)) =>
{
feature_err(
self.sess,
sym::proc_macro_hygiene,
item.span,
fluent_generated::expand_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 9b86f39

Please sign in to comment.