-
Notifications
You must be signed in to change notification settings - Fork 424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dyno: implement "is default initializable" #26679
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: Danila Fedorin <daniel.fedorin@hpe.com>
Signed-off-by: Danila Fedorin <daniel.fedorin@hpe.com>
Signed-off-by: Anna Rift <anna.rift@hpe.com>
Signed-off-by: Anna Rift <anna.rift@hpe.com>
Signed-off-by: Anna Rift <anna.rift@hpe.com>
Signed-off-by: Danila Fedorin <daniel.fedorin@hpe.com>
Signed-off-by: Danila Fedorin <daniel.fedorin@hpe.com>
Signed-off-by: Danila Fedorin <daniel.fedorin@hpe.com>
Signed-off-by: Danila Fedorin <daniel.fedorin@hpe.com>
} | ||
|
||
// note: production disallows default-init for generic fields like `var x;`, | ||
// but why? Seems like this is an implementation detail. Allow it in Dyno. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't that make sense? What value do you default init var x
to?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if it's instantiated with a type, I'd expect it to default-init to x
's type's default value. Production does this, too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In other words, I don't see any philosophical reason why the following program shouldn't work:
record R {
var x;
}
var x: R(int);
writeln(x);
This PR implements the
type has default value
primitive. For the most part, I attempted to mimic the approach used by production. This for the most part boils down to checking fields then invokinginit()
, except for some special cases like array types (which are default-initializable if their elements are) and classes (which are always default-initializable if they are nilable, and never otherwise).There's some trickiness involved: "default initialization" sometimes involves invoking
init
with some actuals. Specifically, in the case of generic types, eachtype
andparam
substitution gets turned into an actual to resolve the corresponding initializer. This enables resolving initializers that only require types / params that are already provided as substitutions for the type. To implement this, extract existing logic for adding substitutions as actuals fromResolver.cpp
(for its use innew
resolution) and share it with the new initializer call builder and existing logic incall-init-deinit.cpp
While this PR generally follows the production compiler's behavior, following a comparison with #26678 I noticed some dd behaviors. Most notably, default generic types such as
numeric
are treated as having a default initializer. these cases likely don't occur in practice because they are wrapped in function calls (and thus, only things that instantiate these types are actually fed to the primitive). Thus, I chose to match Anna's approach and mark those as generic.Testing