Issue 20905 - Type definition inside a static if results in an undefined identifier
Summary: Type definition inside a static if results in an undefined identifier
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 major
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-06-07 18:01 UTC by KytoDragon
Modified: 2022-12-17 10:38 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description KytoDragon 2020-06-07 18:01:11 UTC
This code:

struct Bar {
    Foo foo;  
}

static if (true) {
    alias my_type = ushort;
}

struct Foo {
    my_type index;  
}

results in this error:

test.d(11): Error: undefined identifier `my_type`

Removing Bar or moving Bar after the static if fixes it. This also happens, if Bar is defined in another file and imported into the one with the static if.

Reproduced on Windows x64 with DMD 2.092.0 and LDC 1.21.0.
Comment 1 KytoDragon 2020-06-14 13:14:52 UTC
I changed the importance to major since if you import the file that contains the static if this can't even be fixed by reordering symbols/imports.
Comment 2 KytoDragon 2020-10-03 09:55:19 UTC
It turns out you don't even need an alias for it to fail:

struct Bar {
    Foo foo;  
}

static if (true) {
    struct my_type {
        int a;
    }
}

struct Foo {
    my_type index;  
}

test.d(12): Error: undefined identifier `my_type`
Comment 3 foerdi 2020-11-08 19:39:01 UTC
It seems that this depends on the definition order.

A reduced example:
```
struct Foo {
    my_type index;  
}

static if(true) {
    alias my_type = int;
}
```
Error: undefined identifier my_type

This example works:
```
static if(true) {
    alias my_type = int;
}

struct Foo {
    my_type index;  
}
```