Skip to content

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

Closed
@SimonSapin

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.

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