Skip to content

Commit

Permalink
Merge pull request #42839 from nipunayf/fix-42454-9.x
Browse files Browse the repository at this point in the history
[2201.9.x] Revise the definition API of the TypeReferenceTypeSymbol to exclude searching for virtual symbols in the scope
  • Loading branch information
nipunayf authored Jun 3, 2024
2 parents 5d86fd3 + 81ef1b2 commit 2fb59a2
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.ballerina.compiler.api.symbols.TypeReferenceTypeSymbol;
import io.ballerina.compiler.api.symbols.TypeSymbol;
import io.ballerina.tools.diagnostics.Location;
import org.ballerinalang.model.symbols.SymbolOrigin;
import org.wso2.ballerinalang.compiler.semantics.model.Scope;
import org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol;
import org.wso2.ballerinalang.compiler.semantics.model.types.BParameterizedType;
Expand Down Expand Up @@ -103,7 +104,7 @@ public Symbol definition() {
if (referredType.tag == TypeTags.PARAMETERIZED_TYPE || bType.tag == TypeTags.PARAMETERIZED_TYPE) {
this.definition = symbolFactory.getBCompiledSymbol(((BParameterizedType) this.tSymbol.type).paramSymbol,
this.name());
} else if (referredType.tag == TypeTags.INTERSECTION) {
} else if (referredType.tag == TypeTags.INTERSECTION || referredType.tsymbol.origin == SymbolOrigin.VIRTUAL) {
this.definition = symbolFactory.getBCompiledSymbol(bType.tsymbol,
referredType.tsymbol.getName().getValue());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ private static BIntersectionType defineImmutableRecordType(Location pos, BRecord
BRecordTypeSymbol recordSymbol =
Symbols.createRecordSymbol(recordTypeSymbol.flags | Flags.READONLY,
getImmutableTypeName(names, getSymbolFQN(recordTypeSymbol)),
pkgID, null, env.scope.owner, pos, recordTypeSymbol.origin);
pkgID, null, env.scope.owner, pos, VIRTUAL);

BInvokableType bInvokableType = new BInvokableType(new ArrayList<>(), symTable.nilType, null);
BInvokableSymbol initFuncSymbol = Symbols.createFunctionSymbol(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import io.ballerina.projects.Document;
import io.ballerina.projects.Project;
import io.ballerina.tools.text.LinePosition;
import io.ballerina.tools.text.LineRange;
import org.ballerinalang.model.elements.PackageID;
import org.ballerinalang.test.BCompileUtil;
import org.testng.annotations.BeforeClass;
Expand Down Expand Up @@ -177,4 +178,31 @@ public Object[][] getTypeRefPos() {
{48, 4, "Baz"},
};
}

@Test(dataProvider = "TypeRefPosForTypeNarrowing")
public void testTypeRefLookupForTypeNarrowing(int line, int start, int end) {
Optional<TypeSymbol> typeSymbol = model.typeOf(LineRange.from(srcFile.name(), LinePosition.from(line, start),
LinePosition.from(line, end)));

assertTrue(typeSymbol.isPresent());
assertEquals(typeSymbol.get().typeKind(), TypeDescKind.TYPE_REFERENCE);

TypeReferenceTypeSymbol typeRefTSymbol = (TypeReferenceTypeSymbol) typeSymbol.get();
Symbol definition = typeRefTSymbol.definition();
assertEquals(definition.kind(), TYPE_DEFINITION);

Optional<String> definitionName = definition.getName();
assertTrue(definitionName.isPresent());
assertEquals(definitionName.get(), "(Person & readonly)");
}

@DataProvider(name = "TypeRefPosForTypeNarrowing")
public Object[][] getTypeRefPosForTypeNarrowing() {
return new Object[][]{
{56, 8, 14},
{62, 12, 18},
{68, 8, 14},
{76, 8, 11},
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,39 @@ function test() {
Baz z;
}

function test2() {
readonly & Person|int person = {name: "A", age: 0};
if person is int {
return;
}
_ = person.entries();
}

function test3() {
readonly & Person|int person = {name: "A", age: 0};
if person is readonly & Person {
_ = person.entries();
}
}

function test4() returns error? {
var person = check fn();
_ = person.entries();
}

function test5() {
Person & readonly|xml & readonly|error val = error("");
if val is xml & readonly|error {
return;
}
_ = val.entries();
}

// utils
type Foo Person;

type Bar Foo;

type Baz decimal;

function fn() returns readonly & Person|error => error("");

0 comments on commit 2fb59a2

Please sign in to comment.