Skip to content

Commit 8b180ed

Browse files
authored
Rollup merge of #100651 - nidnogg:diagnostics_migration_expand_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```
2 parents b295639 + a468f13 commit 8b180ed

File tree

5 files changed

+78
-25
lines changed

5 files changed

+78
-25
lines changed

compiler/rustc_error_messages/locales/en-US/expand.ftl

+17
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,20 @@ expand_explain_doc_comment_outer =
33
44
expand_explain_doc_comment_inner =
55
inner doc comments expand to `#![doc = "..."]`, which is what this macro attempted to match
6+
7+
expand_expr_repeat_no_syntax_vars =
8+
attempted to repeat an expression containing no syntax variables matched as repeating at this depth
9+
10+
expand_must_repeat_once =
11+
this must repeat at least once
12+
13+
expand_count_repetition_misplaced =
14+
`count` can not be placed inside the inner-most repetition
15+
16+
expand_meta_var_expr_unrecognized_var =
17+
variable `{$key}` is not recognized in meta-variable expression
18+
19+
expand_var_still_repeating =
20+
variable '{$ident}' is still repeating at this depth
21+
22+
expand_meta_var_dif_seq_matchers = {$msg}

compiler/rustc_errors/src/diagnostic.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_error_messages::FluentValue;
88
use rustc_hir as hir;
99
use rustc_lint_defs::{Applicability, LintExpectationId};
1010
use rustc_span::edition::LATEST_STABLE_EDITION;
11-
use rustc_span::symbol::{Ident, Symbol};
11+
use rustc_span::symbol::{Ident, MacroRulesNormalizedIdent, Symbol};
1212
use rustc_span::{edition::Edition, Span, DUMMY_SP};
1313
use std::borrow::Cow;
1414
use std::fmt;
@@ -87,6 +87,7 @@ into_diagnostic_arg_using_display!(
8787
hir::Target,
8888
Edition,
8989
Ident,
90+
MacroRulesNormalizedIdent,
9091
);
9192

9293
impl IntoDiagnosticArg for bool {

compiler/rustc_expand/src/errors.rs

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use rustc_macros::SessionDiagnostic;
2+
use rustc_span::symbol::MacroRulesNormalizedIdent;
3+
use rustc_span::Span;
4+
5+
#[derive(SessionDiagnostic)]
6+
#[error(expand::expr_repeat_no_syntax_vars)]
7+
pub(crate) struct NoSyntaxVarsExprRepeat {
8+
#[primary_span]
9+
pub span: Span,
10+
}
11+
12+
#[derive(SessionDiagnostic)]
13+
#[error(expand::must_repeat_once)]
14+
pub(crate) struct MustRepeatOnce {
15+
#[primary_span]
16+
pub span: Span,
17+
}
18+
19+
#[derive(SessionDiagnostic)]
20+
#[error(expand::count_repetition_misplaced)]
21+
pub(crate) struct CountRepetitionMisplaced {
22+
#[primary_span]
23+
pub span: Span,
24+
}
25+
26+
#[derive(SessionDiagnostic)]
27+
#[error(expand::meta_var_expr_unrecognized_var)]
28+
pub(crate) struct MetaVarExprUnrecognizedVar {
29+
#[primary_span]
30+
pub span: Span,
31+
pub key: MacroRulesNormalizedIdent,
32+
}
33+
34+
#[derive(SessionDiagnostic)]
35+
#[error(expand::var_still_repeating)]
36+
pub(crate) struct VarStillRepeating {
37+
#[primary_span]
38+
pub span: Span,
39+
pub ident: MacroRulesNormalizedIdent,
40+
}
41+
42+
#[derive(SessionDiagnostic)]
43+
#[error(expand::meta_var_dif_seq_matchers)]
44+
pub(crate) struct MetaVarsDifSeqMatchers {
45+
#[primary_span]
46+
pub span: Span,
47+
pub msg: String,
48+
}

compiler/rustc_expand/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub mod base;
2525
pub mod build;
2626
#[macro_use]
2727
pub mod config;
28+
pub mod errors;
2829
pub mod expand;
2930
pub mod module;
3031
pub mod proc_macro;

compiler/rustc_expand/src/mbe/transcribe.rs

+10-24
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
use crate::base::ExtCtxt;
2+
use crate::errors::{
3+
CountRepetitionMisplaced, MetaVarExprUnrecognizedVar, MetaVarsDifSeqMatchers, MustRepeatOnce,
4+
NoSyntaxVarsExprRepeat, VarStillRepeating,
5+
};
26
use crate::mbe::macro_parser::{MatchedNonterminal, MatchedSeq, MatchedTokenTree, NamedMatch};
37
use crate::mbe::{self, MetaVarExpr};
48
use rustc_ast::mut_visit::{self, MutVisitor};
@@ -165,19 +169,15 @@ pub(super) fn transcribe<'a>(
165169
seq @ mbe::TokenTree::Sequence(_, delimited) => {
166170
match lockstep_iter_size(&seq, interp, &repeats) {
167171
LockstepIterSize::Unconstrained => {
168-
return Err(cx.struct_span_err(
169-
seq.span(), /* blame macro writer */
170-
"attempted to repeat an expression containing no syntax variables \
171-
matched as repeating at this depth",
172-
));
172+
return Err(cx.create_err(NoSyntaxVarsExprRepeat { span: seq.span() }));
173173
}
174174

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

183183
LockstepIterSize::Constraint(len, _) => {
@@ -193,10 +193,7 @@ pub(super) fn transcribe<'a>(
193193
// FIXME: this really ought to be caught at macro definition
194194
// time... It happens when the Kleene operator in the matcher and
195195
// the body for the same meta-variable do not match.
196-
return Err(cx.struct_span_err(
197-
sp.entire(),
198-
"this must repeat at least once",
199-
));
196+
return Err(cx.create_err(MustRepeatOnce { span: sp.entire() }));
200197
}
201198
} else {
202199
// 0 is the initial counter (we have done 0 repetitions so far). `len`
@@ -239,10 +236,7 @@ pub(super) fn transcribe<'a>(
239236
}
240237
MatchedSeq(..) => {
241238
// We were unable to descend far enough. This is an error.
242-
return Err(cx.struct_span_err(
243-
sp, /* blame the macro writer */
244-
&format!("variable '{}' is still repeating at this depth", ident),
245-
));
239+
return Err(cx.create_err(VarStillRepeating { span: sp, ident }));
246240
}
247241
}
248242
} else {
@@ -448,10 +442,7 @@ fn count_repetitions<'a>(
448442
match matched {
449443
MatchedTokenTree(_) | MatchedNonterminal(_) => {
450444
if declared_lhs_depth == 0 {
451-
return Err(cx.struct_span_err(
452-
sp.entire(),
453-
"`count` can not be placed inside the inner-most repetition",
454-
));
445+
return Err(cx.create_err(CountRepetitionMisplaced { span: sp.entire() }));
455446
}
456447
match depth_opt {
457448
None => Ok(1),
@@ -499,12 +490,7 @@ where
499490
{
500491
let span = ident.span;
501492
let key = MacroRulesNormalizedIdent::new(ident);
502-
interp.get(&key).ok_or_else(|| {
503-
cx.struct_span_err(
504-
span,
505-
&format!("variable `{}` is not recognized in meta-variable expression", key),
506-
)
507-
})
493+
interp.get(&key).ok_or_else(|| cx.create_err(MetaVarExprUnrecognizedVar { span, key }))
508494
}
509495

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

0 commit comments

Comments
 (0)