Thread overview
[Issue 22579] betterC errors are issued by the glue layer and hence skipped without codegen
[Issue 22579] Unittests defined in a template are instantiated indiscriminately in betterC mode AND cause inscrutable errors
Dec 08, 2021
Paul Backus
Jun 04, 2022
Walter Bright
December 08, 2021
https://issues.dlang.org/show_bug.cgi?id=22579

Paul Backus <snarwin+bugzilla@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |snarwin+bugzilla@gmail.com

--- Comment #1 from Paul Backus <snarwin+bugzilla@gmail.com> ---
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

--
December 08, 2021
https://issues.dlang.org/show_bug.cgi?id=22579

--- Comment #2 from Andrei Alexandrescu <andrei@erdani.com> ---
Thanks, Paul.

Raising this to blocker status because versioning won't scale without it being
fixed, see
https://github.com/dlang/phobos/pull/8309/commits/207e77af7d6aa5c384cb9a91db9e843dc2069769.

This also raises the question of instantiating and running unittests - who is responsible with each?

--
December 08, 2021
https://issues.dlang.org/show_bug.cgi?id=22579

Andrei Alexandrescu <andrei@erdani.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Hardware|x86                         |All
                 OS|Windows                     |All
           Severity|enhancement                 |blocker

--
December 08, 2021
https://issues.dlang.org/show_bug.cgi?id=22579

moonlightsentinel@disroot.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |moonlightsentinel@disroot.o
                   |                            |rg
           See Also|                            |https://issues.dlang.org/sh
                   |                            |ow_bug.cgi?id=21477
            Summary|Unittests defined in a      |betterC errors are issued
                   |template are instantiated   |by the glue layer and hence
                   |indiscriminately in betterC |skipped without codegen
                   |mode AND cause inscrutable  |
                   |errors                      |

--
December 08, 2021
https://issues.dlang.org/show_bug.cgi?id=22579

--- Comment #3 from moonlightsentinel@disroot.org ---
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).

--
December 08, 2021
https://issues.dlang.org/show_bug.cgi?id=22579

--- Comment #4 from moonlightsentinel@disroot.org ---
(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.

--
December 08, 2021
https://issues.dlang.org/show_bug.cgi?id=22579

--- Comment #5 from moonlightsentinel@disroot.org ---
(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.

--
December 08, 2021
https://issues.dlang.org/show_bug.cgi?id=22579

--- Comment #6 from moonlightsentinel@disroot.org ---
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

--
June 04, 2022
https://issues.dlang.org/show_bug.cgi?id=22579

Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |betterC
                 CC|                            |bugzilla@digitalmars.com

--
December 13
https://issues.dlang.org/show_bug.cgi?id=22579

--- Comment #7 from dlangBugzillaToGithub <robert.schadek@posteo.de> ---
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/20019

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB

--