ballerina-platform / ballerina-lang

The Ballerina Programming Language
https://ballerina.io/
Apache License 2.0
3.68k stars 751 forks source link

Compiler crashes when running an XML processing service #33120

Closed miyurud closed 3 years ago

miyurud commented 3 years ago

Description: When I run the following sample service in Ballerina SL Alpha 5.11 the service crashes giving a lengthy error log.

import ballerina/http;
import ballerina/lang.'decimal;

// INFO: Pass an XML such as <invoice><item><name>White Sugar</name><quantity>4</quantity><unit_price>100</unit_price></item>
// <item><name>Wheat Flour</name><quantity>2</quantity><unit_price>200</unit_price></item>
// <item><name>Chicken Egg</name><quantity>2</quantity><unit_price promo-code="free">20</unit_price></item></invoice>
// when invoking this service
service / on new http:Listener(8090) {
    resource function post total(@http:Payload xml invoice) returns xml|error {
        decimal total = 0;
        foreach var item in invoice/<*> {
            xml unitPrice = item/<unit_price>;
            if (unitPrice is xml:Element) {
                string? code = unitPrice.getAttributes()["promo_code"];
                if code == "free" {
                    continue;
                }
            }

            decimal qt =  check decimal:fromString((item/<quantity>).data());
            decimal uPrice = check decimal:fromString(unitPrice.data());
            total += qt * uPrice;
        }

        return xml `<grand_total>${total}</grand_total>`;
    }
}

The last part of the lengthy error I get is shown below,

    same_locals_1_stack_item_frame(@6521,Object[#126])
    same_locals_1_stack_item_frame(@6526,Object[#128])
    same_frame(@6531)
    same_frame(@6541)
    same_frame(@6551)
    same_frame(@6564)
    same_frame(@6571)
    same_frame(@6606)
    same_frame(@6609)
    same_frame(@6625)
    same_frame(@6635)
    same_frame(@6670)
    same_frame(@6673)
    same_frame(@6687)
    same_frame(@6697)
    same_frame(@6705)
    same_frame(@6740)
    same_frame(@6743)
    same_frame(@6763)
    same_frame(@6773)
    same_frame(@6787)
    same_frame(@6795)
    same_frame(@6803)
    same_frame(@6838)
    same_frame(@6841)
    same_frame(@6861)
    same_frame(@6871)
    same_frame(@6881)
    same_frame(@6894)
    same_frame(@6901)
    same_frame(@6936)
    same_frame(@6939)
    same_frame(@6955)
    same_frame(@6965)
    same_frame(@7000)
    same_frame(@7003)
    same_frame(@7017)
    same_frame(@7027)
    full_frame(@7035,{Object[#3],Object[#11],Object[#159],Integer,Null,Null,Null,Long,Null,Null,Null,Null,Null,Null,Long,Null,Null,Null,Null,Integer,Null,Null,Null,Null,Null,Null,Null,Null,Null,Null,Null,Null,Null,Null,Null,Null,Null,Null,Null,Null,Null,Integer,Null,Null,Integer,Null,Null,Integer,Null,Null,Integer,Null,Null,Integer,Null,Null,Integer,Null,Null,Integer,Null,Null,Integer,Null,Null,Integer,Null,Null,Integer,Null,Null,Integer,Null,Null,Integer,Null,Null,Integer,Null,Integer,Null,Null,Integer},{})
    full_frame(@7540,{Object[#3],Object[#11],Object[#159],Integer,Object[#186],Object[#602],Object[#604],Long,Object[#602],Object[#186],Object[#152],Object[#159],Object[#159],Object[#604],Long,Object[#159],Object[#7],Object[#159],Object[#186],Integer,Object[#159],Object[#210],Object[#159],Object[#186],Object[#159],Object[#210],Object[#186],Object[#152],Object[#152],Object[#186],Object[#159],Object[#152],Object[#126],Object[#152],Object[#152],Object[#186],Object[#152],Object[#126],Object[#152],Object[#159],Object[#126],Integer,Object[#126],Object[#126],Integer,Object[#126],Object[#126],Integer,Object[#126],Object[#126],Integer,Object[#126],Object[#126],Integer,Object[#126],Object[#126],Integer,Object[#126],Object[#126],Integer,Object[#126],Object[#126],Integer,Object[#126],Object[#126],Integer,Object[#126],Object[#126],Integer,Object[#126],Object[#126],Integer,Object[#126],Object[#126],Integer,Object[#126],Object[#126],Integer,Object[#126],Integer,Object[#126],Object[#186],Integer},{})

        at $$$.$gen$$0046$0046$0060init$00620(.:8)
        at $_init.$gen$$0046$0060init$0062(.:0)
        at $_init.$moduleInit(.)
        at $_init.$lambda$$moduleInit$(.)
        at io.ballerina.runtime.internal.scheduling.SchedulerItem.execute(Scheduler.java:571)
        at io.ballerina.runtime.internal.scheduling.Scheduler.run(Scheduler.java:301)
        at io.ballerina.runtime.internal.scheduling.Scheduler.runSafely(Scheduler.java:269)
        at java.base/java.lang.Thread.run(Thread.java:834)

Steps to reproduce:

Affected Versions: I found this error in Ballerina SL Alpha 5.11

OS, DB, other environment details and versions: Ubuntu 20.04, JDK 11.0.8

rdhananjaya commented 3 years ago
import ballerina/http;

service / on new http:Listener(8090) {
    // this breaks
    function bar() returns xml|error {
        return xml `<grand_total>${1d}</grand_total>`;
    }

    // this is also fine
        function bar1() returns xml {
        return xml `<grand_total>${1d}</grand_total>`;
    }
}

// this is fine
class B {
    function bar() returns xml|error {
        return xml `<grand_total>${1d}</grand_total>`;
    }
}