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

lang: add explicit type widening (As) #122

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

zerbina
Copy link
Collaborator

@zerbina zerbina commented Mar 6, 2025

Summary

Add the (As x typ) expression to the source language, which is used
to explicitly widen the type of a value. x having a type that is
equal to typ is not an error.

Details

As takes an expression and a type, fitting the former to the latter
where possible, reporting an error if not. At the moment, it's main
use is constructing values of UnionTy type, but in the future, it'll
also be useful for guiding type inference.

Implementation

  • lower As directly into a type conversion through fitExpr
  • fix error correction for fitting errors producing illformed Exprs,
    by keeping or discarding the trailing expression depending on the
    target type

Tests are added for As, and existing tests for union types now use
As instead of helper procedure for creating union values, greatly
simplifying the affected tests.


Notes For Reviewers

zerbina added 4 commits March 6, 2025 22:08
It's a simple value operator for making sure an expression is of the
expected type, up-converting if possible, but without coercing. This is
currently most useful for creating constructing unions.
The implementation is straightforward: making the sure the target type
is valid, fitting the source expression to the target type, and
discarding the type qualifiers is enough.

Error correction (the lack thereof) for fitting errors is also fixed,
so that no ill-formed `Expr`s are created.
`As` is used in-place of a custom procedure, significantly simplifying
the tests.
@zerbina zerbina added the enhancement New feature or request label Mar 6, 2025
Copy link
Contributor

@saem saem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some questions, otherwise LGTM

e = c.exprToIL(t, a)
typ = c.expectNot(c.evalType(t, b), tkVoid)
expr = inline(c.fitExpr(e, typ), stmts)
result = typ + {} # lvalue-ness and mutability are discarded
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still learning to read the source, but it seems that lvalue-ness and mutability dropping isn't there, I'm guessing it can't be represented?

I guess the drop is required right now because if we're actually widening the bits then a new location would be required?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still learning to read the source, but it seems that lvalue-ness and mutability dropping isn't there, I'm guessing it can't be represented?

It's there, albeit not easy to spot. exprToIL (the enclosing procedure) returns an ExprType, which is a type + qualifiers (ExprFlags, currently a bunch of flags), where the qualifiers track both mutability and lvalue-ness.

If the qualifiers were to be kept, the result assignment would look like this:

result = typ + e.attribs # e.attribs are the source expression's qualifiers 

I guess the drop is required right now because if we're actually widening the bits then a new location would be required?

Yep, that's the idea. If no widening takes place, then no new location is needed, so the lvalue-ness and mutability could be kept, but I decided against it because:

  • to the human reader, a large context window is potentially required to know what a specific As does (the same problem exists in NimSkull with lvalue conversions)
  • the lvalue-ness/mutability changing depending on the target type could become an issue once/if per-procedure type inference is introduced

This doesn't have to (nor should it have to) be the final decision, however. If it turns out that the current rules are problematic (for whatever reason), we should alter them.

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

Successfully merging this pull request may close these issues.

2 participants