From 94a439e4fc4886c7f876575ebdb0bbbc366b6a59 Mon Sep 17 00:00:00 2001 From: Mohan Date: Thu, 11 Jun 2020 14:09:30 +0530 Subject: [PATCH] Fix for https://github.com/ballerina-platform/ballerina-spec/issues/502 --- .../org/ballerinalang/util/diagnostic/DiagnosticCode.java | 1 + .../compiler/semantics/analyzer/TypeChecker.java | 8 +++++++- .../ballerina-lang/src/main/resources/compiler.properties | 5 ++++- .../ballerinalang/test/types/table/TableNegativeTest.java | 6 ++++-- .../resources/test-src/types/table/table-negative.bal | 5 +++++ 5 files changed, 21 insertions(+), 4 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/util/diagnostic/DiagnosticCode.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/util/diagnostic/DiagnosticCode.java index 8a952c8ec86c..d5aa20e71587 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/util/diagnostic/DiagnosticCode.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/util/diagnostic/DiagnosticCode.java @@ -441,6 +441,7 @@ public enum DiagnosticCode { KEY_SPECIFIER_FIELD_VALUE_MUST_BE_CONSTANT("key.specifier.field.value.must.be.constant"), KEY_CONSTRAINT_NOT_SUPPORTED_FOR_TABLE_WITH_MAP_CONSTRAINT ("key.constraint.not.supported.for.table.with.map.constraint"), + CANNOT_INFER_MEMBER_TYPE_FOR_TABLE_DUE_AMBIGUITY("cannot.infer.member.type.for.table.due.ambiguity"), CANNOT_INFER_MEMBER_TYPE_FOR_TABLE("cannot.infer.member.type.for.table"), diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeChecker.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeChecker.java index d24b27ea72b8..d989ea8a88ac 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeChecker.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeChecker.java @@ -740,6 +740,12 @@ public void visit(BLangTableConstructorExpr tableConstructorExpr) { } } + if (tableConstructorExpr.recordLiteralList.size() == 0) { + dlog.error(tableConstructorExpr.pos, DiagnosticCode.CANNOT_INFER_MEMBER_TYPE_FOR_TABLE); + resultType = symTable.semanticError; + return; + } + BType inherentMemberType = inferTableMemberType(memTypes, tableConstructorExpr); BTableType tableType = new BTableType(TypeTags.TABLE, inherentMemberType, null); for (BLangRecordLiteral recordLiteral : tableConstructorExpr.recordLiteralList) { @@ -886,7 +892,7 @@ private BType inferTableMemberType(List memTypes, BLangTableConstructorEx String fieldName = field.name.value; if (fieldNames.contains(fieldName)) { - dlog.error(tableConstructorExpr.pos, DiagnosticCode.CANNOT_INFER_MEMBER_TYPE_FOR_TABLE, + dlog.error(tableConstructorExpr.pos, DiagnosticCode.CANNOT_INFER_MEMBER_TYPE_FOR_TABLE_DUE_AMBIGUITY, fieldName); return symTable.semanticError; } diff --git a/compiler/ballerina-lang/src/main/resources/compiler.properties b/compiler/ballerina-lang/src/main/resources/compiler.properties index 98f78684572d..8bf3c139a64b 100644 --- a/compiler/ballerina-lang/src/main/resources/compiler.properties +++ b/compiler/ballerina-lang/src/main/resources/compiler.properties @@ -1117,9 +1117,12 @@ error.key.specifier.field.value.must.be.constant = \ error.key.constraint.not.supported.for.table.with.map.constraint = \ table with constraint of type 'map' cannot have key specifier or key type constraint -error.cannot.infer.member.type.for.table = \ +error.cannot.infer.member.type.for.table.due.ambiguity = \ cannot infer the member type from table constructor. field ''{0}'' type is ambiguous +error.cannot.infer.member.type.for.table = \ + cannot infer the member type from table constructor; no values are provided in table constructor + error.arrow.expression.mismatched.parameter.length=\ invalid number of parameters used in arrow expression. expected: ''{0}'' but found ''{1}'' diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/table/TableNegativeTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/table/TableNegativeTest.java index 0456504159dc..b9d3b1ea7064 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/table/TableNegativeTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/table/TableNegativeTest.java @@ -35,7 +35,7 @@ public class TableNegativeTest { @Test public void testTableNegativeCases() { CompileResult compileResult = BCompileUtil.compile("test-src/types/table/table-negative.bal"); - Assert.assertEquals(compileResult.getErrorCount(), 20); + Assert.assertEquals(compileResult.getErrorCount(), 21); int index = 0; validateError(compileResult, index++, "unknown type 'CusTable'", @@ -76,7 +76,9 @@ public void testTableNegativeCases() { validateError(compileResult, index++, "table with constraint of type map cannot have key " + "specifier or key type constraint", 116, 21); validateError(compileResult, index++, "member access is not supported for keyless table 'tab'", 120, 26); - validateError(compileResult, index, "cannot infer the member type from table constructor. " + + validateError(compileResult, index++, "cannot infer the member type from table constructor. " + "field 'id' type is ambiguous", 125, 14); + validateError(compileResult, index, "cannot infer the member type from table constructor; " + + "no values are provided in table constructor", 130, 25); } } diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/types/table/table-negative.bal b/tests/jballerina-unit-test/src/test/resources/test-src/types/table/table-negative.bal index 95718b3caaca..44b42c7b499d 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/types/table/table-negative.bal +++ b/tests/jballerina-unit-test/src/test/resources/test-src/types/table/table-negative.bal @@ -125,3 +125,8 @@ function testInferMemberType() { var arr = table [{ id: 13 , name: "Sanjiva", address: "Weerawarana" }, { id: "Hello" , name: "James" , address: "Clark" }]; } + +function testVarTypeTableInfering() { + var customerTable = table []; + customerTable.put({id: 3, name: "Pope", age: 19, address: {no: 12, road: "Sea street"}}); +}