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

Check types of LHS and RHS in assignment "to identifier" #187

Merged
merged 1 commit into from
Mar 23, 2024

Conversation

jlapeyre
Copy link
Collaborator

Before there was no checking that the types of the LHS and RHS were valid in assignment, with the exception of checking constness.

This commit introduces some checking in the case that the LHS is represented by an identifier (i.e. not an indexed identifier) Checking if the LHS is an indexed identifier, for example b[1] = measure q; remains to be implemented.

  • If types have same base array type, but dimensions differ, an IncompatibleDimensionError is recorded. This is tested, for example, with
bit[3] b;
qubit[2] q;
b = measure q;
  • If the RHS is an integer literal and the LHS is unsigned, then the sign of the literal is examined and the RHS is wrapped in Cast if the sign is positive. Otherwise CastError.

  • If the type of the RHS can be cast to that of the left, the RHS is wrapped in Cast.

  • Otherwise IncompatibleTypes is recorded.

Several tests of these new behaviors are included.

A few details of things added:

  • Fix erroneous Type of integer literal. Before we had UInt even though in the data we stored the sign of the literal. The type is now Int.
  • Introduce semantic errors: IncompatibleDimensionError and CastError. The latter is for trying to cast a negative integer literal to an unsigned type. We could perhaps to do better. Perhaps just make this an IncompatibleTypes error instead.

Before there was no checking that the types of the LHS and RHS were valid in assignment, with the
exception of checking constness.

This commit introduces some checking in the case that the LHS is represented by an identifier
(i.e. not an indexed identifier) Checking if the LHS is an indexed identifier, for example `b[1] =
measure q;` remains to be implemented

* If types have same base array type, but dimensions differ, an `IncompatibleDimensionError` is recorded. This
  is tested, for example, with
```
bit[3] b;
qubit[2] q;
b = measure q;
```

* If the RHS is an integer literal and the LHS is unsigned, then the sign of the literal is examined and
  the RHS is wrapped in `Cast` if the sign is positive. Otherwise `CastError`.

* If the type of the RHS can be cast to that of the left, the RHS is wrapped in `Cast`.

* Otherwise `IncompatibleTypes` is recorded.

Several tests of these new behaviors are included.

A few details of things added:

* Fix erroneous `Type` of integer literal. Before we had UInt even though in the data we stored the sign
  of the literal. The type is now `Int`.
* Introduce semantic errors: `IncompatibleDimensionError` and `CastError`. The latter is for trying to
  cast a negative integer literal to an unsigned type. We could perhaps to do better. Perhaps just make
  this an `IncompatibleTypes` error instead.
@jlapeyre jlapeyre merged commit 3396068 into Qiskit:main Mar 23, 2024
7 checks passed
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.

1 participant