Repository navigation
The vec![..] macro can be used in pattern position with bad diagnostics #61933
Copy link
Copy link
Closed
Copy link
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)A-slice-patternsArea: Slice patterns, https://github.com/rust-lang/rust/issues/23121Area: Slice patterns, https://github.com/rust-lang/rust/issues/23121D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.Diagnostics: Confusing error or lint; hard to understand for new users.D-papercutDiagnostics: An error or lint that needs small tweaks.Diagnostics: An error or lint that needs small tweaks.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
fn main() {
match Some(vec![3]) {
Some(vec![x]) => (),
_ => (),
}
}
Gives error message:
error: unexpected `(` after qualified path
--> src/main.rs:3:14
|
3 | Some(vec![x]) => (),
| ^^^^^^^
| |
| unexpected `(` after qualified path
| in this macro invocation
|
= note: this warning originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
But this is clearly wrong as there is no ( in the span pointed to.
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)A-slice-patternsArea: Slice patterns, https://github.com/rust-lang/rust/issues/23121Area: Slice patterns, https://github.com/rust-lang/rust/issues/23121D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.Diagnostics: Confusing error or lint; hard to understand for new users.D-papercutDiagnostics: An error or lint that needs small tweaks.Diagnostics: An error or lint that needs small tweaks.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Activity
estebank commentedon Jun 18, 2019
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).
czipperz commentedon Jun 23, 2019
I think it would be nice if this was special cased since it's easily fixable.
Rollup merge of rust-lang#63399 - estebank:vec-in-pat, r=Centril
Rollup merge of rust-lang#63399 - estebank:vec-in-pat, r=Centril
Rollup merge of rust-lang#63399 - estebank:vec-in-pat, r=Centril
[-]Match diagnostic unexpected `(`[/-][+]The `vec![..]` macro can be used in pattern position with bad diagnostics[/+]oli-obk commentedon Dec 18, 2020
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
macro_rulesdeclarations with an attribute that declares what their returned AST node type is. Then we markmacro_rules! vecas only producing expressions and add a check during expansion that handles this and reports errors if used wrongly. This will always produce the right error, and we can then also re-add a suggestion ifvec!specifically has been used in pattern position.1 remaining item
estebank commentedon Dec 18, 2020
I think that's a reasonable solution. Do you want to cut a PR for it and deleting the now unused code, @oli-obk?
oli-obk commentedon Dec 19, 2020
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 claimin this issue to assign it to yourselfvec!macro in https://github.com/rust-lang/rust/blob/master/library/alloc/src/macros.rs#L41-L51 to the one shown aboveforce_expr_internal_do_not_usemacro arm explaining that it is to force the ast node type to an expression, thus improving the diagnostics if used in type or pattern position./x.py test src/tests/ui --blesswhich auto-updates all.stderrfiles and update all//~ ERRORand other comments that need updating (likely you'll be able to remove some of them entirely because we now only get a single errorr? @oli-obkin the main message so it gets assigned to mebugadani commentedon Jan 16, 2021
@rustbot claim
Rollup merge of rust-lang#81080 - bugadani:vec-diag, r=oli-obk,m-ou-se
Remove `__rust_force_expr`.
Rollup merge of rust-lang#126929 - nnethercote:rm-__rust_force_expr, …
Rollup merge of rust-lang#126929 - nnethercote:rm-__rust_force_expr, …
Rollup merge of rust-lang#126929 - nnethercote:rm-__rust_force_expr, …
Unrolled build for rust-lang#126929