1
Fork 0

Migrated more diagnostics under transcribe.rs

This commit is contained in:
nidnogg 2022-08-16 19:02:51 -03:00
parent 7e15fbab75
commit be18a9bf75
2 changed files with 23 additions and 9 deletions

View file

@ -6,3 +6,9 @@ expand_explain_doc_comment_inner =
expand_expr_repeat_no_syntax_vars = expand_expr_repeat_no_syntax_vars =
attempted to repeat an expression containing no syntax variables matched as repeating at this depth 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

View file

@ -7,9 +7,9 @@ use rustc_ast::tokenstream::{DelimSpan, Spacing, TokenStream, TokenTree};
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxHashMap;
use rustc_errors::{pluralize, PResult}; use rustc_errors::{pluralize, PResult};
use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed}; use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed};
use rustc_macros::SessionDiagnostic;
use rustc_span::hygiene::{LocalExpnId, Transparency}; use rustc_span::hygiene::{LocalExpnId, Transparency};
use rustc_span::symbol::{sym, Ident, MacroRulesNormalizedIdent}; use rustc_span::symbol::{sym, Ident, MacroRulesNormalizedIdent};
use rustc_macros::SessionDiagnostic;
use rustc_span::Span; use rustc_span::Span;
use smallvec::{smallvec, SmallVec}; use smallvec::{smallvec, SmallVec};
@ -61,6 +61,13 @@ struct NoSyntaxVarsExprRepeat {
span: Span, span: Span,
} }
#[derive(SessionDiagnostic)]
#[error(expand::must_repeat_once)]
struct MustRepeatOnce {
#[primary_span]
span: Span,
}
/// This can do Macro-By-Example transcription. /// This can do Macro-By-Example transcription.
/// - `interp` is a map of meta-variables to the tokens (non-terminals) they matched in the /// - `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. /// 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 // FIXME: this really ought to be caught at macro definition
// time... It happens when the Kleene operator in the matcher and // time... It happens when the Kleene operator in the matcher and
// the body for the same meta-variable do not match. // the body for the same meta-variable do not match.
return Err(cx.struct_span_err( return Err(cx.create_err(MustRepeatOnce { span: sp.entire() }));
sp.entire(),
"this must repeat at least once",
));
} }
} else { } else {
// 0 is the initial counter (we have done 0 repetitions so far). `len` // 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 /// Used solely by the `count` meta-variable expression, counts the outer-most repetitions at a
/// given optional nested depth. /// given optional nested depth.
/// ///
@ -452,10 +463,7 @@ fn count_repetitions<'a>(
match matched { match matched {
MatchedTokenTree(_) | MatchedNonterminal(_) => { MatchedTokenTree(_) | MatchedNonterminal(_) => {
if declared_lhs_depth == 0 { if declared_lhs_depth == 0 {
return Err(cx.struct_span_err( return Err(cx.create_err( CountRepetitionMisplaced { span: sp.entire()} ));
sp.entire(),
"`count` can not be placed inside the inner-most repetition",
));
} }
match depth_opt { match depth_opt {
None => Ok(1), None => Ok(1),