core: improve binop and cmpop error messages #439

Open
lyken wants to merge 2 commits from improve-error-432 into master
Collaborator

NOTE: This PR depends on #437 (core: improve function call errors).

Fixes #436 (Improve type error messages for operator functions) (originally from #432 (type inferencer crash on artiq_sinara_tester)).

For #436 (Improve type error messages for operator functions), in particular, the error message is now:

self.core.delay(100*ms)

# Unsupported operand type(s) for *: 'int32' and 'float' (right operand should have type N) at /home/lyken/artiq/artiq/frontend/artiq_sinara_tester.py:469:36
# 
# Notes:
#     N ∈ {int32, ndarray[int32, ndarray_ndims]}
#     ndarray_ndims ∈ {uint32}

For ill-typed binary operations:

def run() -> int32:
    ok = 1.0 * 3
    return 0

# 1 error(s) occurred during top level analysis.
# =========== ERROR 1/1 ============
# Unsupported operand type(s) for +: 'float' and 'int32' (right operand should have type N) at src/test_operator_error.py:2:14
# 
# Notes:
#     N ∈ {float, ndarray[float, ndarray_ndims]}
#     ndarray_ndims ∈ {uint64}
# ==================================
# thread 'main' panicked at nac3standalone/src/main.rs:354:9:
# top level analysis failed

For ill-typed comparison operations:

def run() -> int32:
    ok = 1.0 <= 3
    return 0

# 1 error(s) occurred during top level analysis.
# =========== ERROR 1/1 ============
# '<=' not supported between instances of 'float' and 'int32' (right operand should have type N) at src/test_operator_error.py:2:14
# 
# Notes:
#     N ∈ {float, ndarray[float, ndarray_ndims]}
#     ndarray_ndims ∈ {uint64}
# ==================================
# thread 'main' panicked at nac3standalone/src/main.rs:354:9:
# top level analysis failed
NOTE: This PR depends on https://git.m-labs.hk/M-Labs/nac3/pulls/437 (core: improve function call errors). Fixes https://git.m-labs.hk/M-Labs/nac3/issues/436 (Improve type error messages for operator functions) (originally from https://git.m-labs.hk/M-Labs/nac3/issues/432 (type inferencer crash on artiq_sinara_tester)). For https://git.m-labs.hk/M-Labs/nac3/issues/436 (Improve type error messages for operator functions), in particular, the error message is now: ```python self.core.delay(100*ms) # Unsupported operand type(s) for *: 'int32' and 'float' (right operand should have type N) at /home/lyken/artiq/artiq/frontend/artiq_sinara_tester.py:469:36 # # Notes: # N ∈ {int32, ndarray[int32, ndarray_ndims]} # ndarray_ndims ∈ {uint32} ``` For ill-typed binary operations: ```python def run() -> int32: ok = 1.0 * 3 return 0 # 1 error(s) occurred during top level analysis. # =========== ERROR 1/1 ============ # Unsupported operand type(s) for +: 'float' and 'int32' (right operand should have type N) at src/test_operator_error.py:2:14 # # Notes: # N ∈ {float, ndarray[float, ndarray_ndims]} # ndarray_ndims ∈ {uint64} # ================================== # thread 'main' panicked at nac3standalone/src/main.rs:354:9: # top level analysis failed ``` For ill-typed comparison operations: ```python def run() -> int32: ok = 1.0 <= 3 return 0 # 1 error(s) occurred during top level analysis. # =========== ERROR 1/1 ============ # '<=' not supported between instances of 'float' and 'int32' (right operand should have type N) at src/test_operator_error.py:2:14 # # Notes: # N ∈ {float, ndarray[float, ndarray_ndims]} # ndarray_ndims ∈ {uint64} # ================================== # thread 'main' panicked at nac3standalone/src/main.rs:354:9: # top level analysis failed ```
lyken added 4 commits 2024-06-27 13:49:16 +08:00
lyken requested review from sb10q 2024-06-27 13:49:25 +08:00
lyken requested review from derppening 2024-06-27 13:49:25 +08:00
lyken added a new dependency 2024-06-27 13:58:50 +08:00
lyken removed a dependency 2024-06-27 13:58:56 +08:00
lyken added a new dependency 2024-06-27 13:59:03 +08:00
lyken force-pushed improve-error-432 from 8dfd7f2a7d to 889bb20844 2024-06-27 14:08:29 +08:00 Compare
Author
Collaborator

Force-pushed to rebase onto master since #437 has been merged.

Force-pushed to rebase onto master since https://git.m-labs.hk/M-Labs/nac3/pulls/437 has been merged.
lyken force-pushed improve-error-432 from 889bb20844 to 3087f77ed8 2024-06-27 14:55:51 +08:00 Compare
lyken added 1 commit 2024-06-27 15:00:59 +08:00
derppening requested changes 2024-06-27 15:06:47 +08:00
derppening left a comment
Collaborator

I'd prefer it to be called "invalid operand type(s) float and int32 for operator +"

I'd prefer it to be called "invalid operand type(s) `float` and `int32` for operator `+`"
@ -73,1 +49,3 @@
_ => None,
impl OpInfo {
#[must_use]
pub fn from_binop(op: Operator, variant: BinOpVariant) -> Self {
Collaborator

Why not just implement these using the From trait?

Why not just implement these using the `From` trait?
Author
Collaborator

It is possible for Cmpop and Unaryop, but not for Operator (binary operator) since it takes an extra argument, although I could create:

struct BinaryOp {
  base_op: Operator,
  variant: BinOpVariant,
}
It is possible for `Cmpop` and `Unaryop`, but not for `Operator` (binary operator) since it takes an extra argument, although I could create: ```rust struct BinaryOp { base_op: Operator, variant: BinOpVariant, } ```
Collaborator

Just implement From<(Operator, BinOpVariant)> for OpInfo then.

Just implement `From<(Operator, BinOpVariant)> for OpInfo` then.
Author
Collaborator

Also I don't think From trait is meant to be used like that - it's purpose is for type conversions. Perhaps I will create a trait HasOpInfo.

Also I don't think `From` trait is meant to be used like that - it's purpose is for type conversions. Perhaps I will create a `trait HasOpInfo`.
@ -75,1 +77,4 @@
/// Extra details about how a [`Call`] was written by the user.
#[derive(Debug, Clone)]
pub enum CallInfo {
Collaborator

Call this struct OperatorInfo.

Call this struct `OperatorInfo`.
Author
Collaborator

CallInfo was intended to be general for any kind of function call. But to be honest I cannot think of anything other than normal function calls and operator calls (including is and is not and its friends). I will rename it.

`CallInfo` was intended to be general for any kind of function call. But to be honest I cannot think of anything other than normal function calls and operator calls (including `is` and `is not` and its friends). I will rename it.
@ -80,6 +108,7 @@ pub struct Call {
pub ret: Type,
pub fun: RefCell<Option<Type>>,
pub loc: Option<Location>,
pub info: CallInfo,
Collaborator

Make this Option<OperatorInfo>.

Make this `Option<OperatorInfo>`.
Author
Collaborator

I'd prefer it to be called invalid operand type(s) float and int32 for operator +

The current error message follows Python's:

>>> 3.0 + "a"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'float' and 'str'

Should we still make the change?

> I'd prefer it to be called `invalid operand type(s) float and int32 for operator +` The current error message follows Python's: ``` >>> 3.0 + "a" Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'float' and 'str' ``` Should we still make the change?
Collaborator

I'd prefer it to be called invalid operand type(s) float and int32 for operator +

The current error message follows Python's:

>>> 3.0 + "a"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'float' and 'str'

Should we still make the change?

Use Python's error message format then.

> > I'd prefer it to be called `invalid operand type(s) float and int32 for operator +` > > The current error message follows Python's: > ``` > >>> 3.0 + "a" > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > TypeError: unsupported operand type(s) for +: 'float' and 'str' > ``` > > Should we still make the change? Use Python's error message format then.
lyken force-pushed improve-error-432 from d0ea69b7ae to c57e0936d5 2024-06-27 16:04:14 +08:00 Compare
Author
Collaborator

Revised

Revised
lyken requested review from derppening 2024-06-27 16:05:51 +08:00
lyken added a new dependency 2024-06-27 17:12:41 +08:00
This pull request can be merged automatically.
You are not authorized to merge this pull request.
You can also view command line instructions.

Step 1:

From your project repository, check out a new branch and test the changes.
git checkout -b improve-error-432 master
git pull origin improve-error-432

Step 2:

Merge the changes and update on Gitea.
git checkout master
git merge --no-ff improve-error-432
git push origin master
Sign in to join this conversation.
No reviewers
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Reference: M-Labs/nac3#439
No description provided.