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
The vec![..]
macro can be used in pattern position with bad diagnostics
#61933
Comments
This is caused by macro expansion:
You cannot pattern match on vectors, but you can pattern match on slices (and even rely on match ergonomics to make it look like you're matching on an array). |
I think it would be nice if this was special cased since it's easily fixable. |
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) ``` Fix rust-lang#61933.
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) ``` Fix rust-lang#61933.
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) ``` Fix rust-lang#61933.
(
vec![..]
macro can be used in pattern position with bad diagnostics
I reopened this issue (with a title update), because #80080 will reintroduce the issue (with an even odder diagnostic). While trying to come up with a way to fix this issue again with the new system, I came up with the following situation: Add a way to mark |
I did some more experimentation, and we could also get away with just changing the macro_rules! vec {
() => (
vec!(@force_expr_internal_do_not_use@: $crate::vec::Vec::new())
);
($elem:expr; $n:expr) => (
vec!(@force_expr_internal_do_not_use@: $crate::vec::from_elem($elem, $n))
);
($($x:expr),+ $(,)?) => (
vec!(@force_expr_internal_do_not_use@: <[_]>::into_vec(box [$($x),+]))
);
(@force_expr_internal_do_not_use@: $e:expr) => ($e);
} This gives us
though line 15 is probably not going to get shown since the macro is from liballoc |
I think that's a reasonable solution. Do you want to cut a PR for it and deleting the now unused code, @oli-obk? |
I'll write up instructions and hope that someone will pick it up as a good first issue over the holidays. If not, I'll do it before #80080 gets merged.
|
@rustbot claim |
Gives error message:
But this is clearly wrong as there is no
(
in the span pointed to.The text was updated successfully, but these errors were encountered: