Skip to content

Commit 5e9e730

Browse files
authored
Merge pull request #1525 from Smit-create/i-1524
ASR: Remove implicit casting in BinOp
2 parents 6672141 + 86d7fa9 commit 5e9e730

File tree

7 files changed

+49
-34
lines changed

7 files changed

+49
-34
lines changed

integration_tests/test_numpy_02.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ def test_sign():
338338

339339
def test_real():
340340
c: c32
341-
c = c32(4) + 3j
341+
c = c32(4) + c32(3j)
342342
assert abs(f64(real(c)) - 4.0) < eps
343343

344344
c2: c64
@@ -360,7 +360,7 @@ def test_real():
360360

361361
def test_imag():
362362
c: c32
363-
c = c32(4) + 3j
363+
c = c32(4) + c32(3j)
364364
assert abs(f64(imag(c)) - 3.0) < eps
365365

366366
c2: c64

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1951,22 +1951,22 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
19511951
throw SemanticAbort();
19521952
}
19531953

1954-
if (ASRUtils::is_integer(*dest_type)) {
1954+
left_type = ASRUtils::expr_type(left);
1955+
right_type = ASRUtils::expr_type(right);
1956+
if( !ASRUtils::check_equal_type(left_type, right_type) ) {
1957+
std::string ltype = ASRUtils::type_to_str_python(left_type);
1958+
std::string rtype = ASRUtils::type_to_str_python(right_type);
1959+
diag.add(diag::Diagnostic(
1960+
"Type mismatch in binary operator; the types must be compatible",
1961+
diag::Level::Error, diag::Stage::Semantic, {
1962+
diag::Label("type mismatch (" + ltype + " and " + rtype + ")",
1963+
{left->base.loc, right->base.loc})
1964+
})
1965+
);
1966+
throw SemanticAbort();
1967+
}
19551968

1956-
ASR::ttype_t* left_type = ASRUtils::expr_type(left);
1957-
ASR::ttype_t* right_type = ASRUtils::expr_type(right);
1958-
if( !ASRUtils::check_equal_type(left_type, right_type) ) {
1959-
std::string ltype = ASRUtils::type_to_str_python(left_type);
1960-
std::string rtype = ASRUtils::type_to_str_python(right_type);
1961-
diag.add(diag::Diagnostic(
1962-
"Type mismatch in binary operator; the types must be compatible",
1963-
diag::Level::Error, diag::Stage::Semantic, {
1964-
diag::Label("type mismatch (" + ltype + " and " + rtype + ")",
1965-
{left->base.loc, right->base.loc})
1966-
})
1967-
);
1968-
throw SemanticAbort();
1969-
}
1969+
if (ASRUtils::is_integer(*dest_type)) {
19701970
ASR::dimension_t *m_dims_left = nullptr, *m_dims_right = nullptr;
19711971
int n_dims_left = ASRUtils::extract_dimensions_from_ttype(left_type, m_dims_left);
19721972
int n_dims_right = ASRUtils::extract_dimensions_from_ttype(right_type, m_dims_right);
@@ -2027,21 +2027,6 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
20272027
throw SemanticError("Unsupported binary operation on floats: '" + ASRUtils::binop_to_str_python(op) + "'", loc);
20282028
}
20292029

2030-
cast_helper(left, right, false);
2031-
ASR::ttype_t* left_type = ASRUtils::expr_type(left);
2032-
ASR::ttype_t* right_type = ASRUtils::expr_type(right);
2033-
if( !ASRUtils::check_equal_type(left_type, right_type) ) {
2034-
std::string ltype = ASRUtils::type_to_str_python(ASRUtils::expr_type(left));
2035-
std::string rtype = ASRUtils::type_to_str_python(ASRUtils::expr_type(right));
2036-
diag.add(diag::Diagnostic(
2037-
"Type mismatch in binary operator; the types must be compatible",
2038-
diag::Level::Error, diag::Stage::Semantic, {
2039-
diag::Label("type mismatch (" + ltype + " and " + rtype + ")",
2040-
{left->base.loc, right->base.loc})
2041-
})
2042-
);
2043-
throw SemanticAbort();
2044-
}
20452030
ASR::dimension_t *m_dims_left = nullptr, *m_dims_right = nullptr;
20462031
int n_dims_left = ASRUtils::extract_dimensions_from_ttype(left_type, m_dims_left);
20472032
int n_dims_right = ASRUtils::extract_dimensions_from_ttype(right_type, m_dims_right);

src/runtime/lpython_builtin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,12 +315,12 @@ def complex(x: f64) -> c64:
315315
@interface
316316
@overload
317317
def complex(x: i32) -> c32:
318-
return c32(x) + c32(0)*1j
318+
return c32(x) + c32(0)*c32(1j)
319319

320320
@interface
321321
@overload
322322
def complex(x: f32) -> c32:
323-
return c32(x) + c32(0)*1j
323+
return c32(x) + c32(0)*c32(1j)
324324

325325
@interface
326326
@overload

tests/errors/test_binop3.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from ltypes import c32
2+
3+
def test_issue_1524():
4+
y: c32
5+
y = complex(5)+100
6+
print(y)
7+
8+
test_issue_1524()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"basename": "asr-test_binop3-a67201d",
3+
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
4+
"infile": "tests/errors/test_binop3.py",
5+
"infile_hash": "3e25a0b0167fc2e06dd5797a4f1827108fd2e73d9f61cd95532cdc42",
6+
"outfile": null,
7+
"outfile_hash": null,
8+
"stdout": null,
9+
"stdout_hash": null,
10+
"stderr": "asr-test_binop3-a67201d.stderr",
11+
"stderr_hash": "ff683b1bc0695903f2d2ea7bbd1963346fcb5f84bbfd10a4da0e27d7",
12+
"returncode": 2
13+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
semantic error: Type mismatch in binary operator; the types must be compatible
2+
--> tests/errors/test_binop3.py:5:9
3+
|
4+
5 | y = complex(5)+100
5+
| ^^^^^^^^^^ ^^^ type mismatch (c32 and i32)

tests/tests.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,10 @@ asr = true
756756
filename = "errors/test_binop2.py"
757757
asr = true
758758

759+
[[test]]
760+
filename = "errors/test_binop3.py"
761+
asr = true
762+
759763
[[test]]
760764
filename = "errors/test_dict1.py"
761765
asr = true

0 commit comments

Comments
 (0)