core: reimplement assignment type inference and codegen + other minor changes & refactor #483
No reviewers
Labels
No Milestone
No Assignees
2 Participants
Notifications
Due Date
No due date set.
Blocks
#411 Implement subscript-assignment for NDArrays
M-Labs/nac3
Reference: M-Labs/nac3#483
Loading…
Reference in New Issue
No description provided.
Delete Branch "setitem-assign"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Fixes #482; Fixes #459 partially by allowing LHS list patterns; Required by #411.
Details of this PR are as follows:
1. Treating item assignments differently.
Currently, an assignment statement
<LHS> = <RHS>
is type-checked like so:<LHS>
<RHS>
<LHS>
and<RHS>
However, item assignments should be treated differently; Consider the following when supporting #411:
This PR allows these item assignments to be programmed.
Moreover, for an item assignment expression
object[key] = value
, the current type inferencer actually infers the type ofobject[key]
, but this is not necessary, and it actually doesn't make sense. This is becauseobject[key] = value
is the same asobject.__setitem__(key, value)
. This PR fixes this.2. Supporting
'[' ... ']'
targets and starred targets.'[' ... ']'
and'(' ... ')'
are apparently the same thing. They are different syntaxes to define a so-called"target_list"
in https://docs.python.org/3/reference/simple_stmts.html#assignment-statements.Starred targets are now supported, but the caveat is that their values would be tuples:
Nested item assignments are also supported, and assignment works recursively like Python:
More examples in
nac3standalone/demo/src/assignment.py
.3. Minor bug fix.
This PR fixes the following error as a by-product:
nac3core
currently reports:With this PR:
This is probably due to a mistake in the
Inferencer
on when LHS patterns should be registered intodefined_identifiers
. This PR fixes it.Other notes in this PR
Right now in this PR, for a target list assignment, its corresponding RHS type must be a tuple. Meaning:
The reason for why only tuple types are allowed is because it is easy to implement and tuples have very rich typechecker types. For lists, the implementation might be very complicated with very specialized code mixed into the type inferencer.
Other comparisons between the current
nac3core
andnac3core
with this PR.Nits for now, 34661ffa7f needs more time for review.
@ -18,2 +18,3 @@
clippy::too_many_lines,
clippy::wildcard_imports
clippy::wildcard_imports,
clippy::iter_nth_zero
I don't see why this is necessary. Just use
.next()
as one would.iter_type_vars()
sometimes usesnth(1)
andnth(2)
, etc. Isn't it weird to suddenly use.next()
instead ofnth(0)
? But this is not too important. I can revert that if you want.Clippy's justification is here.
I wouldn't fight Clippy about this.
Ok, let's not do this.
@ -12,6 +11,7 @@ use super::{
RecordField, RecordKey, Type, TypeEnum, TypeVar, Unifier, VarMap,
},
};
use crate::typecheck::typedef::Mapping;
Fold this into the
use
block in the next line.dc1c4aff0e
to070655669b
core: distinguish between setitem and getitem in assignments + other minor changes & refactorto WIP: core: distinguish between setitem and getitem in assignments + other minor changes & refactorMarking it as WIP, I forgot to account for how annoying asterisks on LHS could be.
The current implementation is not flexible enough.
070655669b
to813705819e
WIP: core: distinguish between setitem and getitem in assignments + other minor changes & refactorto WIP: core: reimplement assignment type inference and codegen + other minor changes & refactorWIP: core: reimplement assignment type inference and codegen + other minor changes & refactorto core: reimplement assignment type inference and codegen + other minor changes & refactorRevised PR. The PR message is updated.
813705819e
to4b927fa62f
Amended branch to not use
clippy::iter_nth_zero
.No other comments as long as unit tests and standalone tests pass.