dart-lang / dart-lang/language

Make compile-time environment be required for compilation. (No delayed `.fromEnvironment`)

Open
#2,807 7 comments 1 reaction 0 assignees View on GitHub
request
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

Today our compilation tools allow, e.g, `int.fromEnvironment` to be delayed until a later step. Any step up to constant evaluation does not need to know, but steps after that do, and we have things that front-ends want to do which depend on constant evaluation.
That, in turn, theoretically allows modular compilation to reuse the same artifact with different environments provided.

It also complicates compilation. Anywhere the language specification assumes constants to be evaluated before something else can happen, compilers must postpone that other thing until a compilation environment is provided.

With the patterns feature, constant evaluation has to happen *before* some type inference — so probably during the type inference phase, like almost everything else, since we can't evaluate code without knowing its own static type.

In patterns, constant patterns may contain constant variables, which may be initialized by, e.g., `bool.fromEnvironment`.
At that point, it's not possible to say whether a map pattern (or map literal for that matter) has repeated keys.
It's not possible to say whether a switch is exhaustive until we know the actual constant values used by the patterns.
Because exhaustiveness affects reachability, and reachability affects type inference, we can't do correct type inference before we have the compilation environment.

I propose that we *require* the compilation environment to always be provided at the beginning of compilation.
We can do modular compilation, but as we have decided before, we then have to ensure that the compilation environments used by all the modules are consistent (agree on any value that was actually read). Maybe by just embedding any used environment key/values into the kernel/dill file, and checking them when linking.

You cannot reuse most compilation artifacts across different compilation environments, but the way the language is currently specified, that's pretty much impossible anyway, all we do is to delay actual compilation.

The analyzer may run in a setting with no compilation environment (aka, an *empty* environment), but that's something code should be ready for anyway, by providing a useful `defaultValue` to the `fromEnvironment` constructors.
In any case, the analyzer will be no worse off than today.

@johnniwinther

Contributor guide

Open the contributing guide

Research direction

Start by reading the issue's discussion of fromEnvironment constructors, constant evaluation, patterns, type inference, and modular compilation. Determine the specification and compiler-design decisions needed to require an environment at compilation start, including how module environments remain consistent. Done means the proposal has an agreed, documented design rather than merely a delayed compilation workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.