Skip to content

macro with repeating arguments fails with ambiguous parse #5902

Closed

Description

@metajack

The following macro won't work without the extra set of parens:

macro_rules! handle_element(
    ($tag:expr, $string:expr, $type_id:expr, $ctor:ident, [ $(($field:ident : $field_init:expr)),* ]) => (
        if eq_slice($tag, $string) {
            let _element = ~$ctor {
                parent: Element::new($type_id, ($tag).to_str()),
                $(
                    $field: $field_init,
                )*
            };
            unsafe {
                return Node::as_abstract_node(_element);
            }
        }
    )
)

Here's an example use:

    handle_element!(tag, "ul", HTMLUListElementTypeId, HTMLUListElement, []);
    handle_element!(tag, "img", HTMLImageElementTypeId, HTMLImageElement, [(image: None)]);

I'd like to remove those extra parens. It seems like [ $($field:ident),* ] should be unambiguous, but rustc doesn't like it.

The actual error is error: Local ambiguity: multiple parsing options: built-in NTs ident ('field') or 1 other options. Also, it seems like it should tell me the other options so I have some clue what is going wrong.

Activity

  1. catamorphism commented on Jul 16, 2013

    @catamorphism
    Contributor

    Nominating for milestone 1, well-defined (part of figuring out what macros actually parse)

  2. graydon commented on Jul 18, 2013

    @graydon
    Contributor

    Related to #3232 which has to do with the implementation of parsing being able to stop / reset / restart.

    But I want to have some bug on file for well-definedness that says which grammar-class of macros we support. The current macro tutorial at http://static.rust-lang.org/doc/tutorial-macros.html has a little discussion of it, but is not entirely clear. I believe the class is "1-token predictive grammars" which is the non-backtracking subset of LL(1), quite limited. It's fine if that's our limitation -- it's still much more than in other languages -- but we ought to say so.

    (We discussed this in the nomination meeting today when we hit #3232 and decided to accept "some bug" for the well-definedness of the macro parser, and leave #3232 on the problem of its implementation)

  3. huonw commented on Sep 23, 2013

    @huonw
    Contributor

    Triage: still an issue. Specifically, the following fails

    macro_rules! handle_element(
        ($tag:expr, $string:expr, $type_id:expr, $ctor:ident, [$( $field:ident : $field_init:expr),*] ) => (
            if eq_slice($tag, $string) {
                let _element = ~$ctor {
                    parent: Element::new($type_id, ($tag).to_str()),
                    $(
                        $field: $field_init,
                    )*
                };
                unsafe {
                    return Node::as_abstract_node(_element);
                }
            }
        )
    )
    
    fn main() {
        handle_element!(tag, "ul", HTMLUListElementTypeId, HTMLUListElement, []);
        handle_element!(tag, "img", HTMLImageElementTypeId, HTMLImageElement, [image: None]);
    }
    5902.rs:18:74: 18:75 error: Local ambiguity: multiple parsing options: built-in NTs ident ('field') or 1 other options.
    5902.rs:18     handle_element!(tag, "ul", HTMLUListElementTypeId, HTMLUListElement, []);
                                                                                         ^
    

    FWIW, the example in the issue is one of many working versions, e.g. $ctor:ident $(, $field:ident: $field_init:expr)* also works (called like:

    handle_element!(tag, "ul", HTMLUListElementTypeId, HTMLUListElement);
    handle_element!(tag, "img", HTMLImageElementTypeId, HTMLImageElement, image: None);

    ).

  4. added a commit that references this issue on Aug 21, 2026
    a0f1346
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-parserA-syntaxext

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions