Skip to content

No macro hygiene for items #19700

@maxsnew

Description

@maxsnew

Uses of identifiers in a macro expansion refer to identifiers in the macro use-site and not the macro-definition site, effectively giving macros dynamic scope.

Minimal example:

#![feature(macro_rules)]

fn foo() -> bool {
    true
}

macro_rules! bar(
    () => (foo())
)

#[cfg(test)]
mod test {
    fn foo() -> bool {
        false
    }
    #[test]
    fn test() {
        assert!(bar!());
    }
}

Testing this program will result in a test failure because when bar!() expands to foo() the foo is the foo in the test submodule, whereas it should refer to the foo in the top-level module where bar! was defined.

This paper is a good resource for implementing this: http://www.cs.utah.edu/plt/publications/macromod.pdf

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions