Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanvive committed Jun 11, 2020
1 parent fb4c8f3 commit 94a439e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"),


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -886,7 +892,7 @@ private BType inferTableMemberType(List<BType> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}''

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'",
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"}});
}

0 comments on commit 94a439e

Please sign in to comment.