Skip to content

Commit

Permalink
Merge pull request #42108 from nipunayf/fix-42094
Browse files Browse the repository at this point in the history
Add the locations to the field in an intersected record
  • Loading branch information
KavinduZoysa authored Feb 8, 2024
2 parents 53730b9 + f9b150a commit 0823dc9
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5616,10 +5616,10 @@ private boolean populateFields(IntersectionContext intersectionContext, BRecordT
invokableSymbol.flags = tsymbol.flags;
} else {
recordFieldSymbol = new BVarSymbol(intersectionFlags, name, env.enclPkg.packageID,
intersectionFieldType, newTypeSymbol, lhsRecordField.pos, SOURCE);
intersectionFieldType, newTypeSymbol, lhsRecordField.symbol.pos, SOURCE);
}

newTypeFields.put(key, new BField(name, null, recordFieldSymbol));
newTypeFields.put(key, new BField(name, recordFieldSymbol.pos, recordFieldSymbol));
newTypeSymbol.scope.define(name, recordFieldSymbol);
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@
package io.ballerina.semantic.api.test.symbols;

import io.ballerina.compiler.api.SemanticModel;
import io.ballerina.compiler.api.impl.symbols.BallerinaErrorTypeSymbol;
import io.ballerina.compiler.api.impl.symbols.BallerinaIntersectionTypeSymbol;
import io.ballerina.compiler.api.impl.symbols.BallerinaRecordTypeSymbol;
import io.ballerina.compiler.api.impl.symbols.BallerinaTypeReferenceTypeSymbol;
import io.ballerina.compiler.api.symbols.Documentable;
import io.ballerina.compiler.api.symbols.Documentation;
import io.ballerina.compiler.api.symbols.ErrorTypeSymbol;
import io.ballerina.compiler.api.symbols.ModuleSymbol;
import io.ballerina.compiler.api.symbols.RecordFieldSymbol;
import io.ballerina.compiler.api.symbols.Symbol;
import io.ballerina.compiler.api.symbols.SymbolKind;
import io.ballerina.compiler.api.symbols.TypeDefinitionSymbol;
Expand All @@ -41,6 +46,7 @@
import org.wso2.ballerinalang.compiler.util.Names;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import static io.ballerina.semantic.api.test.util.SemanticAPITestUtils.getDefaultModulesSemanticModel;
Expand Down Expand Up @@ -133,6 +139,46 @@ public Object[][] getTypeRefErrorType() {
};
}

@Test(dataProvider = "IntersectionErrorTypeProvider")
public void testFieldDescriptorsOfErrorIntersection(int line, int offset, String expectedErrorType,
List<String> expectedFieldNames) {
Optional<Symbol> symbol = model.symbol(srcFile, LinePosition.from(line, offset));
assertTrue(symbol.isPresent());

Symbol ballerinaTypeDefinitionSymbol = symbol.get();
assertEquals(ballerinaTypeDefinitionSymbol.kind(), SymbolKind.TYPE);
assertTrue(ballerinaTypeDefinitionSymbol.getName().isPresent());
assertEquals(ballerinaTypeDefinitionSymbol.getName().get(), expectedErrorType);

TypeSymbol ballerinaIntersectionTypeSymbol =
((BallerinaTypeReferenceTypeSymbol) ballerinaTypeDefinitionSymbol).typeDescriptor();
assertEquals(ballerinaIntersectionTypeSymbol.kind(), SymbolKind.TYPE);

TypeSymbol ballerinaErrorTypeSymbol =
((BallerinaIntersectionTypeSymbol) ballerinaIntersectionTypeSymbol).effectiveTypeDescriptor();
assertEquals(ballerinaErrorTypeSymbol.kind(), SymbolKind.TYPE);

TypeSymbol ballerinaRecordTypeSymbol =
((BallerinaErrorTypeSymbol) ballerinaErrorTypeSymbol).detailTypeDescriptor();
assertEquals(ballerinaRecordTypeSymbol.kind(), SymbolKind.TYPE);

Map<String, RecordFieldSymbol> stringRecordFieldSymbolMap =
((BallerinaRecordTypeSymbol) ballerinaRecordTypeSymbol).fieldDescriptors();
List<String> actualFieldNames = stringRecordFieldSymbolMap.keySet().stream().toList();
assertEquals(actualFieldNames.size(), expectedFieldNames.size());
for (int i = 0; i < actualFieldNames.size(); i++) {
assertEquals(actualFieldNames.get(i), expectedFieldNames.get(i));
}
}

@DataProvider(name = "IntersectionErrorTypeProvider")
public Object[][] getIntersectionErrorType() {
return new Object[][]{
{62, 4, "SimpleIntersectionError", List.of("msg", "value")},
{63, 4, "MultipleIntersectionError", List.of("flag", "msg", "value")}
};
}

private void assertModuleInfo(ModuleSymbol module) {
assertEquals(module.id().orgName(), Names.ANON_ORG.toString());
assertEquals(module.id().moduleName(), PackageID.DEFAULT.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,23 @@ function test() {
CancelledError distinctErr;
IntersectionError intersectionErr;
}

type ErrorDetail1 record {
string msg;
int value?;
};

type ErrorDetail2 record {|
*ErrorDetail1;
boolean|int flag;
|};

type SimpleError error<ErrorDetail1>;
type InclusionError error<ErrorDetail2>;

type SimpleIntersectionError distinct error & error<ErrorDetail1> & error<ErrorDetail1>;
type MultipleIntersectionError InclusionError & error<ErrorDetail1> & SimpleError & error<ErrorDetail2>;
function testMultipleIntersectionError() {
SimpleIntersectionError simpleErr;
MultipleIntersectionError multipleErr;
}

0 comments on commit 0823dc9

Please sign in to comment.