Skip to content

The vec![..] macro can be used in pattern position with bad diagnostics #61933

Description

@czipperz
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.

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    C-bugCategory: This is a bug.
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Jun 18, 2019
  2. estebank commented on Jun 18, 2019

    @estebank
    Contributor

    This is caused by macro expansion:

    $ rustc +nightly file3.rs -Zexternal-macro-backtrace
    error: unexpected `(` after qualified path
     --> <::alloc::macros::vec macros>:3:23
      |
    1 | / ( $ elem : expr ; $ n : expr ) => (
    2 | | $ crate :: vec :: from_elem ( $ elem , $ n ) ) ; ( $ ( $ x : expr ) , * ) => (
    3 | | < [ _ ] > :: into_vec ( box [ $ ( $ x ) , * ] ) ) ; ( $ ( $ x : expr , ) * )
      | |                       ^ unexpected `(` after qualified path
    4 | | => ( $ crate :: vec ! [ $ ( $ x ) , * ] )
      | |_________________________________________- in this expansion of `vec!`
      |
     ::: file3.rs:3:14
      |
    3 |           Some(vec![x]) => (),
      |                -------
      |                |
      |                in this macro invocation
      |                in this macro invocation
    
    error: aborting due to previous error
    

    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).

  3. added
    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)
    A-slice-patternsArea: Slice patterns, https://github.com/rust-lang/rust/issues/23121
    and removed
    C-bugCategory: This is a bug.
    on Jun 18, 2019
  4. czipperz commented on Jun 23, 2019

    @czipperz
    ContributorAuthor

    I think it would be nice if this was special cased since it's easily fixable.

  5. added 3 commits that reference this issue on Aug 9, 2019
    a873606
    9ece794
    9e613c7
  6. changed the title [-]Match diagnostic unexpected `(`[/-] [+]The `vec![..]` macro can be used in pattern position with bad diagnostics[/+] on Dec 18, 2020
  7. oli-obk commented on Dec 18, 2020

    @oli-obk
    Contributor

    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_rules declarations with an attribute that declares what their returned AST node type is. Then we mark macro_rules! vec as 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 if vec! specifically has been used in pattern position.

  8. 1 remaining item

  9. estebank commented on Dec 18, 2020

    @estebank
    Contributor

    I think that's a reasonable solution. Do you want to cut a PR for it and deleting the now unused code, @oli-obk?

  10. added
    D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.
    D-papercutDiagnostics: An error or lint that needs small tweaks.
    on Dec 19, 2020
  11. added
    E-easyCall 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.
    on Dec 19, 2020
  12. oli-obk commented on Dec 19, 2020

    @oli-obk
    Contributor

    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.

    1. write @rustbot claim in this issue to assign it to yourself
    2. cherry pick 5e0401f to remove the old code
    3. change the vec! macro in https://github.com/rust-lang/rust/blob/master/library/alloc/src/macros.rs#L41-L51 to the one shown above
    4. leave some documentation on the force_expr_internal_do_not_use macro 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
    5. Run ./x.py test src/tests/ui --bless which auto-updates all .stderr files and update all //~ ERROR and other comments that need updating (likely you'll be able to remove some of them entirely because we now only get a single error
    6. open a PR (also do that if you get stuck or anything doesn't work) and write r? @oli-obk in the main message so it gets assigned to me
  13. bugadani commented on Jan 16, 2021

    @bugadani
    Contributor

    @rustbot claim

  14. added a commit that references this issue on Jan 17, 2021
    19370a4
  15. added a commit that references this issue on Jun 25, 2024
  16. added 2 commits that reference this issue on Jun 26, 2024
  17. added a commit that references this issue on Jun 27, 2024
  18. added a commit that references this issue on Jun 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)A-slice-patternsArea: Slice patterns, https://github.com/rust-lang/rust/issues/23121D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.D-papercutDiagnostics: An error or lint that needs small tweaks.E-easyCall 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.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions