Require const generic literals to be wrapped in Literal #367

Merged
sb10q merged 7 commits from enhance/typing-literal into master 2023-12-16 18:40:48 +08:00
Collaborator

More closely matches the syntax in Python.

This PR also implements the behavior of Literal where Literal[a, b] would accept a value of either a or b.

More closely matches the syntax in Python. This PR also implements the behavior of `Literal` where `Literal[a, b]` would accept a value of either `a` or `b`.
derppening added 7 commits 2023-12-15 12:28:45 +08:00
875d08e9bb core: Add PrimitiveStore into Unifier
This will be used during unification between a const generic variable
and a `Literal`.
f742d0c32d core: Refactor generic constants to `Literal`
Better matches the syntax of `typing.Literal`.
bf830f216e core: Deduplicate values in `Literal`
Matches the behavior with `typing.Literal`.
2722a7a17f core: Do not keep unification result for function arguments
For some reason, when unifying a function call parameter with an
argument, subsequent calls to the same function will only accept the
type of the substituted argument.

This affect snippets like:

```
def make1() -> C[Literal[1]]:
    return ...

def make2() -> C[Literal[2]]:
    return ...

def consume(instance: C[Literal[1, 2]]):
    pass

consume(make1())
consume(make2())
```

The last statement will result in a compiler error, as the parameter of
consume is replaced with C[Literal[1]].

We fix this by getting a snapshot before performing unification, and
restoring the snapshot after unification succeeds.
derppening self-assigned this 2023-12-15 12:28:50 +08:00
derppening requested review from sb10q 2023-12-15 12:28:53 +08:00
sb10q merged commit 8886964776 into master 2023-12-16 18:40:48 +08:00
sb10q deleted branch enhance/typing-literal 2023-12-16 18:40:50 +08:00
Sign in to join this conversation.
No reviewers
No Milestone
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: M-Labs/nac3#367
There is no content yet.