Skip to content

Commit

Permalink
Rollup merge of rust-lang#100651 - nidnogg:diagnostics_migration_expa…
Browse files Browse the repository at this point in the history
…nd_transcribe, r=davidtwco

Migrations for rustc_expand transcribe.rs

This PR includes some migrations to the new diagnostics API for the `rustc_expand` module.
r? ``@davidtwco``
  • Loading branch information
matthiaskrgr authored Aug 17, 2022
2 parents 1f8e822 + a468f13 commit 708bad6
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 25 deletions.
17 changes: 17 additions & 0 deletions compiler/rustc_error_messages/locales/en-US/expand.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,20 @@ expand_explain_doc_comment_outer =
expand_explain_doc_comment_inner =
inner doc comments expand to `#![doc = "..."]`, which is what this macro attempted to match
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
expand_count_repetition_misplaced =
`count` can not be placed inside the inner-most repetition
expand_meta_var_expr_unrecognized_var =
variable `{$key}` is not recognized in meta-variable expression
expand_var_still_repeating =
variable '{$ident}' is still repeating at this depth
expand_meta_var_dif_seq_matchers = {$msg}
3 changes: 2 additions & 1 deletion compiler/rustc_errors/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use rustc_error_messages::FluentValue;
use rustc_hir as hir;
use rustc_lint_defs::{Applicability, LintExpectationId};
use rustc_span::edition::LATEST_STABLE_EDITION;
use rustc_span::symbol::{Ident, Symbol};
use rustc_span::symbol::{Ident, MacroRulesNormalizedIdent, Symbol};
use rustc_span::{edition::Edition, Span, DUMMY_SP};
use std::borrow::Cow;
use std::fmt;
Expand Down Expand Up @@ -87,6 +87,7 @@ into_diagnostic_arg_using_display!(
hir::Target,
Edition,
Ident,
MacroRulesNormalizedIdent,
);

impl IntoDiagnosticArg for bool {
Expand Down
48 changes: 48 additions & 0 deletions compiler/rustc_expand/src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use rustc_macros::SessionDiagnostic;
use rustc_span::symbol::MacroRulesNormalizedIdent;
use rustc_span::Span;

#[derive(SessionDiagnostic)]
#[error(expand::expr_repeat_no_syntax_vars)]
pub(crate) struct NoSyntaxVarsExprRepeat {
#[primary_span]
pub span: Span,
}

#[derive(SessionDiagnostic)]
#[error(expand::must_repeat_once)]
pub(crate) struct MustRepeatOnce {
#[primary_span]
pub span: Span,
}

#[derive(SessionDiagnostic)]
#[error(expand::count_repetition_misplaced)]
pub(crate) struct CountRepetitionMisplaced {
#[primary_span]
pub span: Span,
}

#[derive(SessionDiagnostic)]
#[error(expand::meta_var_expr_unrecognized_var)]
pub(crate) struct MetaVarExprUnrecognizedVar {
#[primary_span]
pub span: Span,
pub key: MacroRulesNormalizedIdent,
}

#[derive(SessionDiagnostic)]
#[error(expand::var_still_repeating)]
pub(crate) struct VarStillRepeating {
#[primary_span]
pub span: Span,
pub ident: MacroRulesNormalizedIdent,
}

#[derive(SessionDiagnostic)]
#[error(expand::meta_var_dif_seq_matchers)]
pub(crate) struct MetaVarsDifSeqMatchers {
#[primary_span]
pub span: Span,
pub msg: String,
}
1 change: 1 addition & 0 deletions compiler/rustc_expand/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub mod base;
pub mod build;
#[macro_use]
pub mod config;
pub mod errors;
pub mod expand;
pub mod module;
pub mod proc_macro;
Expand Down
34 changes: 10 additions & 24 deletions compiler/rustc_expand/src/mbe/transcribe.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::base::ExtCtxt;
use crate::errors::{
CountRepetitionMisplaced, MetaVarExprUnrecognizedVar, MetaVarsDifSeqMatchers, MustRepeatOnce,
NoSyntaxVarsExprRepeat, VarStillRepeating,
};
use crate::mbe::macro_parser::{MatchedNonterminal, MatchedSeq, MatchedTokenTree, NamedMatch};
use crate::mbe::{self, MetaVarExpr};
use rustc_ast::mut_visit::{self, MutVisitor};
Expand Down Expand Up @@ -165,19 +169,15 @@ pub(super) fn transcribe<'a>(
seq @ mbe::TokenTree::Sequence(_, delimited) => {
match lockstep_iter_size(&seq, interp, &repeats) {
LockstepIterSize::Unconstrained => {
return Err(cx.struct_span_err(
seq.span(), /* blame macro writer */
"attempted to repeat an expression containing no syntax variables \
matched as repeating at this depth",
));
return Err(cx.create_err(NoSyntaxVarsExprRepeat { span: seq.span() }));
}

LockstepIterSize::Contradiction(msg) => {
// FIXME: this really ought to be caught at macro definition time... It
// happens when two meta-variables are used in the same repetition in a
// sequence, but they come from different sequence matchers and repeat
// different amounts.
return Err(cx.struct_span_err(seq.span(), &msg));
return Err(cx.create_err(MetaVarsDifSeqMatchers { span: seq.span(), msg }));
}

LockstepIterSize::Constraint(len, _) => {
Expand All @@ -193,10 +193,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`
Expand Down Expand Up @@ -239,10 +236,7 @@ pub(super) fn transcribe<'a>(
}
MatchedSeq(..) => {
// We were unable to descend far enough. This is an error.
return Err(cx.struct_span_err(
sp, /* blame the macro writer */
&format!("variable '{}' is still repeating at this depth", ident),
));
return Err(cx.create_err(VarStillRepeating { span: sp, ident }));
}
}
} else {
Expand Down Expand Up @@ -448,10 +442,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),
Expand Down Expand Up @@ -499,12 +490,7 @@ where
{
let span = ident.span;
let key = MacroRulesNormalizedIdent::new(ident);
interp.get(&key).ok_or_else(|| {
cx.struct_span_err(
span,
&format!("variable `{}` is not recognized in meta-variable expression", key),
)
})
interp.get(&key).ok_or_else(|| cx.create_err(MetaVarExprUnrecognizedVar { span, key }))
}

/// Used by meta-variable expressions when an user input is out of the actual declared bounds. For
Expand Down

0 comments on commit 708bad6

Please sign in to comment.