From be18a9bf75f6c92ee734838fd3eca9257556cf40 Mon Sep 17 00:00:00 2001 From: nidnogg Date: Tue, 16 Aug 2022 19:02:51 -0300 Subject: [PATCH] Migrated more diagnostics under transcribe.rs --- .../locales/en-US/expand.ftl | 6 +++++ compiler/rustc_expand/src/mbe/transcribe.rs | 26 ++++++++++++------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/expand.ftl b/compiler/rustc_error_messages/locales/en-US/expand.ftl index 42519fd22ce6..b25aaaa0e517 100644 --- a/compiler/rustc_error_messages/locales/en-US/expand.ftl +++ b/compiler/rustc_error_messages/locales/en-US/expand.ftl @@ -6,3 +6,9 @@ expand_explain_doc_comment_inner = expand_expr_repeat_no_syntax_vars = attempted to repeat an expression containing no syntax variables matched as repeating at this depth + +expand_must_repeat_once = + this must repeat at least once + +count_repetition_misplaced = + `count` can not be placed inside the inner-most repetition \ No newline at end of file diff --git a/compiler/rustc_expand/src/mbe/transcribe.rs b/compiler/rustc_expand/src/mbe/transcribe.rs index 69090cb457e7..4c8f7a59bbac 100644 --- a/compiler/rustc_expand/src/mbe/transcribe.rs +++ b/compiler/rustc_expand/src/mbe/transcribe.rs @@ -7,9 +7,9 @@ use rustc_ast::tokenstream::{DelimSpan, Spacing, TokenStream, TokenTree}; use rustc_data_structures::fx::FxHashMap; use rustc_errors::{pluralize, PResult}; use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed}; +use rustc_macros::SessionDiagnostic; use rustc_span::hygiene::{LocalExpnId, Transparency}; use rustc_span::symbol::{sym, Ident, MacroRulesNormalizedIdent}; -use rustc_macros::SessionDiagnostic; use rustc_span::Span; use smallvec::{smallvec, SmallVec}; @@ -61,6 +61,13 @@ struct NoSyntaxVarsExprRepeat { span: Span, } +#[derive(SessionDiagnostic)] +#[error(expand::must_repeat_once)] +struct MustRepeatOnce { + #[primary_span] + span: Span, +} + /// This can do Macro-By-Example transcription. /// - `interp` is a map of meta-variables to the tokens (non-terminals) they matched in the /// invocation. We are assuming we already know there is a match. @@ -197,10 +204,7 @@ pub(super) fn transcribe<'a>( // FIXME: this really ought to be caught at macro definition // time... It happens when the Kleene operator in the matcher and // the body for the same meta-variable do not match. - return Err(cx.struct_span_err( - sp.entire(), - "this must repeat at least once", - )); + return Err(cx.create_err(MustRepeatOnce { span: sp.entire() })); } } else { // 0 is the initial counter (we have done 0 repetitions so far). `len` @@ -424,6 +428,13 @@ fn lockstep_iter_size( } } +#[derive(SessionDiagnostic)] +#[error(expand::count_repetition_misplaced)] +struct CountRepetitionMisplaced { + #[primary_span] + span: Span, +} + /// Used solely by the `count` meta-variable expression, counts the outer-most repetitions at a /// given optional nested depth. /// @@ -452,10 +463,7 @@ fn count_repetitions<'a>( match matched { MatchedTokenTree(_) | MatchedNonterminal(_) => { if declared_lhs_depth == 0 { - return Err(cx.struct_span_err( - sp.entire(), - "`count` can not be placed inside the inner-most repetition", - )); + return Err(cx.create_err( CountRepetitionMisplaced { span: sp.entire()} )); } match depth_opt { None => Ok(1),