Skip to content

Const generic in macros_rules unable to be found in function scope #60746

Closed
@memoryruins

Description

@memoryruins

nightly 2019-05-10 d595b113584f8f446957

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=8ed8182c5ced88e5436ba074014283c1

#![feature(const_generics)]

macro_rules! compile {
    ($t:ty; $e:expr) => {{
        fn compiletime<const N: $t>() -> $t {
            N
        }
        compiletime::<{ $e }>()
    }};
}

const fn square(x: usize) -> usize {
    x * x
}

fn main() {
    let mut x = compile! { usize;
        square(4) + square(5)
    };

    x += 1;
    println!("{}", x);
}

Produces the error:

error[E0425]: cannot find value `N` in this scope
  --> src/main.rs:6:13
   |
6  |               N
   |               ^ not found in this scope
...
17 |       let mut x = compile! { usize;
   |  _________________-
18 | |         square(4) + square(5)
19 | |     };
   | |_____- in this macro invocation

Without the macro:
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=3bc031a4b607a3128a5ce1f53564c181

42

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-genericsArea: const generics (parameters and arguments)A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions