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

Semantic errors in infer_ops.rs #123

Open
adsharma opened this issue Apr 26, 2021 · 4 comments
Open

Semantic errors in infer_ops.rs #123

adsharma opened this issue Apr 26, 2021 · 4 comments
Labels

Comments

@adsharma
Copy link
Collaborator

Description in

8b90aba

@jayvdb
Copy link
Member

jayvdb commented Apr 26, 2021

To clarify, infer_ops output is compared against the Python, however very little of the logic in infer_ops is checked by the runnable/printed code, so the results can be different for each language, and the test rig wont notice. The fix for this issue needs to be expanded logic in infer_ops.py to verify all of the logic within it.

@adsharma
Copy link
Collaborator Author

@adsharma
Copy link
Collaborator Author

This is further complicated by the fact that Rust and C use different rules for integer conversions:

#include <stdio.h>
#include <stdint.h>

int16_t add1(int8_t c1, int8_t c2) {
    return c1 + c2;
}

int main()
{
	int8_t c1 = 127;
	int8_t c2 = 100;

        printf("%d\n", add1(c1, c2));  // 227
}
fn add1(c1: i8, c2: i8) -> i16 {
    return (c1 + c2) as i16;
}

fn main()
{
	let c1: i8 = 127;
	let c2: i8 = 100;

        println!("{}", add1(c1, c2));  // -29
}

@adsharma
Copy link
Collaborator Author

 'cpp' PASSED
 'dart' PASSED
 'go' FAILED
 'julia' FAILED
 'kotlin' FAILED
 'nim' FAILED
 'rust' FAILED

One possibility is to generate code in the other 5 languages to have semantics similar to C. Widen to int, perform the operation and then truncate. But the choice of "int" in the C standard seems arbitrary. We could widen to the next integer type with a greater rank that prevents overflow or underflow.

For this case:

def add3(c1: i32, c2: i32):
    return c1 + c2
int64_t add3(int32_t c1, int32_t c2) {
    return (int64_t) c1 + (int64_t) c2;
}

I was completely unaware of the "integer rank" concept in the C language. Glad that the concept matches what I came up with for py2many (RUST_WIDTH_RANK)

adsharma added a commit that referenced this issue May 17, 2021
Tested to work only for widening ops. Need similar code for
subtraction and negative numbers etc. Once we have it working
we should write similar code for other languages that need it.

The following cases pass on rust:

```
    assert add1(127  - 1, 1) == 127
    assert add2(32767, 1) == 32768
    assert add3(2147483647, 1) == 2147483648
```

Related to: #123
adsharma added a commit that referenced this issue May 17, 2021
Tested to work only for widening ops. Need similar code for
subtraction and negative numbers etc. Once we have it working
we should write similar code for other languages that need it.

The following cases pass on rust:

```
    assert add1(127  - 1, 1) == 127
    assert add2(32767, 1) == 32768
    assert add3(2147483647, 1) == 2147483648
```

Related to: #123
adsharma added a commit that referenced this issue May 17, 2021
Tested to work only for widening ops. Need similar code for
subtraction and negative numbers etc. Once we have it working
we should write similar code for other languages that need it.

The following cases pass on rust:

```
    assert add1(127  - 1, 1) == 127
    assert add2(32767, 1) == 32768
    assert add3(2147483647, 1) == 2147483648
```

Related to: #123
adsharma added a commit that referenced this issue May 17, 2021
Tested to work only for widening ops. Need similar code for
subtraction and negative numbers etc. Once we have it working
we should write similar code for other languages that need it.

The following cases pass on rust:

```
    assert add1(127, 1) == 128
    assert add2(32767, 1) == 32768
    assert add3(2147483647, 1) == 2147483648
```

Related to: #123
MiguelMarcelino pushed a commit to MiguelMarcelino/py2many that referenced this issue May 23, 2022
Tested to work only for widening ops. Need similar code for
subtraction and negative numbers etc. Once we have it working
we should write similar code for other languages that need it.

The following cases pass on rust:

```
    assert add1(127, 1) == 128
    assert add2(32767, 1) == 32768
    assert add3(2147483647, 1) == 2147483648
```

Related to: py2many#123
@jayvdb jayvdb added the Rust label May 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants