Skip to content

mbe: Rework diagnostics for metavariable expressions. #142950

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions compiler/rustc_expand/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,6 @@ expand_malformed_feature_attribute =

expand_meta_var_dif_seq_matchers = {$msg}

expand_meta_var_expr_unrecognized_var =
variable `{$key}` is not recognized in meta-variable expression

expand_missing_fragment_specifier = missing fragment specifier
.note = fragment specifiers must be provided
.suggestion_add_fragspec = try adding a specifier here
Expand All @@ -136,6 +133,41 @@ expand_module_multiple_candidates =
expand_must_repeat_once =
this must repeat at least once

expand_mve_expected_ident =
expected an identifier
.not_ident = not a valid identifier
.expr_name = expected a metavariable expression name: `{"${expr( /* ... */ )}"}`
.expr_name_note = valid metavariable expressions are {$valid_expr_list}
.ignore_expr_note = `ignore` takes a metavariable argument
.count_expr_note = `count` takes a metavariable argument

expand_mve_extra_tokens_in_braces =
unexpected trailing tokens in metavariable expression braces
.suggestion = try removing these tokens

expand_mve_extra_tokens_in_expr =
unexpected trailing tokens in metavariable expression
.label = for this metavariable expression
.note= the `{$name}` metavariable expression takes up to {$max} arguments
.suggestion = try removing {$count ->
[one] this token
*[other] these tokens
}

expand_mve_missing_paren =
expected `(`
.label = for this this metavariable expression
.note = metavariable expressions use function-like parentheses syntax
.suggestion = try adding parentheses

expand_mve_unrecognized_expr =
unrecognized metavariable expression
.label = not a valid metavariable expression
.note = valid metavariable expressions are {$valid_expr_list}

expand_mve_unrecognized_var =
variable `{$key}` is not recognized in meta-variable expression

expand_non_inline_modules_in_proc_macro_input_are_unstable =
non-inline modules in proc macro input are unstable

Expand Down
87 changes: 79 additions & 8 deletions compiler/rustc_expand/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@ pub(crate) struct CountRepetitionMisplaced {
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(expand_meta_var_expr_unrecognized_var)]
pub(crate) struct MetaVarExprUnrecognizedVar {
#[primary_span]
pub span: Span,
pub key: MacroRulesNormalizedIdent,
}

#[derive(Diagnostic)]
#[diag(expand_var_still_repeating)]
pub(crate) struct VarStillRepeating {
Expand Down Expand Up @@ -500,3 +492,82 @@ pub(crate) struct ProcMacroBackCompat {
pub crate_name: String,
pub fixed_version: String,
}

pub(crate) use metavar_exprs::*;
mod metavar_exprs {
use super::*;

#[derive(Diagnostic)]
#[diag(expand_mve_expected_ident)]
pub(crate) struct MveExpectedIdent {
#[primary_span]
pub span: Span,
#[label(expand_not_ident)]
pub not_ident_label: Option<Span>,
/// This error is reused a handful of places, the context here tells us how to customize
/// the message.
#[subdiagnostic]
pub context: MveExpectedIdentContext,
}

#[derive(Subdiagnostic)]
pub(crate) enum MveExpectedIdentContext {
#[note(expand_expr_name)]
#[note(expand_expr_name_note)]
ExprName { valid_expr_list: &'static str },
#[note(expand_ignore_expr_note)]
Ignore,
#[note(expand_count_expr_note)]
Count,
}

#[derive(Diagnostic)]
#[diag(expand_mve_extra_tokens_in_braces)]
pub(crate) struct MveExtraTokensInBraces {
#[primary_span]
#[suggestion(code = "", applicability = "machine-applicable")]
pub span: Span,
}

#[derive(Diagnostic)]
#[note]
#[diag(expand_mve_extra_tokens_in_expr)]
pub(crate) struct MveExtraTokensInExpr {
#[primary_span]
#[suggestion(code = "", applicability = "machine-applicable")]
pub span: Span,
#[label]
pub ident_span: Span,
pub count: usize,
pub max: usize,
pub name: &'static str,
}

#[derive(Diagnostic)]
#[note]
#[diag(expand_mve_missing_paren)]
pub(crate) struct MveMissingParen {
#[primary_span]
pub span: Span,
#[suggestion(code = "( /* ... */ )", applicability = "has-placeholders")]
pub insert_span: Option<Span>,
}

#[derive(Diagnostic)]
#[note]
#[diag(expand_mve_unrecognized_expr)]
pub(crate) struct MveUnrecognizedExpr {
#[primary_span]
#[label]
pub span: Span,
pub valid_expr_list: &'static str,
}

#[derive(Diagnostic)]
#[diag(expand_mve_unrecognized_var)]
pub(crate) struct MveUnrecognizedVar {
#[primary_span]
pub span: Span,
pub key: MacroRulesNormalizedIdent,
}
}
Loading
Loading