Skip to content

Commit

Permalink
Show Type Mismatch Error for Division (lcompilers#1686)
Browse files Browse the repository at this point in the history
* Fixed tests
* Added tests for type mismatch

Co-authored-by: Gagandeep Singh <gdp.1807@gmail.com>
  • Loading branch information
gptsarthak and czgdp1807 committed Apr 11, 2023
1 parent ade107a commit a1504ac
Show file tree
Hide file tree
Showing 20 changed files with 86 additions and 46 deletions.
8 changes: 4 additions & 4 deletions integration_tests/elemental_05.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def verifynd(array: f64[:, :, :, :], result: f64[:, :, :, :], size1: i32, size2:
result1d: f64[12800] = reshape(result, shape)

for i in range(size):
assert abs((sinh(array1d[i]) + 2.0)/2 - result1d[i]) <= eps
assert abs((sinh(array1d[i]) + 2.0)/2.0 - result1d[i]) <= eps


def elemental_sinh():
Expand All @@ -41,7 +41,7 @@ def elemental_sinh():
for j in range(10):
for k in range(16):
for l in range(2):
arraynd[i, j, k, l] = float(i + 2*j + 3*k + 4*k)/size
arraynd[i, j, k, l] = float(i + 2*j + 3*k + 4*k)/float(size)

sinhnd = (sinh(arraynd) + 2.0)/2.0

Expand All @@ -63,7 +63,7 @@ def elemental_cosh():

for i in range(20):
for j in range(10):
array2d[i, j] = (i + 2*j)/200.0
array2d[i, j] = float(i + 2*j)/200.0

cosh2d = cosh(5.0 + (array2d))**2.0
verify2d(array2d, cosh2d, 20, 10)
Expand All @@ -77,7 +77,7 @@ def elemental_cosh_():

for i in range(20):
for j in range(10):
array2d[i, j] = (i + 2*j)/200.0
array2d[i, j] = float(i + 2*j)/200.0

cosh2d = cosh(5.0 + (array2d))**2.0
verify2d(array2d, cosh2d, 20, 10)
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/elemental_06.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def elemental_arctan():

for i in range(201):
array1d[i] = f32(i + 1)
array1d_rec[i] = f32(1.0/(i+1))
array1d_rec[i] = f32(1.0/f64(i+1))
arctan1d = arctan(array1d) + arctan(array1d_rec)
for i in range(201):
assert abs(arctan1d[i] - f32(f64(pi) / 2.0)) <= eps
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/elemental_07.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def elemental_tanh():
for j in range(8):
for k in range(4):
for l in range(2):
arraynd[i, j, k, l] = float(i + 2*j + 3*k + 4*k)/size
arraynd[i, j, k, l] = float(i + 2*j + 3*k + 4*k)/float(size)

tanhnd = (tanh(sin(arraynd)) + 2.0)/2.0

Expand Down
4 changes: 2 additions & 2 deletions integration_tests/elemental_08.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def verifynd(array: f64[:, :, :, :], result: f64[:, :, :, :], size1: i32, size2:
result1d: f64[1024] = reshape(result, shape)

for i in range(size):
assert abs(( exp(array1d[i]) + exp(array1d[i] / 3) ) / 2 - result1d[i]) <= eps
assert abs(( exp(array1d[i]) + exp(array1d[i] / 3.0) ) / float(2) - result1d[i]) <= eps

def elemental_exp():
i: i32; j: i32; k: i32; l: i32; size: i32;
Expand All @@ -40,7 +40,7 @@ def elemental_exp():
for j in range(8):
for k in range(4):
for l in range(2):
arraynd[i, j, k, l] = float( f64(i) / 8.0 + f64(j) / 4.0 + f64(k) / 2.0 + f64(l) ) / size
arraynd[i, j, k, l] = float( f64(i) / 8.0 + f64(j) / 4.0 + f64(k) / 2.0 + f64(l) ) / float(size)

expnd = (exp(arraynd) + exp(arraynd / 3.0)) / 2.0

Expand Down
2 changes: 1 addition & 1 deletion integration_tests/generics_list_01.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def add_float(x: f64, y: f64) -> f64:
return x + y

def div_float(x: f64, k: i32) -> f64:
return x / k
return x / f64(k)

def empty_string(x: str) -> str:
return ""
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/test_c_interop_01.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def _lfortran_bgt64(i: i64, j: i64) -> i32:
def test_c_callbacks():
pi: f64 = 3.141592653589793238462643383279502884197
assert abs(_lfortran_dsin(pi) - 0.0) < 1e-12
assert abs(_lfortran_dsin(pi/2) - 1.0) < 1e-12
assert abs(_lfortran_dsin(pi/2.0) - 1.0) < 1e-12
assert abs(_lfortran_ssin(f32(pi)) - f32(0.0)) < f32(1e-6)
assert abs(_lfortran_ssin(f32(pi/2.0)) - f32(1.0)) < f32(1e-6)

Expand Down
4 changes: 2 additions & 2 deletions integration_tests/test_dict_01.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ def test_dict():
rollnumber2cpi[0] = 1.1

for i in range(1000, 1000 + size):
rollnumber2cpi[i] = float(i/100.0 + 5.0)
rollnumber2cpi[i] = float(i)/100.0 + 5.0

for i in range(1000 + size - 1, 1001, -1):
assert abs(rollnumber2cpi[i] - i/100.0 - 5.0) <= 1e-12
assert abs(rollnumber2cpi[i] - f64(i)/100.0 - 5.0) <= 1e-12

assert abs(rollnumber2cpi[0] - 1.1) <= 1e-12
assert len(rollnumber2cpi) == 1001
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/test_dict_05.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def test_dict():
end: i32 = 501

for i in range(start, end):
rollnumber2cpi[i] = float(i/100.0 + 5.0)
rollnumber2cpi[i] = float(i)/100.0 + 5.0

# for i in range(end - 1, start + 1, -1):
# assert abs(rollnumber2cpi[i] - i/100.0 - 5.0) <= 1e-12
Expand Down
4 changes: 2 additions & 2 deletions integration_tests/test_dict_06.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def fill_rollnumber2cpi(size: i32) -> dict[i32, f64]:

rollnumber2cpi[0] = 1.1
for i in range(1000, 1000 + size):
rollnumber2cpi[i] = float(i/100.0 + 5.0)
rollnumber2cpi[i] = float(i)/100.0 + 5.0

return rollnumber2cpi

Expand All @@ -16,7 +16,7 @@ def test_dict():
rollnumber2cpi: dict[i32, f64] = fill_rollnumber2cpi(size)

for i in range(1000 + size - 1, 1001, -1):
assert abs(rollnumber2cpi[i] - i/100.0 - 5.0) <= 1e-12
assert abs(rollnumber2cpi[i] - f64(i)/100.0 - 5.0) <= 1e-12

assert abs(rollnumber2cpi[0] - 1.1) <= 1e-12
assert len(rollnumber2cpi) == 201
Expand Down
4 changes: 2 additions & 2 deletions integration_tests/test_dict_09.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ def fill_rollnumber2cpi(size: i32) -> dict[i32, f64]:

rollnumber2cpi[0] = 1.1
for i in range(1000, 1000 + size):
rollnumber2cpi[i] = float(i/100.0 + 5.0)
rollnumber2cpi[i] = float(i)/100.0 + 5.0

return rollnumber2cpi

def test_assertion(rollnumber2cpi: dict[i32, f64], size: i32):
i: i32
for i in range(1000 + size - 1, 1001, -1):
assert abs(rollnumber2cpi[i] - i/100.0 - 5.0) <= 1e-12
assert abs(rollnumber2cpi[i] - f64(i)/100.0 - 5.0) <= 1e-12

assert abs(rollnumber2cpi[0] - 1.1) <= 1e-12
assert len(rollnumber2cpi) == 201
Expand Down
12 changes: 6 additions & 6 deletions integration_tests/test_math_02.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
def test_trig():
eps: f64 = 1e-12
assert abs(sin(0.0) - 0.0) < eps
assert abs(sin(pi/2) - 1.0) < eps
assert abs(sin(pi/2.0) - 1.0) < eps
assert abs(cos(0.0) - 1.0) < eps
assert abs(cos(pi/2) - 0.0) < eps
assert abs(cos(pi/2.0) - 0.0) < eps
assert abs(tan(0.0) - 0.0) < eps
assert abs(tan(pi/4) - 1.0) < eps
assert abs(asin(1.0) - pi/2) < eps
assert abs(tan(pi/4.0) - 1.0) < eps
assert abs(asin(1.0) - pi/2.0) < eps
assert abs(acos(1.0) - 0.0) < eps
assert abs(atan(1.0) - pi/4) < eps
assert abs(atan2(1.0, 1.0) - pi/4) < eps
assert abs(atan(1.0) - pi/4.0) < eps
assert abs(atan2(1.0, 1.0) - pi/4.0) < eps

def test_sqrt():
eps: f64 = 1e-12
Expand Down
12 changes: 12 additions & 0 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1745,6 +1745,18 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
);
throw SemanticAbort();
}
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();
}
// Floor div operation in python using (`//`)
if (floordiv) {
bool both_int = (ASRUtils::is_integer(*left_type) && ASRUtils::is_integer(*right_type));
Expand Down
30 changes: 15 additions & 15 deletions src/runtime/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def mean(x: list[i32]) -> f64:

for i in range(k):
sum += float(x[i])
return sum/k
return sum/f64(k)


@overload
Expand All @@ -33,7 +33,7 @@ def mean(x: list[i64]) -> f64:
for i in range(k):
sum += float(x[i])

return sum/k
return sum/f64(k)


@overload
Expand All @@ -50,7 +50,7 @@ def mean(x: list[f32]) -> f64:

for i in range(k):
sum += float(x[i])
return sum/k
return sum/f64(k)


@overload
Expand All @@ -67,7 +67,7 @@ def mean(x: list[f64]) -> f64:

for i in range(k):
sum += x[i]
return sum/k
return sum/f64(k)


@overload
Expand Down Expand Up @@ -177,7 +177,7 @@ def harmonic_mean(x: list[i32]) -> f64:
raise Exception("Harmonic mean does not support negative values")
sum += 1 / x[i]

return float(k/sum)
return f64(k)/sum

@overload
def harmonic_mean(x: list[i64]) -> f64:
Expand All @@ -196,8 +196,8 @@ def harmonic_mean(x: list[i64]) -> f64:
return 0.0
if x[i] < i64(0):
raise Exception("Harmonic mean does not support negative values")
sum += 1 / x[i]
return k/sum
sum += i64(1) / x[i]
return f64(k)/sum

@overload
def harmonic_mean(x: list[f64]) -> f64:
Expand All @@ -216,9 +216,9 @@ def harmonic_mean(x: list[f64]) -> f64:
return 0.0
if x[i] < 0.0:
raise Exception("Harmonic mean does not support negative values")
sum += 1 / x[i]
sum += 1.0 / x[i]

return k / sum
return f64(k) / sum


# TODO: Use generics to support other types.
Expand Down Expand Up @@ -283,7 +283,7 @@ def variance(x: list[f64]) -> f64:
i: i32
for i in range(n):
num += (x[i] - xmean)**2.0
return num / (n-1)
return num / f64(n-1)

@overload
def variance(x: list[i32]) -> f64:
Expand All @@ -301,7 +301,7 @@ def variance(x: list[i32]) -> f64:
i: i32
for i in range(n):
num += (f64(x[i]) - xmean)**2.0
return num / (n-1)
return num / f64(n-1)


@overload
Expand Down Expand Up @@ -335,7 +335,7 @@ def pvariance(x: list[f64]) -> f64:
i: i32
for i in range(n):
num += (x[i] - xmean)**2.0
return num / n
return num / f64(n)

@overload
def pvariance(x: list[i32]) -> f64:
Expand All @@ -353,7 +353,7 @@ def pvariance(x: list[i32]) -> f64:
i: i32
for i in range(n):
num += (f64(x[i]) - xmean)**2.0
return num / n
return num / f64(n)


@overload
Expand Down Expand Up @@ -450,7 +450,7 @@ def covariance(x: list[i32], y: list[i32]) -> f64:
i: i32
for i in range(n):
num += (f64(x[i]) - xmean) * (f64(y[i]) - ymean)
return num / (n-1)
return num / f64(n-1)

@overload
def covariance(x: list[f64], y: list[f64]) -> f64:
Expand All @@ -468,7 +468,7 @@ def covariance(x: list[f64], y: list[f64]) -> f64:
i: i32
for i in range(n):
num += (x[i] - xmean) * (y[i] - ymean)
return num / (n-1)
return num / f64(n-1)

@overload
def linear_regression(x: list[i32], y: list[i32]) -> tuple[f64, f64]:
Expand Down
6 changes: 6 additions & 0 deletions tests/errors/test_type_mismatch_01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from lpython import i32, f64

def test():
a : i32 = 10
b : f64 = 20.0
a /= b
2 changes: 1 addition & 1 deletion tests/reference/asr-generics_list_01-39c4044.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"basename": "asr-generics_list_01-39c4044",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/../integration_tests/generics_list_01.py",
"infile_hash": "d5101f5832d47b86a95540b87deb0b36092004160b41af2a6ffb5ee0",
"infile_hash": "fee10ba2a43151e515695ea4179e42231e1fd80d7c75637747511144",
"outfile": null,
"outfile_hash": null,
"stdout": "asr-generics_list_01-39c4044.stdout",
Expand Down
4 changes: 2 additions & 2 deletions tests/reference/asr-test_c_interop_01-e374f43.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"basename": "asr-test_c_interop_01-e374f43",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/../integration_tests/test_c_interop_01.py",
"infile_hash": "8dd5d5b22dd518496006bdf56651d6892012102d8a0f354e05fc94d7",
"infile_hash": "f5363d49163fefe382a94462e7c305a7938ddcc44c4595f8a0c5bc3f",
"outfile": null,
"outfile_hash": null,
"stdout": "asr-test_c_interop_01-e374f43.stdout",
"stdout_hash": "f0f14cea7739e56630b1c9a041e549f8e8e18d05e69dc36fceb5e74b",
"stdout_hash": "9d34b037680950ab931fd76b05db390f3c09fe38e14996dd9b79218b",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
Loading

0 comments on commit a1504ac

Please sign in to comment.