Skip to content

Something weird with const generic resolution in macro_rules #62433

Closed
@scottmcm

Description

@scottmcm
Member

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

Activity

petrochenkov

petrochenkov commented on Jul 6, 2019

@petrochenkov
Contributor

Probably related to #58307.
The workaround should be passing const generic names to macro_rules like local variable names would be passed.

added
A-const-genericsArea: const generics (parameters and arguments)
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
A-resolveArea: Name/path resolution done by `rustc_resolve` specifically
C-bugCategory: This is a bug.
on Jul 6, 2019
added 3 commits that reference this issue on Jul 29, 2019

Rollup merge of rust-lang#63083 - matthewjasper:parameter-hygiene, r=…

16a733b

Rollup merge of rust-lang#63083 - matthewjasper:parameter-hygiene, r=…

cbce93c

Rollup merge of rust-lang#63083 - matthewjasper:parameter-hygiene, r=…

652f13d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

      Participants

      @Centril@petrochenkov@scottmcm

      Issue actions

        Something weird with const generic resolution in macro_rules · Issue #62433 · rust-lang/rust