Skip to content

Syntax extensions (apparently) can not return multiple items. #16723

Closed

Description

@SimonSapin

Since #10649, macro_rules! macros can return multiple items. But there is no easy way to do so from a syntax extension: the result value must implement MacResult and the appropriate implementation appear to be MacItem, but it can only hold one item.

In servo/servo#3142, I worked around this with a new MacItems (plural) type:

struct MacItems {
    items: SmallVector<Gc<ast::Item>>
}

impl MacItems {
    fn new<I: Iterator<Gc<ast::Item>>>(items: I) -> Box<MacResult> {
        box MacItems { items: items.collect() } as Box<MacResult>
    }

impl MacResult for MacItems {
    fn make_items(&self) -> Option<SmallVector<Gc<ast::Item>>> {
        // FIXME: Implement Clone on SmallVector and use it instead of .iter().collect()
        Some(self.items.iter().collect())
    }
}

Perhaps this could be added to libsyntax or merged with MacItem.

Activity

  1. huonw commented on Sep 5, 2014

    @huonw
    Contributor

    Seems reasonable to just extend MacItem (tagging as easy since a perfectly good implementation is in the issue :) ).

  2. added a commit that references this issue on Sep 19, 2014
    89b0944
  3. added a commit that references this issue on Mar 10, 2024
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-syntaxextArea: Syntax extensionsE-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions