Skip to content

Commit b54e6ab

Browse files
committed
Improve exception messages in BExprDb
1 parent 681edc6 commit b54e6ab

File tree

3 files changed

+29
-48
lines changed

3 files changed

+29
-48
lines changed

src/virtual-machine/src/main/java/org/smoothbuild/virtualmachine/bytecode/expr/BExprDb.java

Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -172,29 +172,19 @@ public BFold newFold(BExpr array, BExpr initial, BExpr folder) throws BytecodeEx
172172

173173
private BType validateFoldSubExprsAndGetResultType(BExpr array, BExpr initial, BExpr folder)
174174
throws BKindDbException {
175-
var lambdaType = validateMemberEvaluationTypeClass("folder", folder, BLambdaType.class);
175+
var folderType = validateMemberEvaluationTypeClass("folder", folder, BLambdaType.class);
176176
var arrayType = validateMemberEvaluationTypeClass("array", array, BArrayType.class);
177177
var initialType = initial.evaluationType();
178-
var params = lambdaType.params().elements();
179178
var elementType = arrayType.element();
180-
if (!(params.size() == 2
181-
&& params.get(0).equals(initialType)
182-
&& params.get(1).equals(elementType))) {
183-
throw illegalEvaluationType(
184-
"folder.arguments",
185-
expectedFolderArgumentsType(initialType, elementType),
186-
lambdaType.params());
187-
}
188-
var resultType = lambdaType.result();
189-
if (!resultType.equals(initialType)) {
190-
throw illegalEvaluationType("folder.result", initialType, resultType);
191-
}
192-
return resultType;
179+
var expectedFolderType = folderType(initialType, elementType);
180+
validateMemberType("folder", folderType, expectedFolderType);
181+
return initialType;
193182
}
194183

195-
private BTupleType expectedFolderArgumentsType(BType accumulatorType, BType elementType)
184+
private BLambdaType folderType(BType accumulatorType, BType arrayElementType)
196185
throws BKindDbException {
197-
return kindDb.tuple(accumulatorType, elementType);
186+
var params = kindDb.tuple(accumulatorType, arrayElementType);
187+
return kindDb.lambda(params, accumulatorType);
198188
}
199189

200190
public BIf newIf(BExpr condition, BExpr then_, BExpr else_) throws BytecodeException {
@@ -207,28 +197,20 @@ public BIf newIf(BExpr condition, BExpr then_, BExpr else_) throws BytecodeExcep
207197
}
208198

209199
public BMap newMap(BExpr array, BExpr mapper) throws BytecodeException {
210-
var mapperArgumentType = validateMapSubExprsAndGetMapperResultType(array, mapper);
211-
var kind = kindDb.map(kindDb.array(mapperArgumentType));
200+
var mapperResultType = validateMapSubExprsAndGetMapperResultType(array, mapper);
201+
var kind = kindDb.map(kindDb.array(mapperResultType));
212202
var data = writeChain(array.hash(), mapper.hash());
213203
var root = newRoot(kind, data);
214204
return kind.newExpr(root, this);
215205
}
216206

217207
private BType validateMapSubExprsAndGetMapperResultType(BExpr array, BExpr mapper)
218208
throws BKindDbException {
219-
var lambdaType = validateMemberEvaluationTypeClass("mapper", mapper, BLambdaType.class);
209+
var mapperType = validateMemberEvaluationTypeClass("mapper", mapper, BLambdaType.class);
220210
var arrayType = validateMemberEvaluationTypeClass("array", array, BArrayType.class);
221-
var params = lambdaType.params().elements();
222-
var elementType = arrayType.element();
223-
if (!(params.size() == 1 && params.get(0).equals(elementType))) {
224-
throw illegalEvaluationType(
225-
"mapper.arguments", expectedMapperArgumentsType(elementType), lambdaType.params());
226-
}
227-
return lambdaType.result();
228-
}
229-
230-
private BTupleType expectedMapperArgumentsType(BType elementType) throws BKindDbException {
231-
return kindDb.tuple(elementType);
211+
var expectedParamTypes = kindDb.tuple(arrayType.element());
212+
validateMemberType("mapper.parameters", mapperType.params(), expectedParamTypes);
213+
return mapperType.result();
232214
}
233215

234216
public BOrder newOrder(BArrayType evaluationType, List<? extends BExpr> elements)
@@ -324,12 +306,14 @@ private void validateOrderElements(BType elementType, List<? extends BExpr> elem
324306

325307
private static <T extends BType> T validateMemberEvaluationTypeClass(
326308
String memberName, BExpr member, Class<T> clazz) {
327-
var lambdaEvaluationType = member.evaluationType();
328-
if (!(lambdaEvaluationType.getClass().equals(clazz))) {
329-
throw illegalEvaluationType(memberName, clazz, lambdaEvaluationType);
309+
var evaluationType = member.evaluationType();
310+
var evaluationTypeClass = evaluationType.getClass();
311+
if (!(evaluationTypeClass.equals(clazz))) {
312+
throw illegalEvaluationType(
313+
memberName, clazz.getSimpleName(), evaluationTypeClass.getSimpleName());
330314
}
331315
@SuppressWarnings("unchecked")
332-
var cast = (T) lambdaEvaluationType;
316+
var cast = (T) evaluationType;
333317
return cast;
334318
}
335319

@@ -341,12 +325,6 @@ private static void validateMemberEvaluationType(
341325
}
342326
}
343327

344-
private static IllegalArgumentException illegalEvaluationType(
345-
String name, Class<?> expected, BType actual) {
346-
return illegalEvaluationType(
347-
name, expected.getSimpleName(), actual.getClass().getSimpleName());
348-
}
349-
350328
private static IllegalArgumentException illegalEvaluationType(
351329
String name, BType expected, BType actual) {
352330
return illegalEvaluationType(name, expected.name(), actual.name());
@@ -359,7 +337,10 @@ private static IllegalArgumentException illegalEvaluationType(
359337
}
360338

361339
private static void validateMemberType(String memberName, BValue member, BType expectedType) {
362-
var memberType = member.type();
340+
validateMemberType(memberName, member.type(), expectedType);
341+
}
342+
343+
private static void validateMemberType(String memberName, BType memberType, BType expectedType) {
363344
if (!memberType.equals(expectedType)) {
364345
throw new IllegalArgumentException("`%s.type()` should be `%s` but is `%s`."
365346
.formatted(memberName, expectedType.name(), memberType.name()));

src/virtual-machine/src/test/java/org/smoothbuild/virtualmachine/bytecode/expr/base/BFoldTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ void creating_fold_with_non_lambda_fails() {
3030
void creating_fold_with_folder_which_parameter_count_is_different_than_two_fails() {
3131
assertCall(() -> bytecodeF().fold(bArray(bInt()), bInt(), bLambda(list(bIntType()), bInt())))
3232
.throwsException(new IllegalArgumentException(
33-
"`folder.arguments.evaluationType()` should be `{Int,Int}` but is `{Int}`."));
33+
"`folder.type()` should be `(Int,Int)->Int` but is `(Int)->Int`."));
3434
}
3535

3636
@Test
3737
void creating_fold_with_folder_that_has_different_first_param_type_than_initial_type_fails() {
3838
assertCall(() -> bytecodeF()
3939
.fold(bArray(bInt()), bInt(), bLambda(list(bStringType(), bIntType()), bInt())))
4040
.throwsException(new IllegalArgumentException(
41-
"`folder.arguments.evaluationType()` should be `{Int,Int}` but is `{String,Int}`."));
41+
"`folder.type()` should be `(Int,Int)->Int` but is `(String,Int)->Int`."));
4242
}
4343

4444
@Test
@@ -47,15 +47,15 @@ void creating_fold_with_folder_that_has_different_first_param_type_than_initial_
4747
assertCall(() -> bytecodeF()
4848
.fold(bArray(bInt()), bInt(), bLambda(list(bIntType(), bStringType()), bInt())))
4949
.throwsException(new IllegalArgumentException(
50-
"`folder.arguments.evaluationType()` should be `{Int,Int}` but is `{Int,String}`."));
50+
"`folder.type()` should be `(Int,Int)->Int` but is `(Int,String)->Int`."));
5151
}
5252

5353
@Test
5454
void creating_fold_with_folder_that_has_different_result_type_than_initial_type_fails() {
5555
assertCall(() -> bytecodeF()
5656
.fold(bArray(bInt()), bInt(), bLambda(list(bIntType(), bIntType()), bString())))
5757
.throwsException(new IllegalArgumentException(
58-
"`folder.result.evaluationType()` should be `Int` but is `String`."));
58+
"`folder.type()` should be `(Int,Int)->Int` but is `(Int,Int)->String`."));
5959
}
6060

6161
@Nested

src/virtual-machine/src/test/java/org/smoothbuild/virtualmachine/bytecode/expr/base/BMapTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ void creating_map_with_non_lambda_fails() {
3030
void creating_map_with_mapper_which_parameter_count_is_different_than_one_fails() {
3131
assertCall(() -> bMap(bArray(bInt()), bii2iLambda()))
3232
.throwsException(new IllegalArgumentException(
33-
"`mapper.arguments.evaluationType()` should be `{Int}` but is `{Int,Int}`."));
33+
"`mapper.parameters.type()` should be `{Int}` but is `{Int,Int}`."));
3434
}
3535

3636
@Test
3737
void creating_map_with_mapper_that_has_different_type_than_array_element_type_fails() {
3838
assertCall(() -> bMap(bArray(bString()), bIntIdLambda()))
3939
.throwsException(new IllegalArgumentException(
40-
"`mapper.arguments.evaluationType()` should be `{String}` but is `{Int}`."));
40+
"`mapper.parameters.type()` should be `{String}` but is `{Int}`."));
4141
}
4242

4343
@Nested

0 commit comments

Comments
 (0)