Request for Comment assert(__ctfe)

Jacob Carlborg doob at me.com
Tue Apr 7 17:50:15 UTC 2020


On 2020-04-07 13:41, Johannes Pfau wrote:
> Am Mon, 06 Apr 2020 13:49:19 +0000 schrieb Atila Neves:
> 
>> On Monday, 6 April 2020 at 13:41:52 UTC, Stefan Koch wrote:
>>> On Monday, 6 April 2020 at 10:14:24 UTC, Atila Neves wrote:
>>>> On Sunday, 5 April 2020 at 12:11:23 UTC, Stefan Koch wrote:
>>>>> [...]
>>>>
>>>> Wouldn't it be easier to skip codegen for private functions that are
>>>> never called from non CTFE contexts?
>>>
>>> Easier from the user-perspective yes.
>>>  From the compiler perspective,
>>> That's another step which may take quite a while to do correctly.
>>> The easy thing would be (Essentially an (N*M) loop over all calls and
>>> functions),
>>
>> Where N and M are all calls and private functions in one module,
>> not all code in a project.
>>
> 
> In fact, you can't do that in current D. I thought about it some time
> ago, consider this valid D code:
> 
> ---------------------------
> module foo;
> 
> private void privateFunc() {}
> 
> public void publicTemplate(T)()
> {
>      privateFunc();
> }
> ---------------------------
> 
> Let's say I compile module foo into libfoo. libfoo internally never uses
> publicTemplate. An external user instantiates publicTemplate ==>
> privateFunc needs to be emitted in libfoo.
> 
> Now you can't even check that privateFunc is never called, cause you
> can't analyze the template without instantiating it. And in general, you
> can't instantiate it without knowing the template instance type
> parameters. (In this simple example you might think this should work. But
> just imagine that I can pass in a type with an enum string member, which
> I can then mixin in the template, ....)
> 
> 
> Generally the only way to solve this seems to finally implement export
> properly, as it was proposed years ago. Then privateFunc should be
> flagged export (needed for windows DLL support and linux .so selective
> exports anyway) and you could probably strip uncalled, non-exported
> private functions.
> 
> 
> TLDR: It's certainly not easy to implement.
> 

You can also bypass "private" using __traits(getMemeber) or using an 
extern(D) declaration.

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list