-
-
Notifications
You must be signed in to change notification settings - Fork 719
[DO NOT MERGE] POC: Add language-level tagged unions (enum union) and pattern matching (switch expressions) #23744
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
Open
MetaLang
wants to merge
5
commits into
dlang:master
Choose a base branch
from
MetaLang:sumtype-poc
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+5,442
−19
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e174f31
Add enum union support and tests
MetaLang 1fbcec6
Implement enum union pattern matching
MetaLang 3203b3e
Fix enum union discard pattern example
MetaLang 1e7bbe2
Add enum union POC and pattern matching
MetaLang cc20c6d
Extend enum union pattern matching POC
MetaLang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,7 @@ import dmd.func; | |
| import dmd.funcsem; | ||
| import dmd.globals; | ||
| import dmd.hdrgen; | ||
| import dmd.identifier; | ||
| import dmd.location; | ||
| import dmd.impcnvtab; | ||
| import dmd.importc; | ||
|
|
@@ -113,6 +114,76 @@ Expression implicitCastTo(Expression e, Scope* sc, Type t) | |
| { | ||
| auto eSink = global.errorSink; | ||
|
|
||
| if (auto ts = t.toBasetype().isTypeStruct()) | ||
| { | ||
| if (auto eu = ts.sym.isEnumUnionDeclaration()) | ||
| { | ||
| size_t matchIndex = size_t.max; | ||
| foreach (i, variant; eu.variants) | ||
| { | ||
| auto payloadType = variant.payloadType && variant.payloadType.fields.length | ||
| ? variant.payloadType.fields[0].type : variant.payload.length ? variant.payload[0] : null; | ||
| // Function pointers/delegates may only differ from each other by | ||
| // attributes (e.g. inferred `pure nothrow @nogc @safe` on a lambda), | ||
| // never by parameter/return types, so allowing attribute-widening | ||
| // implicit conversion here cannot introduce cross-variant ambiguity | ||
| // the way e.g. `int` -> `double` numeric widening would. Only loosen | ||
| // when the source is ITSELF already callable: other types (e.g. | ||
| // `noreturn*`) can have unrelated implicit conversions to callable | ||
| // types that must not be treated the same way. | ||
| const requiredMatch = payloadType && payloadType.isFunction_Delegate_PtrToFunction() && | ||
| e.type && e.type.isFunction_Delegate_PtrToFunction() | ||
| ? MATCH.convert : MATCH.exact; | ||
| const isNullUnitVariant = e.type && e.type.toBasetype().ty == Tnull && | ||
| variant.payload.length == 0 && variant.ident == Identifier.idPool("None"); | ||
| if ((!variant.ident && variant.payload.length == 1 && payloadType && | ||
| e.implicitConvTo(payloadType) >= requiredMatch) || isNullUnitVariant) | ||
| { | ||
| if (matchIndex != size_t.max) | ||
| { | ||
| eSink.error(e.loc, "`%s` is ambiguous between variants `%s` and `%s` of enum union `%s`", | ||
| e.toErrMsg(), eu.variants[matchIndex].payloadType && eu.variants[matchIndex].payloadType.fields.length | ||
| ? eu.variants[matchIndex].payloadType.fields[0].type.toErrMsg() | ||
| : "None", | ||
| payloadType ? payloadType.toErrMsg() : "None", | ||
| eu.toPrettyChars()); | ||
| return ErrorExp.get(); | ||
| } | ||
| matchIndex = i; | ||
| } | ||
| } | ||
| if (matchIndex != size_t.max) | ||
| { | ||
| auto variant = eu.variants[matchIndex]; | ||
| // Build `(tmp; tmp.__tag = i; tmp.<payloadVar>.<field> = e; tmp)` | ||
| // rather than a positional struct literal: the payload union | ||
| // promotes one field per variant, so a 2-element literal would | ||
| // target the wrong (first) union member when there is more | ||
| // than one bare-type variant. | ||
| auto tmp = new VarDeclaration(e.loc, t, Identifier.generateId("__enumConv"), null); | ||
| tmp.storage_class |= STC.temp; | ||
| Expression result = new DeclarationExp(e.loc, tmp); | ||
| Expression tmpVar = new VarExp(e.loc, tmp); | ||
|
|
||
| auto tagExp = new DotVarExp(e.loc, tmpVar, eu.tagVar); | ||
| Expression tagAssign = new AssignExp(e.loc, tagExp, new IntegerExp(e.loc, matchIndex, Type.tuns8)); | ||
| result = new CommaExp(e.loc, result, tagAssign); | ||
|
|
||
| if (variant.payloadType && variant.payloadType.fields.length) | ||
| { | ||
| auto field = variant.payloadType.fields[0]; | ||
| auto payloadAccess = new DotVarExp(e.loc, | ||
| new DotVarExp(e.loc, tmpVar, variant.payloadVar), field); | ||
| Expression payloadAssign = new AssignExp(e.loc, payloadAccess, e); | ||
| result = new CommaExp(e.loc, result, payloadAssign); | ||
| } | ||
|
|
||
| result = new CommaExp(e.loc, result, tmpVar); | ||
| return result.expressionSemantic(sc); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Expression visit(Expression e) | ||
| { | ||
| //printf("Expression.implicitCastTo(%s of type %s) => %s\n", e.toChars(), e.type.toChars(), t.toChars()); | ||
|
|
@@ -1561,6 +1632,26 @@ MATCH implicitConvTo(Expression e, Type t) | |
| */ | ||
| MATCH implicitConvTo(Type from, Type to) | ||
| { | ||
| if (auto ts = to.toBasetype().isTypeStruct()) | ||
| { | ||
| if (auto eu = ts.sym.isEnumUnionDeclaration()) | ||
| { | ||
| foreach (variant; eu.variants) | ||
| { | ||
| auto payloadType = variant.payloadType && variant.payloadType.fields.length | ||
| ? variant.payloadType.fields[0].type : variant.payload.length ? variant.payload[0] : null; | ||
| const requiredMatch = payloadType && payloadType.isFunction_Delegate_PtrToFunction() && | ||
| from.isFunction_Delegate_PtrToFunction() | ||
| ? MATCH.convert : MATCH.exact; | ||
| const isNullUnitVariant = from.toBasetype().ty == Tnull && | ||
| variant.payload.length == 0 && variant.ident == Identifier.idPool("None"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Try to avoid comparing identifiers by string, chuck None into the table and do a pointer comparison instead. |
||
| if ((!variant.ident && variant.payload.length == 1 && payloadType && | ||
| from.implicitConvTo(payloadType) >= requiredMatch) || isNullUnitVariant) | ||
| return MATCH.convert; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| MATCH visitType(Type from) | ||
| { | ||
| //printf("Type::implicitConvTo(this=%p, to=%p)\n", this, to); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
That doesn't look like the right place for it.