ballerina-lang icon indicating copy to clipboard operation
ballerina-lang copied to clipboard

[Bug]: Record literal expression error evaluation create infinite loop

Open HindujaB opened this issue 1 year ago • 2 comments

Description

$subject

Steps to Reproduce

The compiler type-checker creates unnecessary anonymous record types when evaluating record literals and logging an error. https://github.com/ballerina-platform/ballerina-lang/blob/89f1a3982bb9aa0d51c2bc982dbc6a5c79b80c23/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeChecker.java#L1907

The log error call creates new anonymous record types in a loop. This results in around 12000 new anonType_ type definitions which cause file too large and method too large errors in code generation.

Please refer to the code provided at https://github.com/wso2-enterprise/internal-support-ballerina/issues/672

Affected Version(s)

2201.8.x and onwards

OS, DB, other environment details and versions

No response

Related area

-> Compilation

Related issue(s) (optional)

https://github.com/wso2-enterprise/internal-support-ballerina/issues/672

Suggested label(s) (optional)

No response

Suggested assignee(s) (optional)

No response

HindujaB avatar May 10 '24 05:05 HindujaB

Removing the getInferredTupleType method call in the logging error fails in the following cases. https://scans.gradle.com/s/zpxzt5hn2u2hg/tests/overview?outcome=FAILED

HindujaB avatar May 10 '24 06:05 HindujaB

import ballerinax/health.fhir.r4;

public type Extension int|record {|string url;|};

r4:Extension[] k = [
    {
        "url": "tribalAffiliation",
        "valueCodeableConcept": {
            "coding": [
                {
                    "system": "http://terminology.hl7.org/CodeSystem/v3-TribalEntityUS",
                    "code": "187",
                    "display": "Paiute-Shoshone Tribe of the Fallon Reservation and Colony, Nevada"
                }
            ],
            "text": "Shoshone"
        }
    }
];

public function main() {
};

When considering the BIR of the above program, it contains 53 anonymous types, which is incorrect. This issue occur from unnecessary cloning of nodes in the type checker. Each cloned node contains an anonymous record, leading to the generation of a type definition for each record.

BIR of the above program

================ Emitting Module ================
module wso2/test v 0.1.0;

import ballerinax/health.fhir.r4 v 4.3.4;
import ballerinai/observe v 0.0.0;


public $anonType$Extension$_0 record {
    string url;
}
{
};

$anonType$_0 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_1 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_2 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_3 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_4 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_5 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_6 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_7 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_8 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_9 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_10 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_11 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_12 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_13 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_14 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_15 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_16 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_17 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_18 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_19 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_20 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_21 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_22 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_23 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_24 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_25 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_26 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_27 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_28 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_29 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_30 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_31 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_32 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_33 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_34 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_35 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_36 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_37 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_38 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_39 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_40 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_41 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_42 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_43 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_44 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_45 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_46 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_47 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_48 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_49 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_50 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_51 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_52 record {
    string system;
    string code;
    string display;
}
{
};

$anonType$_53 record {
    string system;
    string code;
    string display;
}
{
};

public Extension int|$anonType$Extension$_0
{
};



$annotation_data map<any>;
k typeRefDesc<>[];


public .<init> function() -> error{map<ballerina/lang.value:0.0.0:Cloneable>}|() {
    %0(RETURN) error|();
    %1(TEMP) typeDesc<any|error>;
    %4(TEMP) int;
    %6(TEMP) ballerinax/health.fhir.r4:4.3.4:CodeableConceptExtension;
    %7(TEMP) string;
    %8(TEMP) string;
    %9(TEMP) string;
    %10(TEMP) typeDesc<any|error>;
    %11(TEMP) ballerinax/health.fhir.r4:4.3.4:CodeableConcept;
    %12(TEMP) string;
    %13(TEMP) ballerinax/health.fhir.r4:4.3.4:Coding[];
    %14(TEMP) int;
    %15(TEMP) typeDesc<any|error>;
    %16(TEMP) ballerinax/health.fhir.r4:4.3.4:Coding;
    %17(TEMP) string;
    %18(TEMP) typeRefDesc<>|();
    %19(TEMP) string;
    %21(TEMP) typeRefDesc<>|();
    %22(TEMP) string;
    %24(TEMP) string|();
    %25(TEMP) string;
    %27(TEMP) string|();

    bb0 {
        %1 = newType map<any>;
        $annotation_data = NewMap %1{};
        %4 = ConstLoad -1;
        %1 = newType ballerinax/health.fhir.r4:4.3.4:CodeableConceptExtension;
        %7 = ConstLoad url;
        %8 = ConstLoad tribalAffiliation;
        %9 = ConstLoad valueCodeableConcept;
        %10 = newType ballerinax/health.fhir.r4:4.3.4:CodeableConcept;
        %12 = ConstLoad coding;
        %14 = ConstLoad -1;
        %15 = newType ballerinax/health.fhir.r4:4.3.4:Coding;
        %17 = ConstLoad system;
        %19 = ConstLoad http://terminology.hl7.org/CodeSystem/v3-TribalEntityUS;
        %18 = <typeRefDesc<>|()> %19;
        %19 = ConstLoad code;
        %22 = ConstLoad 187;
        %21 = <typeRefDesc<>|()> %22;
        %22 = ConstLoad display;
        %25 = ConstLoad Paiute-Shoshone Tribe of the Fallon Reservation and Colony, Nevada;
        %24 = <string|()> %25;
        %16 = NewMap %15{%17:%18,%19:%21,%22:%24};
        %13 = newArray ballerinax/health.fhir.r4:4.3.4:Coding[][%14]{%16};
        %25 = ConstLoad text;
        %17 = ConstLoad Shoshone;
        %27 = <string|()> %17;
        %11 = NewMap %10{%12:%13,%25:%27};
        %6 = NewMap %1{%7:%8,%9:%11};
        k = newArray typeRefDesc<>[][%4]{%6};
        %0 = ConstLoad 0;
        GOTO bb1;
    }
    bb1 {
        return;
    }


}

public .<start> function() -> error|() {
    %0(RETURN) error|();

    bb0 {
        %0 = ConstLoad 0;
        GOTO bb1;
    }
    bb1 {
        return;
    }


}

public .<stop> function() -> error|() {
    %0(RETURN) error|();

    bb0 {
        %0 = ConstLoad 0;
        GOTO bb1;
    }
    bb1 {
        return;
    }


}

public main function() -> () {
    %0(RETURN) ();

    bb0 {
        %0 = ConstLoad 0;
        GOTO bb1;
    }
    bb1 {
        return;
    }


}

chiranSachintha avatar May 13 '24 07:05 chiranSachintha

This issue is NOT closed with a proper Reason/ label. Make sure to add proper reason label before closing. Please add or leave a comment with the proper reason label now.

      - Reason/EngineeringMistake - The issue occurred due to a mistake made in the past.
      - Reason/Regression - The issue has introduced a regression.
      - Reason/MultipleComponentInteraction - Issue occured due to interactions in multiple components.
      - Reason/Complex - Issue occurred due to complex scenario.
      - Reason/Invalid - Issue is invalid.
      - Reason/Other - None of the above cases.

github-actions[bot] avatar May 22 '24 09:05 github-actions[bot]

https://github.com/ballerina-platform/ballerina-release/issues/2699

chiranSachintha avatar May 31 '24 09:05 chiranSachintha