Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: inconsistent typing for reintroduced symbol #11494

Closed
yglukhov opened this issue Jun 13, 2019 · 4 comments

Comments

@yglukhov
Copy link
Member

commented Jun 13, 2019

The following code used to work with 0.19.*, but now it doesn't.

import macros

macro staticForEach(arr: untyped, body: untyped): untyped =
    result = newNimNode(nnkStmtList)

    arr.expectKind(nnkBracket)
    for n in arr:
        let b = copyNimTree(body)
        result.add quote do:
            block:
                type it {.inject.} = `n`
                `b`

template forEveryMatchingEntity*() =
    staticForEach([int, string, float]):
        var a: it
        echo a

forEveryMatchingEntity()
nim c test.nim
/home/yglukhov/test.nim(21, 23) template/generic instantiation of `forEveryMatchingEntity` from here
/home/yglukhov/test.nim(18, 13) Error: inconsistent typing for reintroduced symbol 'a': previous type was: it; new type is: it

@yglukhov yglukhov added the Regression label Jun 13, 2019

@yglukhov

This comment has been minimized.

Copy link
Member Author

commented Jun 16, 2019

Offending commit: e874705

@Araq

This comment has been minimized.

Copy link
Member

commented Jul 6, 2019

I improved the error message but IMO the compiler is correct, you should use:

import macros

macro staticForEach(arr: untyped, body: untyped): untyped =
    result = newNimNode(nnkStmtList)

    arr.expectKind(nnkBracket)
    for n in arr:
        let b = copyNimTree(body)
        result.add quote do:
            block:
                type it {.inject.} = `n`
                `b`

template forEveryMatchingEntity*() =
    staticForEach([int, string, float]):
        var a {.inject.}: it
        echo a

forEveryMatchingEntity()

@Araq Araq closed this in 465815e Jul 6, 2019

@yglukhov

This comment has been minimized.

Copy link
Member Author

commented Jul 7, 2019

I still think there's an issue here because the following code works:

import macros

macro staticForEach(arr: untyped, body: untyped): untyped =
    result = newNimNode(nnkStmtList)

    arr.expectKind(nnkBracket)
    for n in arr:
        let b = copyNimTree(body)
        result.add quote do:
            block:
                type it {.inject.} = `n`
                `b`

staticForEach([int, string, float]):
    var a: it
    echo a

Note the missing inject on a symbol above.
Also I don't understand why inject is needed on a symbol as you suggest it. There's no namespace "mixing" where the symbol is introduced.

@Araq

This comment has been minimized.

Copy link
Member

commented Jul 8, 2019

The template forEveryMatchingEntity does make the difference, inside it var a is marked as gensym. And gensym'ed symbols are not replicated in this case. Hmm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.