Skip to content

Something weird with const generic resolution in macro_rules #62433

Closed
@scottmcm

Description

@scottmcm

This works, so const generic resolution works through macros (https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=e93e583bc759379eae9dac80ed011361)

#![feature(const_generics)]
trait Foo {}
struct Bar<T, const N: usize>(T);
trait Qux<const N: usize> {}
macro_rules! bar {
    ($t:ty) => {
        impl<T, const N: usize> Foo for $t
        {}
    }
}
bar!(Bar<T, {N}>);

This also works, so const generics work in a where guard (https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=cde12aadff1355221b94beef0782da9c)

#![feature(const_generics)]
trait Foo {}
struct Bar<T, const N: usize>(T);
trait Qux<const N: usize> {}
impl<T, const N: usize> Foo for Bar<T, {N}>
    where (): Qux<{N}>
{}

But if I combine those together, all of a sudden it has name resolution problems (https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=c11ade7c9cc0f31a0fc538816bbe0719)

#![feature(const_generics)]
trait Foo {}
struct Bar<T, const N: usize>(T);
trait Qux<const N: usize> {}
macro_rules! bar {
    ($t:ty) => {
        impl<T, const N: usize> Foo for $t
            where (): Qux<{N}>
        {}
    }
}
bar!(Bar<T, {N}>);
error[E0425]: cannot find value `N` in this scope
  --> src/lib.rs:8:28
   |
8  |             where (): Qux<{N}>
   |                            ^ not found in this scope
...
12 | bar!(Bar<T, {N}>);
   | ------------------ in this macro invocation

So something seems weird, but I have no idea what it could be

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-genericsArea: const generics (parameters and arguments)A-resolveArea: Name/path resolution done by `rustc_resolve` specificallyC-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