From: Roger Meier Date: Sun, 24 Jun 2012 19:20:58 +0000 (+0000) Subject: THRIFT-1635 D codegen bug for required non-nullable fields X-Git-Tag: 0.9.1~333 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=40633a6316112d56f66b0e4b6cd6f9ec405b41bd;p=common%2Fthrift.git THRIFT-1635 D codegen bug for required non-nullable fields Patch: David Nadlinger git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1353316 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/lib/d/src/thrift/codegen/base.d b/lib/d/src/thrift/codegen/base.d index 96240120..d6973750 100644 --- a/lib/d/src/thrift/codegen/base.d +++ b/lib/d/src/thrift/codegen/base.d @@ -782,14 +782,14 @@ void writeStruct(T, Protocol, alias fieldMetaData = cast(TFieldMeta[])null, // WORKAROUND: To stop LDC from emitting the manifest constant »meta« below // into the writeStruct function body this is inside the string mixin // block – the code wouldn't depend on it (this is an LDC bug, and because - // of it a new array would be allocate on each method invocation at runtime). + // of it a new array would be allocated on each method invocation at runtime). foreach (name; StaticFilter!( Compose!(isNullable, PApply!(MemberType, T)), FieldNames!T )) { static if (memberReq!(T, name, fieldMetaData) == TReq.REQUIRED) { - code ~= `enforce(__traits(getMember, s, name) !is null, - new TException("Required field '` ~ name ~ `' is null."));\n`; + code ~= "enforce(__traits(getMember, s, `" ~ name ~ "`) !is null, + new TException(`Required field '" ~ name ~ "' is null.`));\n"; } } @@ -908,6 +908,40 @@ void writeStruct(T, Protocol, alias fieldMetaData = cast(TFieldMeta[])null, p.writeStructEnd(); } +unittest { + // Ensure that the generated code at least compiles for the basic field type + // combinations. Functionality checks are covered by the rest of the test + // suite. + + struct Test { + // Non-nullable. + int a1; + int a2; + int a3; + int a4; + + // Nullable. + string b1; + string b2; + string b3; + string b4; + + mixin TStructHelpers!([ + TFieldMeta("a1", 1, TReq.OPT_IN_REQ_OUT), + TFieldMeta("a2", 2, TReq.OPTIONAL), + TFieldMeta("a3", 3, TReq.REQUIRED), + TFieldMeta("a4", 4, TReq.IGNORE), + TFieldMeta("b1", 5, TReq.OPT_IN_REQ_OUT), + TFieldMeta("b2", 6, TReq.OPTIONAL), + TFieldMeta("b3", 7, TReq.REQUIRED), + TFieldMeta("b4", 8, TReq.IGNORE), + ]); + } + + static assert(__traits(compiles, { Test t; t.read(cast(TProtocol)null); })); + static assert(__traits(compiles, { Test t; t.write(cast(TProtocol)null); })); +} + private { /* * Returns a D code string containing the matching TType value for a passed