March 31, 2023
https://issues.dlang.org/show_bug.cgi?id=23818

          Issue ID: 23818
           Summary: Error HMODULE not defined, please use HMODULE
           Product: D
           Version: D2
          Hardware: All
                OS: Windows
            Status: NEW
          Keywords: rejects-valid
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody@puremagic.com
          Reporter: schveiguy@gmail.com

This is some sort of heisenbug. It is reproducible on Windows, with dmd version 1.102.2, and with ldc version 1.32.0

The project should be constructed as follows:

1. Create an empty dub project.
2. Set a dependency for bindbc-sdl with exact version 1.2.4
3. Edit the code to contain this:

```d
import std.stdio;
import bindbc.sdl;
import std.file;

void main()
{
}
```

Building with dub by default, you will get the error:

```
C:\pathtodmd\..\import\std\internal\windows\advapi32.d(36,9): Error: undefined
identifier `HMODULE`, did you mean alias `HMODULE`?
```

The odd things about this error:

1. Removing *any* of the imports removes the error
2. Reordering the imports removes the error
3. Changing to a newer version of bindbc-sdl removes the error

I do not know the cause of this, or how to narrow it down further. I will note that the `HMODULE` alias is defined using a mixin template (one of very many).

It appears like whoever created this file went a bit crazy on template usage. The code is:

```d
package template DECLARE_HANDLE(string name, base = HANDLE) {
    mixin ("alias " ~ base.stringof ~ " " ~ name ~ ";");
}
```

Located here: https://github.com/dlang/dmd/blob/4430ae1fdf80c8232cfdf7ee0f82d7eeb5be81af/druntime/src/core/sys/windows/basetsd.d#L47

And the declaration looks like:

```d
mixin DECLARE_HANDLE!("HMODULE");
```

Located here: https://github.com/dlang/dmd/blob/4430ae1fdf80c8232cfdf7ee0f82d7eeb5be81af/druntime/src/core/sys/windows/windef.d#L97

I honestly can't see why we are doing this instead of:

```d
alias HMODULE = HANDLE;
```

But something regarding how this is done is messing up the compiler. So even if this is improved, the bug that causes this error should be investigated and fixed. I'm keeping all the versions in here so it can be reproduced and examined, even if we fix that problem.

--