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

[Bug]: Can't use types that don't explicitly redefine strings field for raw template expressions #43501

Open
heshanpadmasiri opened this issue Oct 21, 2024 · 0 comments
Labels
Team/jBallerina All the issues related to BIR, JVM backend code generation and runtime Type/Bug

Comments

@heshanpadmasiri
Copy link
Member

Description

For raw template expressions we can given a object type that includes object:RawTemplat with a narrower insertions field as the contextually expected type. However if that type in question don't explicitly override the strings field,

  1. insertions and strings get mixed up
  2. trying to print the object value causes NPE

This is potentionally an object type inclusion bug

Steps to Reproduce

Consider,

import ballerina/io;

type Template2 object {
    *object:RawTemplate;
    public (string[] & readonly) strings;
    public int[] insertions;
};

type Template1 object {
    *object:RawTemplate;
    public int[] insertions;
};

public function main() {
    Template2 v1 = `${1}, ${2}`;
    io:println(v1.insertions);
    io:println(v1.strings);

    Template1 v2 = `${1}, ${2}`;
    io:println(v2.insertions);
    io:println(v2.strings);
}

This results: (note insertions and strings are interchanged in second example)

[1,2]
["",", ",""]
["",", ",""]
[1,2]

This caues NPE:

import ballerina/io;

type Template1 object {
    *object:RawTemplate;
    public int[] insertions;
};

public function main() {
    Template1 v2 = `${1}, ${2}`;
    io:println(v2);
}
[2024-10-21 15:36:34,674] SEVERE {b7a.log.crash} - Cannot load from object array because "this.refValues" is null 
java.lang.NullPointerException: Cannot load from object array because "this.refValues" is null
	at io.ballerina.runtime.internal.values.ArrayValueImpl.getBString(ArrayValueImpl.java:456)
	at ballerina.io.1.$value$PrintableRawTemplateImpl.toString(print.bal:127)
	at ballerina.io.1.$value$PrintableRawTemplateImpl.call(print.bal)
	at ballerina.io.1.$value$PrintableClassImpl.toString(print.bal:107)
	at ballerina.io.1.$value$PrintableClassImpl.call(print.bal)
	at io.ballerina.runtime.internal.util.StringUtils.getStringVal(StringUtils.java:96)
	at io.ballerina.runtime.api.utils.StringUtils.getStringValue(StringUtils.java:173)
	at io.ballerina.stdlib.io.nativeimpl.PrintUtils.println(PrintUtils.java:69)
	at ballerina.io.1.print.externPrintln(print.bal:147)
	at ballerina.io.1.print.println(print.bal:65)
	at main.main(main.bal:10)
	at $_init.$moduleExecute(.)
	at $_init.$lambda$$moduleExecute$(.)
	at io.ballerina.runtime.internal.scheduling.SchedulerItem.execute(SchedulerItem.java:54)
	at io.ballerina.runtime.internal.scheduling.Scheduler.run(Scheduler.java:320)
	at io.ballerina.runtime.internal.scheduling.Scheduler.runSafely(Scheduler.java:287)
	at java.base/java.lang.Thread.run(Thread.java:840)

Affected Version(s)

Ballerina 2201.10.1 (Swan Lake Update 10)
Language specification 2024R1
Update Tool 1.4.3

OS, DB, other environment details and versions

No response

Related area

-> Runtime

Related issue(s) (optional)

No response

Suggested label(s) (optional)

No response

Suggested assignee(s) (optional)

No response

@ballerina-bot ballerina-bot added the Team/jBallerina All the issues related to BIR, JVM backend code generation and runtime label Oct 21, 2024
@heshanpadmasiri heshanpadmasiri changed the title [Bug]: Can't use custom types that don't explicitly redefine strings field for raw template expressions [Bug]: Can't use types that don't explicitly redefine strings field for raw template expressions Oct 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Team/jBallerina All the issues related to BIR, JVM backend code generation and runtime Type/Bug
Projects
None yet
Development

No branches or pull requests

2 participants