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

[Master] Fix incorrect results for panic in module-const-declaration with arithmetic operators #37726

Merged
merged 19 commits into from
Nov 24, 2022

Conversation

prakanth97
Copy link
Contributor

@prakanth97 prakanth97 commented Sep 13, 2022

Purpose

Fixes #36648
Fixes #37711

Approach

Give compile-time errors for panic scenarios instead of giving wrong output.

Samples

const NUM1 = -1;
const int NUM2 = -9223372036854775807 - 1;
const int NUM3 = 0;
const int ANS1 = NUM2 / NUM1;          // error
const int ANS2 = NUM2 / NUM3;          // error

const int ANS3 = NUM2 * NUM1;          // error
const int ANS4 = NUM2 * 5;             // error

const int ANS5 = NUM2 % NUM3;          // error
const int ANS6 = -NUM2;                // error

const int ANS7 = NUM2 + NUM1;          // error
const int ANS8 = NUM2 - 1;             // error

Now, these test cases will give compile-time error

Remarks

#13944

Check List

  • Read the Contributing Guide
  • Updated Change Log
  • Checked Tooling Support (#)
  • Added necessary tests
    • Unit Tests
    • Spec Conformance Tests
    • Integration Tests
    • Ballerina By Example Tests
  • Increased Test Coverage
  • Added necessary documentation
    • API documentation
    • Module documentation in Module.md files
    • Ballerina By Examples

@prakanth97 prakanth97 added the Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. label Sep 13, 2022
@prakanth97
Copy link
Contributor Author

While we are logging the java errors for constant-expr

,

we stick with the java like error for

const NUM1 = -1;
const int NUM2 = -9223372036854775807 - 1;
const int ANS1 = NUM2 / NUM1;          // error

which is invalid constant expression, reason 'long overflow'. Is that correct or do we need to have custom compile-time error codes?

@SandaruJayawardana
Copy link
Contributor

we stick with the java like error for
const NUM1 = -1;
const int NUM2 = -9223372036854775807 - 1;
const int ANS1 = NUM2 / NUM1; // error
which is invalid constant expression, reason 'long overflow'. Is that correct or do we need to have custom compile-time error codes?

IMO, we have to change the error messages since Ballerina has different data types compared to Java. For example, long data type in above error msg (long overflow).

@prakanth97
Copy link
Contributor Author

prakanth97 commented Sep 14, 2022

we stick with the java like error for
const NUM1 = -1;
const int NUM2 = -9223372036854775807 - 1;
const int ANS1 = NUM2 / NUM1; // error
which is invalid constant expression, reason 'long overflow'. Is that correct or do we need to have custom compile-time error codes?

IMO, we have to change the error messages since Ballerina has different data types compared to Java. For example, long data type in above error msg (long overflow).

Updated 60870f0

@codecov
Copy link

codecov bot commented Sep 14, 2022

Codecov Report

Base: 77.69% // Head: 76.28% // Decreases project coverage by -1.40% ⚠️

Coverage data is based on head (0318dec) compared to base (f62dd91).
Patch coverage: 47.82% of modified lines in pull request are covered.

Additional details and impacted files
@@             Coverage Diff              @@
##             master   #37726      +/-   ##
============================================
- Coverage     77.69%   76.28%   -1.41%     
+ Complexity    67866    53265   -14601     
============================================
  Files          3543     3406     -137     
  Lines        256170   200803   -55367     
  Branches      39947    25886   -14061     
============================================
- Hits         199020   153190   -45830     
+ Misses        47668    39025    -8643     
+ Partials       9482     8588     -894     
Impacted Files Coverage Δ
.../java/io/ballerina/projects/CodeActionManager.java 78.78% <ø> (-2.80%) ⬇️
.../compiler/semantics/analyzer/DataflowAnalyzer.java 88.61% <ø> (-3.20%) ⬇️
...alang/langserver/BallerinaTextDocumentService.java 56.08% <0.00%> (-6.65%) ⬇️
...rinalang/langserver/BallerinaWorkspaceService.java 67.30% <ø> (-9.54%) ⬇️
...a/org/ballerinalang/langserver/LSClientLogger.java 57.53% <0.00%> (-4.24%) ⬇️
...rg/ballerinalang/langserver/util/LSClientUtil.java 9.09% <0.00%> (-50.49%) ⬇️
...ava/io/ballerina/projectdesign/ComponentModel.java 0.00% <ø> (ø)
...ballerina/projectdesign/ComponentModelBuilder.java 0.00% <0.00%> (ø)
...rc/main/java/io/ballerina/projectdesign/Utils.java 0.00% <ø> (ø)
...erina/projectdesign/generators/GeneratorUtils.java 0.00% <0.00%> (ø)
... and 708 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@SandaruJayawardana
Copy link
Contributor

We need to handle overflow scenarios of decimal also.
e.x. Following code crashes the compiler.

const decimal X = 1e100000000000000050 * 1e10000000000300050;

@prakanth97
Copy link
Contributor Author

prakanth97 commented Oct 3, 2022

We need to handle overflow scenarios of decimal also. e.x. Following code crashes the compiler.

const decimal X = 1e100000000000000050 * 1e10000000000300050;

Fixes in #37823

@pcnfernando
Copy link
Member

@prakanth97 can you please resolve the conflict

@pcnfernando
Copy link
Member

@prakanth97 please check on the build

@prakanth97
Copy link
Contributor Author

prakanth97 commented Oct 31, 2022

@prakanth97 please check on the build

Build failure is not due to my fix, Now it is resolved in the master

@prakanth97 prakanth97 requested a review from lasinicl November 21, 2022 05:14
@pcnfernando pcnfernando added this to the 2201.4.0 milestone Nov 22, 2022
@SandaruJayawardana
Copy link
Contributor

I am getting 0 for following example. Is it correct? @prakanth97

const decimal d5 = 2E-5800d * 2E-5800d * 2E-5800d  * 1E-614d  * 2E-5800d;

@prakanth97
Copy link
Contributor Author

I am getting 0 for following example. Is it correct? @prakanth97

const decimal d5 = 2E-5800d * 2E-5800d * 2E-5800d  * 1E-614d  * 2E-5800d;

Added check based on this comment https://github.com/ballerina-platform/ballerina-lang/pull/37726/files#r1019873516

It is correct according to the spec https://ballerina.io/spec/lang/master/#section_5.2.4.2

@pcnfernando
Copy link
Member

@prakanth97 can you check on the pipeline failures

@pcnfernando pcnfernando merged commit b23b120 into ballerina-platform:master Nov 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times.
Projects
No open projects
Status: Addressing Review Suggestions
4 participants