Commit a5a0bc6c authored by Kenton Varda's avatar Kenton Varda

Allow a parameter's default to be set explicitly to null.

Fixes #363.
parent 7aca59a9
......@@ -2102,8 +2102,28 @@ private:
auto typeBuilder = slot.initType();
if (translator.compileType(member.fieldType, typeBuilder, implicitMethodParams)) {
if (member.hasDefaultValue) {
translator.compileBootstrapValue(member.fieldDefaultValue,
typeBuilder, slot.initDefaultValue());
if (member.isParam &&
member.fieldDefaultValue.isRelativeName() &&
member.fieldDefaultValue.getRelativeName().getValue() == "null") {
// special case: parameter set null
switch (typeBuilder.which()) {
case schema::Type::TEXT:
case schema::Type::DATA:
case schema::Type::LIST:
case schema::Type::STRUCT:
case schema::Type::INTERFACE:
case schema::Type::ANY_POINTER:
break;
default:
errorReporter.addErrorOn(member.fieldDefaultValue.getRelativeName(),
"Only pointer parameters can declare their default as 'null'.");
break;
}
translator.compileDefaultDefaultValue(typeBuilder, slot.initDefaultValue());
} else {
translator.compileBootstrapValue(member.fieldDefaultValue,
typeBuilder, slot.initDefaultValue());
}
slot.setHadExplicitDefault(true);
} else {
translator.compileDefaultDefaultValue(typeBuilder, slot.initDefaultValue());
......
......@@ -840,6 +840,8 @@ interface TestMoreStuff extends(TestCallOrder) {
methodWithDefaults @8 (a :Text, b :UInt32 = 123, c :Text = "foo") -> (d :Text, e :Text = "bar");
methodWithNullDefault @12 (a :Text, b :TestInterface = null);
getHandle @9 () -> (handle :TestHandle);
# Get a new handle. Tests have an out-of-band way to check the current number of live handles, so
# this can be used to test garbage collection.
......
......@@ -155,3 +155,7 @@ const embedNoSuchFile :Data = embed "no-such-file";
using Baz = import "nosuchfile-unused.capnp".Baz;
# Check that an import in an unused `using` still reports error.
interface TestInterface {
foo @0 (a :UInt32 = null);
}
......@@ -56,4 +56,5 @@ file:149:18-49: error: Double-application of generic parameters.
file:150:38-43: error: Sorry, only pointer types can be used as generic parameters.
file:153:30-44: error: Embeds can only be used when Text, Data, or a struct is expected.
file:154:37-51: error: Couldn't read file for embed: no-such-file
file:160:23-27: error: Only pointer parameters can declare their default as 'null'.
file:156:20-45: error: Import failed: nosuchfile-unused.capnp
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment