Skip to content
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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

DanilaFe
Copy link
Contributor

@DanilaFe DanilaFe commented Feb 7, 2025

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 invoking init(), 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, each type and param 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 from Resolver.cpp (for its use in new resolution) and share it with the new initializer call builder and existing logic in call-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

  • dyno tests

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: 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.
Copy link
Member

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?

Copy link
Contributor Author

@DanilaFe DanilaFe Feb 8, 2025

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.

Copy link
Contributor Author

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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants