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

Improve xml index expr type checking #22088

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 @@ -318,6 +318,7 @@ public enum DiagnosticCode {
UNSUPPORTED_METHOD_INVOCATION_XML_NAV("method.invocation.in.xml.navigation.expressions.not.supported"),
DEPRECATED_XML_ATTRIBUTE_ACCESS("deprecated.xml.attribute.access.expression"),
UNSUPPORTED_INDEX_IN_XML_NAVIGATION("indexing.within.xml.navigation.expression.not.supported"),
DEPRECATED_XML_CHILD_ACCESS("deprecated.xml.child.access"),

UNDEFINED_ANNOTATION("undefined.annotation"),
ANNOTATION_NOT_ALLOWED("annotation.not.allowed"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4640,7 +4640,15 @@ private BType checkIndexAccessExpr(BLangIndexBasedAccess indexBasedAccessExpr) {
return actualType;
}

checkExpr(indexExpr, this.env);
BType indexExprType = checkExpr(indexExpr, this.env);
if (types.isAssignable(indexExprType, symTable.stringType)) {
dlog.warning(indexBasedAccessExpr.pos, DiagnosticCode.DEPRECATED_XML_CHILD_ACCESS);
} else if (!types.isAssignable(indexExprType, symTable.intType)) {
dlog.error(
indexBasedAccessExpr.pos, DiagnosticCode.INCOMPATIBLE_TYPES, symTable.intType, indexExprType);
return symTable.semanticError;
}

actualType = symTable.xmlType;
indexBasedAccessExpr.originalType = actualType;
} else if (varRefType == symTable.semanticError) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,10 @@ warning.usage.of.deprecated.construct=\
warning.non.module.qualified.error.reason=\
error reason ''{0}'' is not module qualified

warning.deprecated.xml.child.access=\
xml child access using member access is deprecated and will be removed in the next minor release. xml \
step expressions can now be used to access children.

error.redeclared.import.module=\
redeclared import module ''{0}''

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,18 @@ public void testXMLNavigationExpressionWithQuotedIdentifiers() {

@Test
public void testInvalidXMLAccessWithIndex() {
BAssertUtil.validateError(negativeResult, 0, "cannot update an xml sequence", 5, 5);
int i = 0;
BAssertUtil.validateError(negativeResult, i++, "cannot update an xml sequence", 5, 5);
BAssertUtil.validateError(negativeResult, i++, "cannot update an xml sequence", 13, 5);
BAssertUtil.validateError(negativeResult, i++, "invalid assignment in variable 'x1/*'", 13, 5);
BAssertUtil.validateWarning(negativeResult, i++, "xml child access using member access is deprecated and " +
"will be removed in the next minor release. xml step expressions can now be used to access children.",
18, 13);
BAssertUtil.validateError(negativeResult, i++, "incompatible types: expected 'int', found 'boolean'", 19, 13);
BAssertUtil.validateError(negativeResult, i++, "incompatible types: expected 'int', found 'float'", 20, 13);

BAssertUtil.validateError(negativeResult, 1, "cannot update an xml sequence", 13, 5);
Assert.assertEquals(negativeResult.getErrorCount(), i - 1);
Assert.assertEquals(negativeResult.getWarnCount(), 1);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ function testUpdatingGetAllChildren() {
xml x2 = xml `<fruit>apple</fruit>`;
x1/* = x2;
}

function testNonStringIndexAccess() {
xml x = xml `<elem></elem>`;
var c = x["child"];
var k = x[true];
var b = x[1.2];
}