www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 22663] New: Module is not recognized inside an "if"

https://issues.dlang.org/show_bug.cgi?id=22663

          Issue ID: 22663
           Summary: Module is not recognized inside an "if"
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: rempas tutanota.com

I'm having a very very weird bug. So the main idea is that I have a module
called "number_conv" which is inside a package called "enums". So we can
imagine the file structure like that:


 dub.sdl
 source
   | app.d
   | conv.d // The actual file that does the calls to the enum from
"number_conv"
   | enums
     | number_conv.d
So the problem here is that I have defined an enum inside the "number_conv" module called "digit_count". When I try to use this enum, I'm calling it using the following syntax `number_conv.digit_count!`. This will work as expected except for one call that will not recognize "number_conv" and say that it is an unknown identifier. I have created the smallest possible files to represent the actual problem and tested them inside the "/tmp" directory. Here are the files File: test.d Code: ``` import enums.number_conv; enum is_same(alias value, T) = is(typeof(value) == T); enum is_signed(alias value) = is_same!(value, byte) || is_same!(value, short) || is_same!(value, int) || is_same!(value, long); enum is_unsigned(alias value) = is_same!(value, ubyte) || is_same!(value, ushort) || is_same!(value, uint) || is_same!(value, ulong); char* to_str(T)(T num, ubyte base = 10) { static if (is_unsigned!num) { ubyte i; static if (is_same!(num, ubyte)) { mixin(number_conv.digit_count!("4", "3", "9", "4")); } else static if (is_same!(num, ushort)) { mixin(number_conv.digit_count!("6", "5", "17", "7")); } else static if (is_same!(num, uint)) { mixin(number_conv.digit_count!("11", "9", "33", "12")); } else { mixin(number_conv.digit_count!("21", "17", "65", "23")); } return cast(char*)"0"; } else static if (is_signed!num) { if (num == 0) bool min_num = false; ubyte i; static if (is_same!(num, byte)) { mixin(number_conv.digit_count!("5", "4", "10", "5")); } else static if (is_same!(num, short)) { mixin(number_conv.digit_count!("7", "6", "18", "8")); } else static if (is_same!(num, int)) { mixin(number_conv.digit_count!("12", "10", "34", "13")); // If we remove "number_conv" here and just use "digit_count!", it will work } else { mixin(number_conv.digit_count!("21", "18", "66", "24")); } return cast(char*)"0"; } } extern (C) void main() { import core.stdc.stdio; printf("The number is %s", to_str(2737)); } ``` File: enums/number_conv.d Code: ``` module enums.number_conv; enum digit_count(string dec, string hex, string bin, string oct) = ` if (base == 10) i = ` ~ dec ~ `; else if (base == 16) i = ` ~ hex ~ `; else if (base == 2) i = ` ~ bin ~ `; else if (base == 8) i = ` ~ oct ~ `; `; ``` --
Jan 10 2022