From 7e15fbab75ab919238ec86011fd54cba7ad78e68 Mon Sep 17 00:00:00 2001 From: nidnogg Date: Tue, 16 Aug 2022 18:34:13 -0300 Subject: [PATCH 1/6] Added first migration for repeated expressions without syntax vars --- .../rustc_error_messages/locales/en-US/expand.ftl | 3 +++ compiler/rustc_expand/src/mbe/transcribe.rs | 14 +++++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/expand.ftl b/compiler/rustc_error_messages/locales/en-US/expand.ftl index bdfa22e77eb2f..42519fd22ce63 100644 --- a/compiler/rustc_error_messages/locales/en-US/expand.ftl +++ b/compiler/rustc_error_messages/locales/en-US/expand.ftl @@ -3,3 +3,6 @@ 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 diff --git a/compiler/rustc_expand/src/mbe/transcribe.rs b/compiler/rustc_expand/src/mbe/transcribe.rs index e47ea83ac3809..69090cb457e75 100644 --- a/compiler/rustc_expand/src/mbe/transcribe.rs +++ b/compiler/rustc_expand/src/mbe/transcribe.rs @@ -9,6 +9,7 @@ use rustc_errors::{pluralize, PResult}; use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed}; 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}; @@ -53,6 +54,13 @@ impl<'a> Iterator for Frame<'a> { } } +#[derive(SessionDiagnostic)] +#[error(expand::expr_repeat_no_syntax_vars)] +struct NoSyntaxVarsExprRepeat { + #[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. @@ -165,11 +173,7 @@ 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) => { From be18a9bf75f6c92ee734838fd3eca9257556cf40 Mon Sep 17 00:00:00 2001 From: nidnogg Date: Tue, 16 Aug 2022 19:02:51 -0300 Subject: [PATCH 2/6] 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 42519fd22ce63..b25aaaa0e5175 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 69090cb457e75..4c8f7a59bbac2 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), From 72ce216def236055f5bee03d06085d0ec9c270a9 Mon Sep 17 00:00:00 2001 From: nidnogg Date: Tue, 16 Aug 2022 19:19:59 -0300 Subject: [PATCH 3/6] Previous commit under x.py fmt --- compiler/rustc_expand/src/mbe/transcribe.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_expand/src/mbe/transcribe.rs b/compiler/rustc_expand/src/mbe/transcribe.rs index 4c8f7a59bbac2..af5489761920f 100644 --- a/compiler/rustc_expand/src/mbe/transcribe.rs +++ b/compiler/rustc_expand/src/mbe/transcribe.rs @@ -463,7 +463,7 @@ fn count_repetitions<'a>( match matched { MatchedTokenTree(_) | MatchedNonterminal(_) => { if declared_lhs_depth == 0 { - return Err(cx.create_err( CountRepetitionMisplaced { span: sp.entire()} )); + return Err(cx.create_err(CountRepetitionMisplaced { span: sp.entire() })); } match depth_opt { None => Ok(1), From c6f9a9c410e063f59bc2f6958674099b3fe5f184 Mon Sep 17 00:00:00 2001 From: nidnogg Date: Wed, 17 Aug 2022 11:18:19 -0300 Subject: [PATCH 4/6] Moved structs to rustc_expand::errors, added several more migrations, fixed slug name --- .../locales/en-US/expand.ftl | 12 ++++- compiler/rustc_errors/src/diagnostic.rs | 3 +- compiler/rustc_expand/src/errors.rs | 50 +++++++++++++++++++ compiler/rustc_expand/src/lib.rs | 1 + compiler/rustc_expand/src/mbe/transcribe.rs | 40 +++------------ 5 files changed, 70 insertions(+), 36 deletions(-) create mode 100644 compiler/rustc_expand/src/errors.rs diff --git a/compiler/rustc_error_messages/locales/en-US/expand.ftl b/compiler/rustc_error_messages/locales/en-US/expand.ftl index b25aaaa0e5175..ee76a4f45005d 100644 --- a/compiler/rustc_error_messages/locales/en-US/expand.ftl +++ b/compiler/rustc_error_messages/locales/en-US/expand.ftl @@ -10,5 +10,13 @@ expand_expr_repeat_no_syntax_vars = 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 +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} \ No newline at end of file diff --git a/compiler/rustc_errors/src/diagnostic.rs b/compiler/rustc_errors/src/diagnostic.rs index 17e6c9e9575fd..356f9dfdb3b2e 100644 --- a/compiler/rustc_errors/src/diagnostic.rs +++ b/compiler/rustc_errors/src/diagnostic.rs @@ -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; @@ -87,6 +87,7 @@ into_diagnostic_arg_using_display!( hir::Target, Edition, Ident, + MacroRulesNormalizedIdent, ); impl IntoDiagnosticArg for bool { diff --git a/compiler/rustc_expand/src/errors.rs b/compiler/rustc_expand/src/errors.rs new file mode 100644 index 0000000000000..f8b8750a78965 --- /dev/null +++ b/compiler/rustc_expand/src/errors.rs @@ -0,0 +1,50 @@ +use rustc_macros::SessionDiagnostic; +use rustc_span::Span; +use rustc_span::symbol::{MacroRulesNormalizedIdent}; + +#[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::var_still_repeating)] +pub(crate) struct MetaVarsDifSeqMatchers { + #[primary_span] + pub span: Span, + pub msg: String, +} + + diff --git a/compiler/rustc_expand/src/lib.rs b/compiler/rustc_expand/src/lib.rs index 91a183427843e..e1dde1672c190 100644 --- a/compiler/rustc_expand/src/lib.rs +++ b/compiler/rustc_expand/src/lib.rs @@ -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; diff --git a/compiler/rustc_expand/src/mbe/transcribe.rs b/compiler/rustc_expand/src/mbe/transcribe.rs index af5489761920f..bec6d1a2df7d8 100644 --- a/compiler/rustc_expand/src/mbe/transcribe.rs +++ b/compiler/rustc_expand/src/mbe/transcribe.rs @@ -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}; @@ -7,7 +11,6 @@ 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_span::Span; @@ -54,20 +57,6 @@ impl<'a> Iterator for Frame<'a> { } } -#[derive(SessionDiagnostic)] -#[error(expand::expr_repeat_no_syntax_vars)] -struct NoSyntaxVarsExprRepeat { - #[primary_span] - 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. @@ -188,7 +177,7 @@ pub(super) fn transcribe<'a>( // 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, _) => { @@ -247,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 { @@ -428,13 +414,6 @@ 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. /// @@ -511,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 From caab20c431a05dc3f01d3810ac13b6315f2d9b70 Mon Sep 17 00:00:00 2001 From: nidnogg Date: Wed, 17 Aug 2022 11:20:37 -0300 Subject: [PATCH 5/6] Moved structs to rustc_expand::errors, added several more migrations, fixed slug name --- compiler/rustc_expand/src/errors.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/compiler/rustc_expand/src/errors.rs b/compiler/rustc_expand/src/errors.rs index f8b8750a78965..3f62120b1c81f 100644 --- a/compiler/rustc_expand/src/errors.rs +++ b/compiler/rustc_expand/src/errors.rs @@ -1,6 +1,6 @@ use rustc_macros::SessionDiagnostic; +use rustc_span::symbol::MacroRulesNormalizedIdent; use rustc_span::Span; -use rustc_span::symbol::{MacroRulesNormalizedIdent}; #[derive(SessionDiagnostic)] #[error(expand::expr_repeat_no_syntax_vars)] @@ -46,5 +46,3 @@ pub(crate) struct MetaVarsDifSeqMatchers { pub span: Span, pub msg: String, } - - From a468f131628341ff83bdabd1133f432c17fa1cbe Mon Sep 17 00:00:00 2001 From: nidnogg Date: Wed, 17 Aug 2022 13:13:07 -0300 Subject: [PATCH 6/6] Hotfix for duplicated slug name on VarStillRepeating struct --- compiler/rustc_expand/src/errors.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_expand/src/errors.rs b/compiler/rustc_expand/src/errors.rs index 3f62120b1c81f..0d7e137c7dd0c 100644 --- a/compiler/rustc_expand/src/errors.rs +++ b/compiler/rustc_expand/src/errors.rs @@ -40,7 +40,7 @@ pub(crate) struct VarStillRepeating { } #[derive(SessionDiagnostic)] -#[error(expand::var_still_repeating)] +#[error(expand::meta_var_dif_seq_matchers)] pub(crate) struct MetaVarsDifSeqMatchers { #[primary_span] pub span: Span,