Issue 20479 - octal integer literals don't work with BetterC mode
Summary: octal integer literals don't work with BetterC mode
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P3 normal
Assignee: No Owner
URL:
Keywords: betterC, CTFE
Depends on:
Blocks:
 
Reported: 2020-01-04 01:56 UTC by Heromyth
Modified: 2023-01-15 06:50 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Heromyth 2020-01-04 01:56:58 UTC
import std.conv : octal;

extern(C) void main()
{
    enum a = octal!167; // bug
    enum b = octal!"167"; // ok
}
Comment 1 Dominikus Dittes Scherkl 2020-01-04 15:48:57 UTC
This is not D_as_better_C specific.
The octal template requires a string as template-parameter, because any other type would have already been interpreted as a decimal (e.g. decimal 167 would be converted to octal 347 which is very unexpected).

So I think this bug report is invalid. String is necessary.
Comment 2 Mathias LANG 2020-01-18 09:53:01 UTC
Actually the issue is valid, but it would have been nice to post the error messages you're getting.

First, using the op's code, we get the following error with the latest DMD (2.090.0):
```
dmd -betterC foo.d
/usr/local/opt/dmd/include/dlang/dmd/std/array.d(965): Error: TypeInfo cannot be used with -betterC
```

Which is obviously wrong, as `octal` should not lead to code being emitted in the binary.
Then, if we tweak the code a bit:
```
import core.stdc.stdio;
import std.conv : octal;

extern(C) void main()
{
    enum b = octal!"167";
    printf("Octal literal: %d\n", b);
}
```

We get the following link error:
```
dmd -betterC -run foo.d
Undefined symbols for architecture x86_64:
  "__D3std4conv14isOctalLiteralFNaNbNiNfxAyaZb", referenced from:
      __D3std4conv__T5octalTiZQjFNaNbNiNfxAyaZi in foo.o
ld: symbol(s) not found for architecture x86_64
```

Turning the symbol into a `static immutable` doesn't help either.
Comment 3 Heromyth 2021-06-18 07:32:12 UTC
It totally does not work now with DMD 2.097.0.

The latest error message:

```
/Library/D/dmd/src/phobos/std/array.d(982,49): Error: `TypeInfo` cannot be used with -betterC
```

OR

```
/Library/D/dmd/src/phobos/std/conv.d(4858,33): Error: array concatenation of expression `num ~ " is not an octal literal"` requires the GC which is not available with -betterC
```
Comment 4 mipri 2021-06-18 09:06:41 UTC
Issue #21492 seems to be the reason for the failure with octal!167

Issue #19268 has a lot of discussion that's relevant to octal!"167".
This specific case can be fixed just in phobos by swapping

-private T octal(T)(const string num)
+private T octal(T, string num)()

and changing callers to suit.
Comment 5 Walter Bright 2023-01-15 06:50:14 UTC
This is working in master now.