D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 22261 - import expression does not work in __ctfe context
Summary: import expression does not work in __ctfe context
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-09-01 09:45 UTC by Tomoya Tanjo
Modified: 2021-12-19 01:26 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Tomoya Tanjo 2021-09-01 09:45:08 UTC
It would be nice if we can use import expressions in __ctfe context as shown below.

run.dlang.io: https://t.co/ui274K3Vqa?amp=1

```dlang
import std;

void main()
{
    enum str = load(__FILE__);
}

auto load(string path)
{
    if (__ctfe)
    {
        return import(path);
    }
    else
    {
        return readText(path);
    }
}
```

It fails to compile with the following message:
```console
> rdmd playground.d
onlineapp.d(12): Error: variable `path` cannot be read at compile time
```
Comment 1 moonlightsentinel 2021-12-19 01:26:52 UTC
The supplied code never enters CTFE because semantic analysis fails with the aforementioned error.

See https://wiki.dlang.org/User:Quickfur/Compile-time_vs._compile-time