This is a twofer.
A top-level unittest that is not marked with @betterC in Phobos causes no trouble in betterC mode. However, inside a template that's not the case:
template canon(string v)
{
unittest
{
Object[] arr = [new A(1), new B(), null];
}
}
If client code instantiates this template the unittest will be materialized.
The second bug is that the line above causes an error message without a line or any other information:
Error: `TypeInfo` cannot be used with -betterC
Comment #1 by snarwin+bugzilla — 2021-12-08T15:42:03Z
Here's a full example that reproduces the error:
--- issue22579.d
template canon(string v)
{
unittest
{
class A { this(int n) {} }
class B {}
Object[] arr = [new A(1), new B(), null];
}
}
--- main.d
import issue22579;
alias _ = canon!"";
extern(C) int main() { return 0; }
---
Compile with the following command:
---
dmd -betterC -unittest main.d issue22579.d
---
run.dlang.io link: https://run.dlang.io/is/VtkBvu
Comment #3 by moonlightsentinel — 2021-12-08T21:42:21Z
Nothing specific to unittests in this example. The underlying issue is that -betterC checks are currently issued by the glue layer and hence skipped for declarations that are not codegened (usually declarations outside of root modules - without -i).
Comment #4 by moonlightsentinel — 2021-12-08T21:45:59Z
(In reply to Paul Backus from comment #1)
> Here's a full example that reproduces the error:
That reduction is inaccurate because it actually compiles both modules. It doesn't matter whether the unittest is nested inside of a template when the module (issue22579.d) is explicitly passed on the command line.
Comment #5 by moonlightsentinel — 2021-12-08T21:47:39Z
(In reply to Andrei Alexandrescu from comment #0)
> A top-level unittest that is not marked with @betterC in Phobos causes no
> trouble in betterC mode. However, inside a template that's not the case:
@betterC is not a language / compiler feature. It's an "user" (druntime/phobos) defined annotation used by the test_extractor to mark unittests that should be tested in a generated module compiled with -betterC.
Comment #6 by moonlightsentinel — 2021-12-08T22:20:10Z
Reduced example:
--- issue22579.d
// Use auto instead of int[] to force semantic on the function body
auto foo(int i)
{
return [i];
}
int[] bar()(int i)
{
return [i];
}
--- main.d
import issue22579;
extern(C) int main()
{
// foo is emitted for issue22579 and hence only causes errors when
// the it is a root module (-i or passed on the command line)
foo(1);
// bar is instantiated in / emitted to main and hence always
// triggers errors in the glue layer
// bar(2);
return 0;
}
---
dmd -betterC -c main.d
<Success>
dmd -betterC -c main.d issue22579.d
issue22579.d(4): Error: `TypeInfo` cannot be used with -betterC
Comment #7 by robert.schadek — 2024-12-13T19:19:43Z