Skip to content
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
33 changes: 22 additions & 11 deletions compiler/rustc_expand/src/mbe/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rustc_ast_pretty::pprust;
use rustc_attr_ir::diagnostic::Directive;
use rustc_attr_ir::{self as attrs, find_attr};
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
use rustc_errors::{Applicability, Diag, ErrorGuaranteed, MultiSpan};
use rustc_errors::{Applicability, Diag, ErrorGuaranteed, MultiSpan, StringPart};
use rustc_feature::Features;
use rustc_hir::def::MacroKinds;
use rustc_lint_defs::builtin::{
Expand Down Expand Up @@ -1643,21 +1643,32 @@ fn check_matcher_core<'tt>(
);
}

let msg = "allowed there are: ";
match possible {
&[] => {}
&[t] => {
err.note(format!(
"only {t} is allowed after `{kind}` fragments",
));
err.highlighted_note(vec![
StringPart::normal("only "),
StringPart::highlighted(t),
StringPart::normal(format!(
" is allowed after `{kind}` fragments"
)),
]);
}
ts => {
err.note(format!(
"{}{} or {}",
msg,
ts[..ts.len() - 1].to_vec().join(", "),
ts[ts.len() - 1],
));
let mut msg_parts =
vec![StringPart::normal("allowed there are: ")];

for (i, &t) in ts.iter().enumerate() {
if i == ts.len() - 1 {
msg_parts.push(StringPart::normal(" or "));
} else if i > 0 {
msg_parts.push(StringPart::normal(", "));
}

msg_parts.push(StringPart::highlighted(t));
}

err.highlighted_note(msg_parts);
}
}
errored = Err(err.emit_err());
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/macros/highlight-symbols-after-fragment-issue-156458.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Make sure accepted characters in the macro syntax warning message is highlighted in magenta.

//@ compile-flags: --error-format=human --color=always
//@ edition:2018

macro_rules! m {
($e:expr ~ $id:ident) => {};
}

fn main() {}

//~? RAW `=>`, `,` or `;`
47 changes: 47 additions & 0 deletions tests/ui/macros/highlight-symbols-after-fragment-issue-156458.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading