Skip to content

Commit 7c96d90

Browse files
committed
More explicit diagnostic when using a vec![] in a pattern
``` error: unexpected `(` after qualified path --> $DIR/vec-macro-in-pattern.rs:3:14 | LL | Some(vec![x]) => (), | ^^^^^^^ | | | unexpected `(` after qualified path | in this macro invocation | use a slice pattern here instead | = help: for more information, see https://doc.rust-lang.org/edition-guide/rust-2018/slice-patterns.html = note: this warning originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) ```
1 parent 813a3a5 commit 7c96d90

File tree

6 files changed

+68
-7
lines changed

6 files changed

+68
-7
lines changed

src/libsyntax/ext/expand.rs

+22-6
Original file line numberDiff line numberDiff line change
@@ -686,12 +686,13 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
686686
);
687687
}
688688

689-
fn parse_ast_fragment(&mut self,
690-
toks: TokenStream,
691-
kind: AstFragmentKind,
692-
path: &Path,
693-
span: Span)
694-
-> AstFragment {
689+
fn parse_ast_fragment(
690+
&mut self,
691+
toks: TokenStream,
692+
kind: AstFragmentKind,
693+
path: &Path,
694+
span: Span,
695+
) -> AstFragment {
695696
let mut parser = self.cx.new_parser_from_tts(&toks.into_trees().collect::<Vec<_>>());
696697
match parser.parse_ast_fragment(kind, false) {
697698
Ok(fragment) => {
@@ -700,6 +701,21 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
700701
}
701702
Err(mut err) => {
702703
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+
};
703719
err.emit();
704720
self.cx.trace_macros_diag();
705721
kind.dummy(span)

src/libsyntax/ext/tt/macro_rules.rs

+23
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,29 @@ impl<'a> ParserAnyMacro<'a> {
7070
} else if !parser.sess.source_map().span_to_filename(parser.token.span).is_real() {
7171
e.span_label(site_span, "in this macro invocation");
7272
}
73+
match kind {
74+
AstFragmentKind::Ty => {
75+
e.span_label(
76+
site_span,
77+
"this macro call doesn't expand to a type",
78+
);
79+
}
80+
AstFragmentKind::Pat if macro_ident.name == sym::vec => {
81+
e.span_label(
82+
site_span,
83+
"use a slice pattern here instead",
84+
);
85+
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+
);
93+
}
94+
_ => {}
95+
};
7396
e
7497
}));
7598

src/test/ui/proc-macro/lifetimes.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: expected type, found `'`
22
--> $DIR/lifetimes.rs:9:10
33
|
44
LL | type A = single_quote_alone!();
5-
| ^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^^^^^^ this macro call doesn't expand to a type
66

77
error: aborting due to previous error
88

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn main() {
2+
match Some(vec![3]) {
3+
Some(vec![x]) => (), //~ ERROR unexpected `(` after qualified path
4+
_ => (),
5+
}
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: unexpected `(` after qualified path
2+
--> $DIR/vec-macro-in-pattern.rs:3:14
3+
|
4+
LL | Some(vec![x]) => (),
5+
| ^^^^^^^
6+
| |
7+
| unexpected `(` after qualified path
8+
| in this macro invocation
9+
| use a slice pattern here instead
10+
|
11+
= help: for more information, see https://doc.rust-lang.org/edition-guide/rust-2018/slice-patterns.html
12+
= note: this warning originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
13+
14+
error: aborting due to previous error
15+

src/test/ui/type/ascription/issue-47666.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ LL | let _ = Option:Some(vec![0, 1]);
66
| | |
77
| | expected type
88
| | in this macro invocation
9+
| | this macro call doesn't expand to a type
910
| help: maybe write a path separator here: `::`
1011
|
1112
= note: `#![feature(type_ascription)]` lets you annotate an expression with a type: `<expr>: <type>`

0 commit comments

Comments
 (0)