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

Add the locations to the field in an intersected record #42108

Merged
merged 3 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
Copy link
Member

@chiranSachintha chiranSachintha Feb 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is okay to set the position here. My concern is why we keep the position as a field of BField. Normally, we access the position from the symbol. Here as well, when we need to retrieve the position, we need to obtain it from the symbol.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we create the record field symbol of the resultant record symbol of the intersection, we use the position of this BField. Alternatively, we can use lhsRecordField.symbol.pos as well here.

intersectionFieldType, newTypeSymbol, lhsRecordField.pos, SOURCE);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. IMO, That is the best way to access the position. Shall we try whether we can remove pos in the BField without affecting the code?

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;
}
nipunayf marked this conversation as resolved.
Show resolved Hide resolved
Loading