Skip to content

'Cannot find value in this scope' on macro expansion with generic over const impl block inside. #61574

Closed
@L117

Description

@L117

I kinda hoped that snippet below will be compiled:

#![feature(const_generics)]

use std::ops::Add;

struct VectorLike<T, const SIZE: usize>([T; {SIZE}]);

macro_rules! impl_operator_overload {
    ($trait_ident:ident, $method_ident:ident) => {
    
        impl<T, const SIZE: usize> $trait_ident for VectorLike<T, {SIZE}> 
        where
            T: $trait_ident,
        {
            type Output = VectorLike<T, {SIZE}>;
            
            fn $method_ident(self, _: VectorLike<T, {SIZE}>) -> VectorLike<T, {SIZE}> {
                unimplemented!()
            }
        }
        
    }
}

impl_operator_overload!(Add, add);

Instead I got a bunch of errors like

error[E0425]: cannot find value `SIZE` in this scope

for SIZE in every occurrence of {SIZE} inside macro body, and one

the const parameter `SIZE` is not constrained by the impl trait, self type, or predicates

for SIZE in impl<T, const SIZE: usize>.

Playground permalink

Unlike snippet with "const variable" above, this one with "const value" compiles without any
issues:

#![feature(const_generics)]

use std::ops::Add;

struct VectorLike<T, const SIZE: usize>([T; {SIZE}]);

macro_rules! impl_operator_overload {
    ($trait_ident:ident, $method_ident:ident) => {
    
        impl<T> $trait_ident for VectorLike<T, 3> 
        where
            T: $trait_ident,
        {
            type Output = VectorLike<T, 3>;
            
            fn $method_ident(self, _: VectorLike<T, 3>) -> VectorLike<T, 3> {
                unimplemented!()
            }
        }
        
    }
}

impl_operator_overload!(Add, add);

This issue seems to be related to #58307.

Rustc version: 1.37.0-nightly (2019-06-04 5d8f59f4b1473217c2de).

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