Skip to content

Commit 75c5ad2

Browse files
committed
review comments: use structured suggestion
1 parent 7c96d90 commit 75c5ad2

File tree

5 files changed

+52
-40
lines changed

5 files changed

+52
-40
lines changed

src/libsyntax/ext/expand.rs

+2-15
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::config::StripUnconfigured;
66
use crate::ext::base::*;
77
use crate::ext::proc_macro::collect_derives;
88
use crate::ext::hygiene::{ExpnId, SyntaxContext, ExpnInfo, ExpnKind};
9+
use crate::ext::tt::macro_rules::annotate_err_with_kind;
910
use crate::ext::placeholders::{placeholder, PlaceholderExpander};
1011
use crate::feature_gate::{self, Features, GateIssue, is_builtin_attr, emit_feature_err};
1112
use crate::mut_visit::*;
@@ -701,21 +702,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
701702
}
702703
Err(mut err) => {
703704
err.set_span(span);
704-
match kind {
705-
AstFragmentKind::Ty => {
706-
err.span_label(
707-
span,
708-
"this macro call doesn't expand to a type",
709-
);
710-
}
711-
AstFragmentKind::Pat => {
712-
err.span_label(
713-
span,
714-
"this macro call doesn't expand to a pattern",
715-
);
716-
}
717-
_ => {}
718-
};
705+
annotate_err_with_kind(&mut err, kind, span);
719706
err.emit();
720707
self.cx.trace_macros_diag();
721708
kind.dummy(span)

src/libsyntax/ext/tt/macro_rules.rs

+34-19
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::symbol::{kw, sym, Symbol};
1717
use crate::tokenstream::{DelimSpan, TokenStream, TokenTree};
1818
use crate::{ast, attr, attr::TransparencyError};
1919

20-
use errors::FatalError;
20+
use errors::{DiagnosticBuilder, FatalError};
2121
use log::debug;
2222
use syntax_pos::Span;
2323

@@ -43,6 +43,18 @@ pub struct ParserAnyMacro<'a> {
4343
arm_span: Span,
4444
}
4545

46+
pub fn annotate_err_with_kind(err: &mut DiagnosticBuilder<'_>, kind: AstFragmentKind, span: Span) {
47+
match kind {
48+
AstFragmentKind::Ty => {
49+
err.span_label(span, "this macro call doesn't expand to a type");
50+
}
51+
AstFragmentKind::Pat => {
52+
err.span_label(span, "this macro call doesn't expand to a pattern");
53+
}
54+
_ => {}
55+
};
56+
}
57+
4658
impl<'a> ParserAnyMacro<'a> {
4759
pub fn make(mut self: Box<ParserAnyMacro<'a>>, kind: AstFragmentKind) -> AstFragment {
4860
let ParserAnyMacro { site_span, macro_ident, ref mut parser, arm_span } = *self;
@@ -71,27 +83,30 @@ impl<'a> ParserAnyMacro<'a> {
7183
e.span_label(site_span, "in this macro invocation");
7284
}
7385
match kind {
74-
AstFragmentKind::Ty => {
75-
e.span_label(
76-
site_span,
77-
"this macro call doesn't expand to a type",
78-
);
79-
}
8086
AstFragmentKind::Pat if macro_ident.name == sym::vec => {
81-
e.span_label(
82-
site_span,
83-
"use a slice pattern here instead",
84-
);
87+
let mut suggestion = None;
88+
if let Ok(code) = parser.sess.source_map().span_to_snippet(site_span) {
89+
if let Some(bang) = code.find('!') {
90+
suggestion = Some(code[bang + 1..].to_string());
91+
}
92+
}
93+
if let Some(suggestion) = suggestion {
94+
e.span_suggestion(
95+
site_span,
96+
"use a slice pattern here instead",
97+
suggestion,
98+
Applicability::MachineApplicable,
99+
);
100+
} else {
101+
e.span_label(
102+
site_span,
103+
"use a slice pattern here instead",
104+
);
105+
}
85106
e.help("for more information, see https://doc.rust-lang.org/edition-guide/\
86-
rust-2018/slice-patterns.html");
87-
}
88-
AstFragmentKind::Pat => {
89-
e.span_label(
90-
site_span,
91-
"this macro call doesn't expand to a pattern",
92-
);
107+
rust-2018/slice-patterns.html");
93108
}
94-
_ => {}
109+
_ => annotate_err_with_kind(&mut e, kind, site_span),
95110
};
96111
e
97112
}));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// run-rustfix
2+
fn main() {
3+
// everything after `.as_ref` should be suggested
4+
match Some(vec![3]).as_ref().map(|v| v.as_slice()) {
5+
Some([_x]) => (), //~ ERROR unexpected `(` after qualified path
6+
_ => (),
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
// run-rustfix
12
fn main() {
2-
match Some(vec![3]) {
3-
Some(vec![x]) => (), //~ ERROR unexpected `(` after qualified path
3+
// everything after `.as_ref` should be suggested
4+
match Some(vec![3]).as_ref().map(|v| v.as_slice()) {
5+
Some(vec![_x]) => (), //~ ERROR unexpected `(` after qualified path
46
_ => (),
57
}
68
}

src/test/ui/suggestions/vec-macro-in-pattern.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
error: unexpected `(` after qualified path
2-
--> $DIR/vec-macro-in-pattern.rs:3:14
2+
--> $DIR/vec-macro-in-pattern.rs:5:14
33
|
4-
LL | Some(vec![x]) => (),
5-
| ^^^^^^^
4+
LL | Some(vec![_x]) => (),
5+
| ^^^^^^^^
66
| |
77
| unexpected `(` after qualified path
88
| in this macro invocation
9-
| use a slice pattern here instead
9+
| help: use a slice pattern here instead: `[_x]`
1010
|
1111
= help: for more information, see https://doc.rust-lang.org/edition-guide/rust-2018/slice-patterns.html
1212
= note: this warning originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

0 commit comments

Comments
 (0)