Skip to content

ASR: Remove implicit casting in BinOp #1525

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

Merged
merged 3 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions integration_tests/test_numpy_02.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ def test_sign():

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

c2: c64
Expand All @@ -360,7 +360,7 @@ def test_real():

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

c2: c64
Expand Down
45 changes: 15 additions & 30 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1951,22 +1951,22 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
throw SemanticAbort();
}

if (ASRUtils::is_integer(*dest_type)) {
left_type = ASRUtils::expr_type(left);
right_type = ASRUtils::expr_type(right);
if( !ASRUtils::check_equal_type(left_type, right_type) ) {
std::string ltype = ASRUtils::type_to_str_python(left_type);
std::string rtype = ASRUtils::type_to_str_python(right_type);
diag.add(diag::Diagnostic(
"Type mismatch in binary operator; the types must be compatible",
diag::Level::Error, diag::Stage::Semantic, {
diag::Label("type mismatch (" + ltype + " and " + rtype + ")",
{left->base.loc, right->base.loc})
})
);
throw SemanticAbort();
}

ASR::ttype_t* left_type = ASRUtils::expr_type(left);
ASR::ttype_t* right_type = ASRUtils::expr_type(right);
if( !ASRUtils::check_equal_type(left_type, right_type) ) {
std::string ltype = ASRUtils::type_to_str_python(left_type);
std::string rtype = ASRUtils::type_to_str_python(right_type);
diag.add(diag::Diagnostic(
"Type mismatch in binary operator; the types must be compatible",
diag::Level::Error, diag::Stage::Semantic, {
diag::Label("type mismatch (" + ltype + " and " + rtype + ")",
{left->base.loc, right->base.loc})
})
);
throw SemanticAbort();
}
if (ASRUtils::is_integer(*dest_type)) {
ASR::dimension_t *m_dims_left = nullptr, *m_dims_right = nullptr;
int n_dims_left = ASRUtils::extract_dimensions_from_ttype(left_type, m_dims_left);
int n_dims_right = ASRUtils::extract_dimensions_from_ttype(right_type, m_dims_right);
Expand Down Expand Up @@ -2027,21 +2027,6 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
throw SemanticError("Unsupported binary operation on floats: '" + ASRUtils::binop_to_str_python(op) + "'", loc);
}

cast_helper(left, right, false);
ASR::ttype_t* left_type = ASRUtils::expr_type(left);
ASR::ttype_t* right_type = ASRUtils::expr_type(right);
if( !ASRUtils::check_equal_type(left_type, right_type) ) {
std::string ltype = ASRUtils::type_to_str_python(ASRUtils::expr_type(left));
std::string rtype = ASRUtils::type_to_str_python(ASRUtils::expr_type(right));
diag.add(diag::Diagnostic(
"Type mismatch in binary operator; the types must be compatible",
diag::Level::Error, diag::Stage::Semantic, {
diag::Label("type mismatch (" + ltype + " and " + rtype + ")",
{left->base.loc, right->base.loc})
})
);
throw SemanticAbort();
}
ASR::dimension_t *m_dims_left = nullptr, *m_dims_right = nullptr;
int n_dims_left = ASRUtils::extract_dimensions_from_ttype(left_type, m_dims_left);
int n_dims_right = ASRUtils::extract_dimensions_from_ttype(right_type, m_dims_right);
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/lpython_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,12 @@ def complex(x: f64) -> c64:
@interface
@overload
def complex(x: i32) -> c32:
return c32(x) + c32(0)*1j
return c32(x) + c32(0)*c32(1j)

@interface
@overload
def complex(x: f32) -> c32:
return c32(x) + c32(0)*1j
return c32(x) + c32(0)*c32(1j)

@interface
@overload
Expand Down
8 changes: 8 additions & 0 deletions tests/errors/test_binop3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from ltypes import c32

def test_issue_1524():
y: c32
y = complex(5)+100
print(y)

test_issue_1524()
13 changes: 13 additions & 0 deletions tests/reference/asr-test_binop3-a67201d.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "asr-test_binop3-a67201d",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/errors/test_binop3.py",
"infile_hash": "3e25a0b0167fc2e06dd5797a4f1827108fd2e73d9f61cd95532cdc42",
"outfile": null,
"outfile_hash": null,
"stdout": null,
"stdout_hash": null,
"stderr": "asr-test_binop3-a67201d.stderr",
"stderr_hash": "ff683b1bc0695903f2d2ea7bbd1963346fcb5f84bbfd10a4da0e27d7",
"returncode": 2
}
5 changes: 5 additions & 0 deletions tests/reference/asr-test_binop3-a67201d.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
semantic error: Type mismatch in binary operator; the types must be compatible
--> tests/errors/test_binop3.py:5:9
|
5 | y = complex(5)+100
| ^^^^^^^^^^ ^^^ type mismatch (c32 and i32)
4 changes: 4 additions & 0 deletions tests/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,10 @@ asr = true
filename = "errors/test_binop2.py"
asr = true

[[test]]
filename = "errors/test_binop3.py"
asr = true

[[test]]
filename = "errors/test_dict1.py"
asr = true
Expand Down