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

Support constant byte array literal #40624

Closed

Conversation

ushirask
Copy link
Contributor

@ushirask ushirask commented Jun 7, 2023

Purpose

Support constant byte array literal

Fixes #40536
Fixes #37423

Approach

Describe how you are implementing the solutions along with the design details.

Samples

Provide high-level details about the samples related to this feature.

Remarks

List any other known issues, related PRs, TODO items, or any other notes related to the PR.

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

@ushirask ushirask mentioned this pull request Jun 7, 2023
13 tasks
@ushirask ushirask changed the base branch from symbolEnter-revamp to master June 7, 2023 16:23
@ushirask ushirask changed the base branch from master to symbolEnter-revamp June 7, 2023 16:24
@ushirask ushirask changed the base branch from symbolEnter-revamp to master June 14, 2023 05:05
@ushirask
Copy link
Contributor Author

Depends on #40621

@ushirask ushirask marked this pull request as ready for review June 22, 2023 04:19
@prakanth97
Copy link
Contributor

prakanth97 commented Jun 22, 2023

Getting runtime error for below case

import ballerina/io;

const X = base16 `aabb`;

public function main() {
    byte[] b = X;
    io:println(b[0]);
}
[2023-06-22 10:48:32,362] SEVERE {b7a.log.crash} - class java.lang.Integer cannot be cast to class java.lang.Byte (java.lang.Integer and java.lang.Byte are in module java.base of loader 'bootstrap') 
java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.Byte (java.lang.Integer and java.lang.Byte are in module java.base of loader 'bootstrap')
        at io.ballerina.runtime.internal.values.TupleValueImpl.getByte(TupleValueImpl.java:282)
        at prakanth.test.0.test.main(test.bal:7)
        at prakanth.test.0.$_init.$moduleExecute(test)
        at prakanth.test.0.$_init.$lambda$$moduleExecute$(test)
        at io.ballerina.runtime.internal.scheduling.SchedulerItem.execute(SchedulerItem.java:54)
        at io.ballerina.runtime.internal.scheduling.Scheduler.run(Scheduler.java:306)
        at io.ballerina.runtime.internal.scheduling.Scheduler.runSafely(Scheduler.java:273)
        at java.base/java.lang.Thread.run(Thread.java:829)

seems same behaviour for the list constant as well.

Below works fine.

const byte[] X = base16 `aabb`; 

public function main() {
    byte b = X[0];
    io:println(b);
}

@@ -259,6 +255,12 @@ public void visit(BLangLiteral literalExpr, AnalyzerData data) {
return;
}

if (literalType.tag == TypeTags.BYTE_ARRAY) {
BLangListConstructorExpr listConstructorExpr = rewriteByteArrayLiteral(literalExpr);
data.resultType = checkListConstructorCompatibility(literalType, listConstructorExpr, data);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should do the type checking with the expected type as well here. Due to that we did log error for below case.

const string[] X = base16 `aabb`;

This should be error according to the spec

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can simply check compatibility with expected type here, will change to that.

const byte[] data2 = base64 `ABCD pqrs`;

const byte[3] data3 = base16 `55 EE 66`;
const byte[6] data4 = base64 `ABCD pqrs`;
Copy link
Contributor

@prakanth97 prakanth97 Jun 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we adds tests containing inferred array, unions and singletons as expected type as well?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and subtype of int as expected type

@codecov
Copy link

codecov bot commented Jun 22, 2023

Codecov Report

Patch coverage: 93.69% and project coverage change: +0.16 🎉

Comparison is base (acc9116) 76.45% compared to head (7fa743c) 76.61%.

❗ Current head 7fa743c differs from pull request most recent head 1555bc9. Consider uploading reports for the commit 1555bc9 to get more accurate results

Additional details and impacted files
@@             Coverage Diff              @@
##             master   #40624      +/-   ##
============================================
+ Coverage     76.45%   76.61%   +0.16%     
- Complexity    55279    55321      +42     
============================================
  Files          3386     3377       -9     
  Lines        208244   208073     -171     
  Branches      26954    26941      -13     
============================================
+ Hits         159209   159422     +213     
+ Misses        40259    39867     -392     
- Partials       8776     8784       +8     
Impacted Files Coverage Δ
...alang/compiler/semantics/analyzer/TypeChecker.java 91.73% <ø> (ø)
...llerinalang/compiler/util/ImmutableTypeCloner.java 88.44% <ø> (ø)
...lang/compiler/semantics/analyzer/CodeAnalyzer.java 87.58% <33.33%> (-0.08%) ⬇️
.../compiler/semantics/analyzer/ConstantAnalyzer.java 87.69% <85.71%> (-0.24%) ⬇️
...llerinalang/compiler/semantics/analyzer/Types.java 81.72% <85.71%> (+<0.01%) ⬆️
...mpiler/semantics/analyzer/ConstantTypeChecker.java 65.27% <95.55%> (+22.12%) ⬆️
...g/wso2/ballerinalang/compiler/desugar/Desugar.java 90.34% <100.00%> (+0.01%) ⬆️
...allerinalang/compiler/parser/BLangNodeBuilder.java 94.39% <100.00%> (+<0.01%) ⬆️
...rinalang/compiler/semantics/model/SymbolTable.java 96.40% <100.00%> (-0.14%) ⬇️
...lerinalang/compiler/util/TypeDefBuilderHelper.java 90.42% <100.00%> (+1.46%) ⬆️

... and 28 files with indirect coverage changes

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

@@ -527,7 +549,7 @@ private BType checkMappingConstructorCompatibility(BType expType, BLangRecordLit
switch (possibleType.tag) {
case TypeTags.MAP:
return validateSpecifiedFieldsAndGetType(mappingConstructor, possibleType, data);
case TypeTags.RECORD:
case TypeTags.RECORD :
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
case TypeTags.RECORD :
case TypeTags.RECORD:

@@ -2715,7 +2715,7 @@ private HashSet<String> getFieldNames(List<RecordLiteralNode.RecordField> specif
return fieldNames;
}

private String getKeyValueFieldName(BLangRecordKeyValueField field) {
public String getKeyValueFieldName(BLangRecordKeyValueField field) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use default-access-level instead of making it public.

Suggested change
public String getKeyValueFieldName(BLangRecordKeyValueField field) {
String getKeyValueFieldName(BLangRecordKeyValueField field) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Address here d97530c

Comment on lines 1022 to 1023
&& targetTag == TypeTags.ARRAY
&& isArrayTypesAssignable((BArrayType) source, target, unresolvedTypes);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can write this two lines in-line

@@ -0,0 +1,71 @@
/*
* Copyright (c) 2023, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Copyright (c) 2023, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
* Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com).

Let's fix the other few places as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed here d97530c

Copy link
Contributor

@prakanth97 prakanth97 Jun 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to review this #40621 before this PR

@ushirask ushirask closed this Jul 11, 2023
@ushirask ushirask force-pushed the byte-array-literal branch 2 times, most recently from 1555bc9 to d08328e Compare July 11, 2023 03:25
@ushirask
Copy link
Contributor Author

Moved to #40979

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Improvement]: Support constant byte array literal CCE when byte-array-literal occurs within a const-expr
3 participants