Skip to content

Macro expression reuse causes invalid behavior #8852

Closed
@darkf

Description

@darkf

The following code:

enum T {
    A(int),
    B(float)
}

macro_rules! test(
    ($e:expr) => (
        fn foo(a:T, b:T) -> T {
            match (a, b) {
                (A(x), A(y)) => A($e),
                (B(x), B(y)) => B($e),
                _ => fail!()
            }
        }
    )
)

test!(x + y)

fn main() {
    foo(A(1), A(2));
}

Yields the following type error:

y.rs:18:6: 18:11 error: mismatched types: expected `int` but found `float` (expected int but found float)
y.rs:18 test!(x + y)
              ^~~~~
error: aborting due to previous error

Which is wrong, as the expression should be expanded into both locations, both xs and ys having separate types.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-syntaxextArea: Syntax extensions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions