Skip to content

Commit 49b8394

Browse files
committed
[#462] Improve error when using template with wrong number of arguments
1 parent c4e9dcf commit 49b8394

File tree

8 files changed

+73
-26
lines changed

8 files changed

+73
-26
lines changed

compiler/core/src/zserio/ast/TemplatableType.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,14 @@ TemplatableType instantiate(ArrayDeque<TypeReference> instantiationReferenceStac
8989
final List<TemplateArgument> templateArguments = instantiationReference.getTemplateArguments();
9090
if (templateParameters.size() != templateArguments.size())
9191
{
92-
throw new ParserException(instantiationReference,
93-
"Wrong number of template arguments for template '" + getName() + "'! Expecting " +
94-
templateParameters.size() + ", got " + templateArguments.size() + "!");
92+
final ParserStackedException stackedException = new ParserStackedException(
93+
instantiationReference.getLocation(),
94+
"Template instantiation of '" + getName() + "' has " +
95+
(templateParameters.size() > templateArguments.size() ? "too few" : "too many") +
96+
" arguments! Expecting " + templateParameters.size() +
97+
", got " + templateArguments.size() + "!");
98+
stackedException.pushMessage(getLocation(), " See " + getName() + " definition here");
99+
throw stackedException;
95100
}
96101

97102
TemplatableType instantiation = instantiateImpl(templateArguments, instantiationPackage);

compiler/core/src/zserio/ast/TypeInstantiation.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -146,21 +146,21 @@ protected void fillInstantiationStack(ParserStackedException exception)
146146
{
147147
exception.pushMessage(resolvingType.getLocation(),
148148
" See subtype '" +
149-
ZserioTypeUtil.getReferencedFullName(resolvingTypeReference) + "' definition here:");
149+
ZserioTypeUtil.getReferencedFullName(resolvingTypeReference) + "' definition here");
150150
resolvingTypeReference = ((Subtype)resolvingType).getTypeReference();
151151
}
152152
else if (resolvingType instanceof InstantiateType)
153153
{
154154
exception.pushMessage(resolvingType.getLocation(),
155155
" See template instantiation '" +
156-
ZserioTypeUtil.getReferencedFullName(resolvingTypeReference) + "' definition here:");
156+
ZserioTypeUtil.getReferencedFullName(resolvingTypeReference) + "' definition here");
157157
resolvingTypeReference = ((InstantiateType)resolvingType).getTypeReference();
158158
}
159159
resolvingType = resolvingTypeReference.getType();
160160
}
161161

162162
exception.pushMessage(resolvingType.getLocation(),
163-
" See '" + resolvingType.getName() + "' definition here:");
163+
" See '" + resolvingType.getName() + "' definition here");
164164
}
165165

166166
private final TypeReference typeReference;

compiler/core/src/zserio/ast/ZserioAstTemplator.java

+5
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ public void visitTypeReference(TypeReference typeReference)
106106
typeReference.getTemplateArguments());
107107
typeReference.resolveInstantiation(instantiation);
108108
}
109+
catch (InstantiationException e)
110+
{
111+
// prevent recursive InstantitationExcpetion (which is also ParserException)
112+
throw e;
113+
}
109114
catch (ParserException e)
110115
{
111116
throw new InstantiationException(e, instantiationReferenceStack);

test/errors/parameterized_types_error/java/parameterized_types_error/ParameterizedTypesErrorTest.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ public void noParameters()
3434
public void referencedBuiltInType()
3535
{
3636
final String[] errors = {
37-
"referenced_builtin_type_error.zs:3:9: See 'uint32' definition here:",
38-
"referenced_builtin_type_error.zs:3:16: See subtype 'Item' definition here:",
39-
"referenced_builtin_type_error.zs:5:14: See subtype 'Subtype' definition here:",
37+
"referenced_builtin_type_error.zs:3:9: See 'uint32' definition here",
38+
"referenced_builtin_type_error.zs:3:16: See subtype 'Item' definition here",
39+
"referenced_builtin_type_error.zs:5:14: See subtype 'Subtype' definition here",
4040
"referenced_builtin_type_error.zs:10:5: " +
4141
"Referenced type 'Subtype' is not a parameterized type!"
4242
};
@@ -47,7 +47,7 @@ public void referencedBuiltInType()
4747
public void referencedEnumType()
4848
{
4949
final String[] errors = {
50-
"referenced_enum_type_error.zs:3:13: See 'Item' definition here:",
50+
"referenced_enum_type_error.zs:3:13: See 'Item' definition here",
5151
"referenced_enum_type_error.zs:12:5: " +
5252
"Referenced type 'Item' is not a parameterized type!"
5353
};
@@ -58,7 +58,7 @@ public void referencedEnumType()
5858
public void referencedNonParameterizedType()
5959
{
6060
final String[] errors = {
61-
"referenced_non_parameterized_type_error.zs:3:8: See 'Item' definition here:",
61+
"referenced_non_parameterized_type_error.zs:3:8: See 'Item' definition here",
6262
"referenced_non_parameterized_type_error.zs:12:5: " +
6363
"Referenced type 'Item' is not a parameterized type!"
6464
};
@@ -69,7 +69,7 @@ public void referencedNonParameterizedType()
6969
public void referencedParameterizedType()
7070
{
7171
final String[] errors = {
72-
"referenced_parameterized_type_error.zs:3:8: See 'Item' definition here:",
72+
"referenced_parameterized_type_error.zs:3:8: See 'Item' definition here",
7373
"referenced_parameterized_type_error.zs:12:5: " +
7474
"Referenced type 'Item' is defined as parameterized type!"
7575
};
@@ -120,7 +120,7 @@ public void wrongEnumArgumentType()
120120
public void tooFewArguments()
121121
{
122122
final String[] errors = {
123-
"too_few_arguments_error.zs:3:8: See 'Item' definition here:",
123+
"too_few_arguments_error.zs:3:8: See 'Item' definition here",
124124
"too_few_arguments_error.zs:13:5: " +
125125
"Parameterized type instantiation of 'Item' has too few arguments! Expecting 2, got 1!"
126126
};
@@ -131,9 +131,9 @@ public void tooFewArguments()
131131
public void tooManyArguments()
132132
{
133133
final String[] errors = {
134-
"too_many_arguments_error.zs:3:8: See 'Item' definition here:",
135-
"too_many_arguments_error.zs:9:26: See template instantiation 'ItemU16' definition here:",
136-
"too_many_arguments_error.zs:11:17: See subtype 'Subtype' definition here:",
134+
"too_many_arguments_error.zs:3:8: See 'Item' definition here",
135+
"too_many_arguments_error.zs:9:26: See template instantiation 'ItemU16' definition here",
136+
"too_many_arguments_error.zs:11:17: See subtype 'Subtype' definition here",
137137
"too_many_arguments_error.zs:17:5: " +
138138
"Parameterized type instantiation of 'Subtype' has too many arguments! Expecting 1, got 2!"
139139
};

test/errors/templates_error/build.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,14 @@
5757
<testGen testName="templates_error" zsFile="symbol_with_type_name_clash_error.zs" ignoreErrors="true"/>
5858
<testGen testName="templates_error" zsFile="templatable_not_a_template_error.zs" ignoreErrors="true"/>
5959
<testGen testName="templates_error" zsFile="templated_template_parameter_error.zs" ignoreErrors="true"/>
60+
<testGen testName="templates_error" zsFile="too_few_arguments_error.zs" ignoreErrors="true"/>
61+
<testGen testName="templates_error" zsFile="too_many_arguments_error.zs" ignoreErrors="true"/>
6062
<testGen testName="templates_error" zsFile="unresolved_reference_in_template_error.zs"
6163
ignoreErrors="true"/>
6264
<testGen testName="templates_error" zsFile="unresolved_template_instantiation_error.zs"
6365
ignoreErrors="true"/>
6466
<testGen testName="templates_error" zsFile="unresolved_template_instantiation_in_template_error.zs"
6567
ignoreErrors="true"/>
66-
<testGen testName="templates_error" zsFile="wrong_number_of_arguments_error.zs" ignoreErrors="true"/>
6768
</target>
6869

6970
<target name="compile" depends="gen">

test/errors/templates_error/java/templates_error/TemplatesErrorTest.java

+27-8
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,33 @@ public void templatedTemplateParameter()
339339
assertTrue(zserioErrors.isPresent(error));
340340
}
341341

342+
@Test
343+
public void tooFewArguments()
344+
{
345+
final String errors[] =
346+
{
347+
"too_few_arguments_error.zs:11:5: In instantiation of 'TestStruct' required from here",
348+
"too_few_arguments_error.zs:3:8: See TestStruct definition here",
349+
"too_few_arguments_error.zs:11:5: " +
350+
"Template instantiation of 'TestStruct' has too few arguments! Expecting 2, got 1!"
351+
};
352+
assertTrue(zserioErrors.isPresent(errors));
353+
}
354+
355+
@Test
356+
public void tooManyArguments()
357+
{
358+
final String errors[] =
359+
{
360+
"too_many_arguments_error.zs:16:5: In instantiation of 'OtherStruct' required from here",
361+
"too_many_arguments_error.zs:11:5: In instantiation of 'TestStruct' required from here",
362+
"too_many_arguments_error.zs:3:8: See TestStruct definition here",
363+
"too_many_arguments_error.zs:11:5: " +
364+
"Template instantiation of 'TestStruct' has too many arguments! Expecting 2, got 3!"
365+
};
366+
assertTrue(zserioErrors.isPresent(errors));
367+
}
368+
342369
@Test
343370
public void unresolvedReferenceInTemplate()
344371
{
@@ -363,13 +390,5 @@ public void unresolvedTemplateInstantiationInTemplate()
363390
assertTrue(zserioErrors.isPresent(error));
364391
}
365392

366-
@Test
367-
public void wrongNumberOfArguments()
368-
{
369-
final String error = "wrong_number_of_arguments_error.zs:11:5: " +
370-
"Wrong number of template arguments for template 'TestStruct'! Expecting 2, got 1!";
371-
assertTrue(zserioErrors.isPresent(error));
372-
}
373-
374393
private static ZserioErrorOutput zserioErrors;
375394
}

test/errors/templates_error/zs/wrong_number_of_arguments_error.zs test/errors/templates_error/zs/too_few_arguments_error.zs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package wrong_number_of_arguments_error;
1+
package too_few_arguments_error;
22

33
struct TestStruct<T1, T2>
44
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package too_many_arguments_error;
2+
3+
struct TestStruct<T1, T2>
4+
{
5+
T1 value1;
6+
T2 value2;
7+
};
8+
9+
struct OtherStruct<T>
10+
{
11+
TestStruct<T, T, T> test;
12+
};
13+
14+
struct WrongNumberOfArguments
15+
{
16+
OtherStruct<uint32> test;
17+
};

0 commit comments

Comments
 (0)