Files
logos-cpp-sdk/tests/experimental/test_lidl_gen_cdylib.cpp
Dario LipicarandClaude Opus 5 198f0317ca feat(optional): ?T is two-state, and the generators finally read it (#125)
* feat(optional): ?T is two-state, and the generators finally read it

No generator in any language read the optional flag — it had never been
implemented. `?T` was a HARD REJECT on the cdylib backend ("module not
cdylib-eligible"), `std::optional<T>` in an impl header fell through to the
opaque `any` with no diagnostic, and a `? name: T` field was emitted as a
required `T`. Three real contracts in the workspace already declare optionals
and were silently getting one of those three answers.

`?T` is TWO-state: a value of T, or empty. Never three — "one LIDL type <-> one
type per language" leaves nowhere for a third state, because every target has
exactly one empty inhabitant.

ONE MEANING, TWO SPELLINGS. `? name: T` (the field flag) and `name: ?T` (the
type kind) are the same declaration. Backends no longer answer that themselves:
logos-lidl's fieldIsOptional/fieldValueType are re-exported from lidl_compat.h
and every site THIS COMMIT TOUCHES reads them, so the two spellings emit
byte-identical code on the cdylib and client backends. That
caught a live drift on the way in — lidlRecordCollidesWithBytesTag read `f.type`
and so refused `? _bytes: tstr` while letting `_bytes: ?tstr` straight through,
one declaration with two answers.

THE WIRE RULE DEPENDS ON THE SLOT. Absent and explicit null are the SAME state
on decode and DIFFERENT on encode:
  - decode is liberal, by exactly one inhabitant: in an optional slot absent and
    null both mean empty; in a required slot both stay errors. A present value
    goes through the decoder a required T would get, so a wrong type still fails
    at the same path — optional widens the domain, it does not switch checking
    off. `?bstr` therefore keeps the LENIENT bytes decode a bare `bstr` gets,
    rather than silently becoming stricter in the optional slot.
  - encode has one canonical form: empty OMITS the key where the slot is NAMED
    (a record field) and is spelled null where it is POSITIONAL (an argument, a
    return, an event parameter — no key to omit, and arity must not change). Key
    omission lives in the record emitter because a Codec only ever sees a value,
    never the slot it sits in. A round trip therefore canonicalises.
  - `?any` collapses onto `any`: nlohmann::json already carries null, so
    std::optional<LogosMap> would give the slot two spellings of empty.

The dispatch gate now admits a missing trailing optional argument and
materialises it as null, exactly the way a missing record field already was. A
method with no optional parameter emits the byte-identical gate it always did.

Header-first: `std::optional<T>` <-> `?T`, composing with records and
containers. `std::optional<std::optional<T>>` has NO LIDL type (three C++ states
over a two-state wire), so it maps down to `?T` — which makes the author's own
declaration stop compiling against the generated codec, deliberately — and says
so at derivation time instead of leaving a conversion error in generated code.

The Qt/Lp consumer surface is NOT fixed and does not pretend to be. The wrappers
real modules get come from legacy/main.cpp, where the AST is flattened to a
single Qt type-name string per slot before optionality could be seen; Qt has no
optional metatype, so `?T` lands on QVariant — the right shape (an invalid
QVariant is Qt's empty inhabitant) with no type. The generator now prints a Note
naming every flattened slot so an affected build is never silent, and
docs/project.md records exactly what a Qt consumer will still do with an
optional field.

Verified by output equivalence, not by a green build: the generator was built
before and after and run over every .lidl in the workspace plus the impl-header
fixtures, in cdylib, consumer-qt, consumer-lp, client and header-first modes.
428 of 465 artefacts are byte-identical; all 37 that differ belong to one of the
four contracts that declare an optional (the 38th path is the manifest). The
harness's sensitivity is pinned by a negative control: qt vs lp output differs
in 45 files. The emitted codec was additionally compiled under -Wall -Wextra and
run against the rules above — omission, absent==null, required-still-rejects,
present-but-wrong-still-fails, and canonicalising round trip.

Tests: 199 pass, 0 fail (180 before, 19 new).

Requires logos-lidl's optionality accessors and logos-protocol's
Codec<std::optional<T>>.

NOT FIXED, AND IT IS THE PATH THAT MATTERS MOST. The legacy interface-wrapper
path is untouched, and it is the one every real module builds through
(buildPlugin.nix:145 -> logos-cpp-generator --general-only). There the two
spellings still diverge:

  ? maybe: tstr   ->  QString maybe{};   __m.value("maybe").toString()
  maybe: ?tstr    ->  QVariant maybe{};  __m.value("maybe")

and --api-style lp diverges too, neither side being std::optional. So R3 holds
on the backends below and NOT on the Qt consumer a shipping module actually
gets. logos-chat-module -- the contract that prompted this work -- uses the
field-flag spelling, so it lands on the branch that silently defaults.

The cause is upstream of codegen: legacy/main.cpp's moduleRecordsToJson and
moduleMethodsToJson flatten every TypeExpr to a single Qt TYPE-NAME STRING, so
optionality (along with nesting, map key types and descriptions) is gone before
generator_lib.cpp sees it. Widening that interface is a larger change and is
deliberately not attempted here. The only R3 test on a Qt surface covers
lidl_gen_client.cpp, which is on no live build path.

* chore: re-pin logos-lidl to master for the optionality accessors

lidl_compat.h re-exports typeIsOptional / optionalValueType / fieldIsOptional /
fieldValueType / paramIsOptional / paramValueType, which landed in
logos-lidl#7. The pinned lidl predated it, so CI failed to compile.

logos-lidl 8c95d4f -> 35f33d8. Tests: 199 pass, 0 fail.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* chore: re-pin logos-protocol to master for Codec<std::optional<T>>

The generated record codecs emit Codec<std::optional<T>> for an optional
field; that specialisation landed in logos-protocol#37 and the pinned
protocol predated it.

Note this repo's own tests would NOT have caught the omission -- the
generator tests string-assert emitted text rather than compiling it, so a
missing codec specialisation only surfaces when a real module compiles
generated optional code (logos-test-modules' ext provider).

logos-protocol 4359557 -> 72754ab. Tests: 199 pass, 0 fail.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* test(doctests): override logos-lidl alongside every logos-cpp-sdk override

The doc-tests build downstream repos (logoscore-cli, capability_module,
accounts_module) with --override-input logos-cpp-sdk. Nix does not carry the
overridden input's OWN lock, so those builds got this branch's cpp-sdk source
while still resolving logos-lidl from their own, older locks. The shipped
share/lidl-frontend/lidl_compat.h then calls accessors that lidl does not
have:

  lidl_compat.h:46: error: 'paramValueType' has not been declared in 'lidl'
  lidl_compat.h:92: error: 'fieldValueType' was not declared in this scope

Every --override-input logos-cpp-sdk now has a matching
--override-input <same-path>/logos-cpp-sdk/logos-lidl.

This is specific to the override path. A normal consumer running
'nix flake update logos-cpp-sdk' inherits cpp-sdk's own lock, which pins the
lidl carrying these accessors, and is unaffected.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* test(doctests): move logos-lidl at the qt-sdk nodes, not under logos-cpp-sdk

The doc-tests failed to build logos-qt-generator:

  share/lidl-frontend/lidl_compat.h:46: error: 'paramValueType' has not been
  declared in 'lidl'

MECHANISM. This SDK installs cpp-generator/experimental/lidl_compat.h into
$out/share/lidl-frontend/, and logos-qt-sdk's logos-qt-generator *compiles*
that installed header against qt-sdk's OWN logos-lidl input. Under
logos-qt-sdk, logos-lidl is a SIBLING of logos-cpp-sdk, not a descendant:

  logos-qt-sdk
  |-- logos-cpp-sdk   <- --override-input moves this to the commit under test
  `-- logos-lidl      <- stays on qt-sdk's lock (8c95d4f), lacks the accessors

logos-logoscore-cli and logos-module-builder both declare
`logos-qt-sdk.inputs.logos-cpp-sdk.follows = "logos-cpp-sdk"` but no lidl
follows, so overriding the SDK hands qt-sdk a new lidl_compat.h next to its
old lidl. The failing derivation is logos-qt-generator — not anything in
logos-cpp-sdk, which is why the previous attempt aimed at the wrong node.

THE FIX is one `<path-to-logos-qt-sdk>/logos-lidl` override per qt-sdk node
that ends up on the SDK under test. A tree-walk over the resolved lock found
four in logoscore-cli's closure and two per module build; with the overrides
applied the walk reports zero remaining.

WHAT WAS REMOVED, and why it was doing nothing:

  * The `.../logos-cpp-sdk/logos-lidl` overrides added in bef3ef5 were no-ops.
    With only `--override-input logos-cpp-sdk <sha>`, that node's logos-lidl
    already resolves to 35f33d87 out of cpp-sdk's own lock — nix >= 2.26
    carries an overridden input's lock, and CI runs Determinate Nix. Verified
    by resolving the lock with and without them: byte-identical.
  * The `logos-module-client/...` overrides never matched anything. Nix says so
    out loud ("does not match any input"): logoscore-cli has no such root
    input; module-client only appears under logos-test-modules/, outside the
    runtime closure. The prose claiming it pins the SDK is corrected too.

cpp-sdk-concurrent-dispatch is fixed here as well — it failed the same way and
carried no lidl overrides at all.

VERIFIED locally against bef3ef5, the exact commit CI failed on:

  * accounts .lgx  -> exit 0, logos-accounts_module-module-lib.lgx (5,939,898 B)
  * logoscore CLI  -> exit 0, ./logos/bin/logoscore reports
                      "logos-cpp-sdk bef3ef57d3f489073672e70a786c550df7edd003"
  * negative control (same command minus the single qt-sdk lidl flag) fails
    with CI's exact derivation,
    /nix/store/pf96n2ldvhy6sq39ygkh5zdqx7dcn4df-logos-qt-generator-0.1.0.drv
  * no "does not match any input" warnings remain on any command

The durable fix is a one-line bump of logos-qt-sdk's own flake.lock logos-lidl
to master (logos-lidl#7 is purely additive: six new inline helpers, nothing
removed or renamed). Once qt-sdk carries it, every override added here can go.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-07-31 07:19:47 -03:00

557 lines
22 KiB
C++

// Code-generation tests for the cdylib backend's events sidecar.
//
// The sidecar is a Qt-FREE translation unit, so two classes of defect live
// here: dropping a payload (logos-cpp-sdk#99 — every `bstr` event argument was
// serialized as an empty tagged value), and emitting a Qt type into a TU that
// cannot compile one.
//
// These assert on generated source text. The bytes the emitted encoder actually
// produces are covered by value in tests/sdk/test_logos_json_bytes.cpp.
#include <gtest/gtest.h>
#include "lidl_gen_cdylib.h"
namespace {
TypeExpr prim(const char* name)
{
return {TypeExpr::Primitive, name, {}};
}
ParamDecl param(const char* name, const TypeExpr& type)
{
ParamDecl p;
p.name = name;
p.type = type;
return p;
}
ModuleDecl moduleWithEvent(const char* eventName, const std::vector<ParamDecl>& params)
{
ModuleDecl m;
m.name = "delivery_module";
EventDecl e;
e.name = eventName;
e.params = params;
m.events.push_back(e);
return m;
}
QString eventsSourceFor(const ModuleDecl& m)
{
return lidlMakeEventsSourceCdylib(m, "DeliveryModuleImpl", "delivery_module_plugin.h");
}
MethodDecl method(const char* name, const TypeExpr& returnType,
const std::vector<ParamDecl>& params)
{
MethodDecl md;
md.name = name;
md.returnType = returnType;
md.params = params;
return md;
}
ModuleDecl moduleWithMethod(const MethodDecl& md)
{
ModuleDecl m;
m.name = "delivery_module";
m.methods.push_back(md);
return m;
}
QString implSourceFor(const ModuleDecl& m)
{
return lidlMakeModuleImplExports(m, "DeliveryModuleImpl", "delivery_module_plugin.h");
}
} // namespace
// logos-cpp-sdk#99: `payload` was replaced by an empty tagged value, so a module
// could emit real bytes and every consumer still received zero of them.
TEST(LidlGenCdylib, BinaryEventPayloadUsesCanonicalBytesEncoding)
{
const ModuleDecl m = moduleWithEvent("messageReceived", {
param("messageHash", prim("tstr")),
param("contentTopic", prim("tstr")),
param("payload", prim("bstr")),
param("timestamp", prim("int")),
});
const QString source = eventsSourceFor(m);
// The real argument is serialized, through the canonical encoder...
EXPECT_TRUE(source.contains("args.push_back(lidlBytesToJson(payload));"));
EXPECT_TRUE(source.contains("std::string lidlB64UrlEncode"));
EXPECT_TRUE(source.contains("nlohmann::json lidlBytesToJson"));
// ...and the empty tagged value is gone.
EXPECT_FALSE(source.contains("nlohmann::json{{\"_bytes\", \"\"}}"));
// The other parameters are still passed straight through.
EXPECT_TRUE(source.contains("args.push_back(messageHash);"));
EXPECT_TRUE(source.contains("args.push_back(timestamp);"));
// Bytes are taken by const-ref, matching the author's logos_events: block.
EXPECT_TRUE(source.contains("const std::vector<uint8_t>& payload"));
}
// The encoder is only needed by modules that actually emit binary payloads.
// Emitted unconditionally it is an unused static function in every other
// module's sidecar (-Wunused-function).
TEST(LidlGenCdylib, BytesEncoderOmittedWhenNoEventCarriesBytes)
{
const ModuleDecl m = moduleWithEvent("fault", {
param("code", prim("int")),
param("message", prim("tstr")),
param("fatal", prim("bool")),
});
const QString source = eventsSourceFor(m);
EXPECT_FALSE(source.contains("lidlB64UrlEncode"));
EXPECT_FALSE(source.contains("lidlBytesToJson"));
EXPECT_TRUE(source.contains("args.push_back(code);"));
}
// The sidecar is compiled into the module's Qt-free cdylib, so a JSON payload
// has to be spelled as its nlohmann alias. Emitted as QVariantMap it does not
// compile at all.
TEST(LidlGenCdylib, JsonEventPayloadIsQtFree)
{
ModuleDecl m;
m.name = "state_module";
EventDecl e;
e.name = "stateChanged";
e.params.push_back(param("key", prim("tstr")));
e.params.push_back(param("state",
TypeExpr{TypeExpr::Map, "", {prim("tstr"), prim("any")}}));
m.events.push_back(e);
const QString source =
lidlMakeEventsSourceCdylib(m, "StateModuleImpl", "state_module_plugin.h");
EXPECT_TRUE(source.contains("const LogosMap& state"));
EXPECT_TRUE(source.contains("#include <logos_json.h>"));
// No Qt type may appear anywhere in a Qt-free TU.
EXPECT_FALSE(source.contains("QVariant"));
}
// `[bstr]` is in the supported subset: each element carries the canonical
// tagged form, so a module can take or return a list of blobs (e.g. a program
// plus its dependency ELFs) instead of hand-encoding them as hex strings.
//
// #111 reached this with a dedicated depth-1 list codec; the gate now RECURSES
// and the generated Codec's full specialization for std::vector<uint8_t> beats
// its generic vector rule, so the same mechanism covers [bstr], [[bstr]] and
// {tstr: [bstr]}. The assertions moved to that mechanism; what they pin did not.
TEST(LidlGenCdylib, ArrayOfBytesEventParamIsEligibleAndTagsEachElement)
{
const ModuleDecl m = moduleWithEvent("batchReceived", {
param("payloads", TypeExpr{TypeExpr::Array, "", {prim("bstr")}}),
});
QString error;
EXPECT_TRUE(lidlCdylibSupported(m, &error)) << error.toStdString();
const QString source = eventsSourceFor(m);
// Spelled Qt-free and encoded through the codec, so each element keeps its
// canonical tag instead of becoming a plain array of numbers.
EXPECT_TRUE(source.contains("std::vector<std::vector<uint8_t>>")) << source.toStdString();
EXPECT_TRUE(source.contains("logos::toJson<std::vector<std::vector<uint8_t>>>(payloads)"))
<< source.toStdString();
// From #111, still exactly right: Qt-free, and taken by const-ref like the
// other composite payloads.
EXPECT_TRUE(source.contains("const std::vector<std::vector<uint8_t>>& payloads"))
<< source.toStdString();
EXPECT_FALSE(source.contains("QVariant")) << source.toStdString();
}
// Ported from #111. Its assertions named that PR's depth-1 helpers
// (lidlBytesListFromJson / lidlBytesListToJson); the generated Codec subsumes
// them, so the assertions moved to the codec while what they pin — per-element
// tagging, and never nlohmann's blanket container conversion — did not.
TEST(LidlGenCdylib, ArrayOfBytesMethodParamDecodesPerElement)
{
const ModuleDecl m = moduleWithMethod(method("send", prim("tstr"), {
param("program_elf", prim("bstr")),
param("program_dependencies", TypeExpr{TypeExpr::Array, "", {prim("bstr")}}),
}));
QString error;
ASSERT_TRUE(lidlCdylibSupported(m, &error)) << error.toStdString();
const QString source = implSourceFor(m);
EXPECT_TRUE(source.contains("logos::fromJson<std::vector<std::vector<uint8_t>>>("))
<< source.toStdString();
// The scalar param decodes leniently too — and now through the SAME
// function as the nested one. It used to be a separate emitted helper, so a
// scalar bstr accepted a plain string while a [bstr] element rejected it:
// echoBytes("hi") worked and echoBytesList(["hi"]) threw, inside one module.
EXPECT_TRUE(source.contains("logos::bytesFromJsonLenient(")) << source.toStdString();
// nlohmann's blanket container decode must not be used for this type: it
// refuses a tagged object and would silently accept a raw number array,
// skipping the base64 decode entirely.
EXPECT_FALSE(source.contains(".get<std::vector<std::vector<uint8_t>>>()"))
<< source.toStdString();
}
// Ported from #111: a `[bstr]` RETURN tags each element.
// nlohmann::json(std::vector<std::vector<uint8_t>>) would emit nested number
// arrays, which no consumer decodes as bytes.
TEST(LidlGenCdylib, ArrayOfBytesReturnTagsEachElement)
{
const ModuleDecl m = moduleWithMethod(
method("fetchAll", TypeExpr{TypeExpr::Array, "", {prim("bstr")}}, {}));
QString error;
ASSERT_TRUE(lidlCdylibSupported(m, &error)) << error.toStdString();
const QString source = implSourceFor(m);
EXPECT_TRUE(source.contains("logos::toJson<std::vector<std::vector<uint8_t>>>("))
<< source.toStdString();
EXPECT_FALSE(source.contains("nlohmann::json(result)")) << source.toStdString();
}
// #111 gated its list encoder so a module that never carries `[bstr]` did not
// gain an unused static function. The generic codec is a TEMPLATE — it only
// instantiates where used — so that hazard is gone and there is no dedicated
// list encoder to omit. What still needs gating is the SCALAR encoder, and it
// still is; this pins both halves so neither regresses.
TEST(LidlGenCdylib, NoDedicatedListEncoderAndTheScalarOneStaysGated)
{
const ModuleDecl noBytes = moduleWithEvent("fault", {
param("code", prim("int")),
param("message", prim("tstr")),
});
const QString plain = eventsSourceFor(noBytes);
EXPECT_FALSE(plain.contains("lidlBytesToJson")) << plain.toStdString();
EXPECT_FALSE(plain.contains("lidlBytesListToJson")) << plain.toStdString();
const ModuleDecl withList = moduleWithEvent("batchReceived", {
param("payloads", TypeExpr{TypeExpr::Array, "", {prim("bstr")}}),
});
const QString listed = eventsSourceFor(withList);
// The list rides the codec; no bespoke list encoder is emitted at all.
EXPECT_FALSE(listed.contains("lidlBytesListToJson")) << listed.toStdString();
EXPECT_TRUE(listed.contains("logos::toJson<std::vector<std::vector<uint8_t>>>("))
<< listed.toStdString();
}
// The gate recurses, so what it refuses is now a property of the leaf. A map
// with a non-tstr key has no C++ spelling (the codec spells a map as
// std::map<std::string, T>) and must still be refused BY NAME — it used to be
// admitted by a blanket `return true` for any map and then silently flattened
// to an untyped LogosMap, losing the key type.
TEST(LidlGenCdylib, NonStringMapKeyIsRejected)
{
ModuleDecl m;
m.name = "k_module";
MethodDecl md;
md.name = "takeOddMap";
md.returnType = prim("tstr");
ParamDecl p;
p.name = "m";
p.type = TypeExpr{TypeExpr::Map, "", {prim("int"), prim("tstr")}};
md.params.push_back(p);
m.methods.push_back(md);
QString error;
EXPECT_FALSE(lidlCdylibSupported(m, &error));
EXPECT_TRUE(error.contains("takeOddMap")) << error.toStdString();
}
// A record the contract declares is admitted and spelled as its struct; an
// UNDECLARED Named type is not. `void` is the reason that distinction has to
// exist — it is not a LIDL builtin, so `-> void` arrives as Named("void").
TEST(LidlGenCdylib, OnlyDeclaredRecordsAreRecords)
{
ModuleDecl m;
m.name = "r_module";
TypeDecl rec;
rec.name = "Blob";
FieldDecl f;
f.name = "payload";
f.type = prim("bstr");
rec.fields = {f};
m.types.push_back(rec);
MethodDecl good;
good.name = "echoBlob";
good.returnType = TypeExpr{TypeExpr::Named, "Blob", {}};
ParamDecl gp; gp.name = "v"; gp.type = TypeExpr{TypeExpr::Named, "Blob", {}};
good.params.push_back(gp);
m.methods.push_back(good);
QString error;
EXPECT_TRUE(lidlCdylibSupported(m, &error)) << error.toStdString();
// The struct and its codec specialization are emitted.
const QString types = lidlMakeTypesHeaderCdylib(m);
// Forward-declared, not defined: the struct is the author's (the contract
// was derived from that very declaration), so emitting it again would be a
// redefinition.
EXPECT_TRUE(types.contains("struct Blob;")) << types.toStdString();
EXPECT_FALSE(types.contains("struct Blob {")) << types.toStdString();
// Specialized into logos::detail, beside the primary template it specializes,
// and spelled ::Blob because the author's struct is at global scope while
// this is namespace logos::detail.
EXPECT_TRUE(types.contains("template <> struct Codec<::Blob, void>")) << types.toStdString();
EXPECT_TRUE(types.contains("namespace logos { namespace detail {")) << types.toStdString();
// The bstr field goes through the bytes codec, not nlohmann's array-of-numbers.
EXPECT_TRUE(types.contains("Codec<std::vector<uint8_t>>::to(v.payload)")) << types.toStdString();
// The generic half is NOT emitted any more — it comes from logos_codec.h.
EXPECT_TRUE(types.contains("#include <logos_codec.h>")) << types.toStdString();
EXPECT_FALSE(types.contains("namespace logos_gen")) << types.toStdString();
EXPECT_FALSE(types.contains("struct Codec<int64_t>")) << types.toStdString();
// An undeclared Named type is NOT a record and stays refused.
MethodDecl bad;
bad.name = "takeGhost";
bad.returnType = prim("tstr");
ParamDecl bp; bp.name = "g"; bp.type = TypeExpr{TypeExpr::Named, "Ghost", {}};
bad.params.push_back(bp);
m.methods.push_back(bad);
EXPECT_FALSE(lidlCdylibSupported(m, &error));
EXPECT_TRUE(error.contains("takeGhost")) << error.toStdString();
}
// The supported scalar / bytes payloads stay eligible.
TEST(LidlGenCdylib, SupportedEventParamsRemainEligible)
{
const ModuleDecl m = moduleWithEvent("messageReceived", {
param("messageHash", prim("tstr")),
param("payload", prim("bstr")),
param("timestamp", prim("int")),
});
QString error;
EXPECT_TRUE(lidlCdylibSupported(m, &error)) << error.toStdString();
}
// ---------------------------------------------------------------------------
// Optionality — `?T` on the Qt-free cdylib surface.
//
// Two-state (a value of T, or empty), std::optional<T>, and the wire rule that
// depends on the SLOT: empty omits the key where the slot is named (a record
// field) and is spelled null where it is positional (an argument, a return, an
// event parameter — those have no key to omit and their arity must not change).
// ---------------------------------------------------------------------------
TypeExpr opt(const TypeExpr& inner)
{
return {TypeExpr::Optional, "", {inner}};
}
FieldDecl field(const char* name, const TypeExpr& type)
{
FieldDecl f;
f.name = name;
f.type = type;
return f;
}
// `?T` used to be a HARD REJECT — "module not cdylib-eligible" — so nothing
// downstream could even be reached. The gate opens exactly as far as the value
// type allows.
TEST(LidlGenCdylib, OptionalIsEligibleWhenItsValueTypeIs)
{
ModuleDecl m;
m.name = "o_module";
m.methods.push_back(method("echoOptional", opt(prim("tstr")),
{param("v", opt(prim("tstr")))}));
m.methods.push_back(method("nested", opt(TypeExpr{TypeExpr::Array, "", {prim("bstr")}}),
{param("v", TypeExpr{TypeExpr::Array, "", {opt(prim("int"))}})}));
QString error;
EXPECT_TRUE(lidlCdylibSupported(m, &error)) << error.toStdString();
}
// `result` and `void` are return-only spellings, and neither can be optional:
// void is the absence of a value, and result already carries its own
// success/error discriminant. The value type is checked as a non-return
// position, which is what makes both fall out.
TEST(LidlGenCdylib, OptionalResultAndVoidAreRejected)
{
for (const char* n : {"result", "void"}) {
ModuleDecl m;
m.name = "o_module";
m.methods.push_back(method("bad", opt(prim(n)), {}));
QString error;
EXPECT_FALSE(lidlCdylibSupported(m, &error)) << n;
EXPECT_TRUE(error.contains("bad")) << error.toStdString();
}
}
// R3. `? name: T` (the field flag) and `name: ?T` (the type kind) are the same
// declaration and MUST emit byte-identical code. Nothing enforced that before —
// a backend reading only one of the two would have silently disagreed with the
// next one to try.
TEST(LidlGenCdylib, BothOptionalSpellingsEmitIdenticalCode)
{
auto moduleWithField = [](const FieldDecl& f) {
ModuleDecl m;
m.name = "o_module";
TypeDecl t;
t.name = "Opt";
t.fields = {f};
m.types.push_back(t);
return m;
};
FieldDecl flagged = field("maybe", prim("tstr"));
flagged.optional = true; // `? maybe: tstr`
const FieldDecl typed = field("maybe", opt(prim("tstr"))); // `maybe: ?tstr`
const QString a = lidlMakeTypesHeaderCdylib(moduleWithField(flagged));
const QString b = lidlMakeTypesHeaderCdylib(moduleWithField(typed));
EXPECT_EQ(a, b) << a.toStdString() << "\n---\n" << b.toStdString();
EXPECT_TRUE(a.contains("std::optional<std::string>")) << a.toStdString();
}
// R2, named slot: empty OMITS the key. Writing null instead would be the
// positional spelling in a slot that has a name — and Codec<std::optional<T>>
// cannot do this itself, because a codec only ever sees a value, never the slot.
TEST(LidlGenCdylib, OptionalRecordFieldOmitsTheKeyWhenEmpty)
{
ModuleDecl m;
m.name = "o_module";
TypeDecl t;
t.name = "Opt";
t.fields = {field("required", prim("tstr")), field("maybe", opt(prim("tstr")))};
m.types.push_back(t);
const QString types = lidlMakeTypesHeaderCdylib(m);
EXPECT_TRUE(types.contains("if (v.maybe.has_value())")) << types.toStdString();
EXPECT_TRUE(types.contains("out[\"maybe\"] = Codec<std::string>::to(*v.maybe);"))
<< types.toStdString();
// Decode needs no optional branch: an absent key is ALREADY materialised as
// null right there, so absent and explicit null reach the codec
// indistinguishable — nullopt in an optional field, still an error in a
// required one.
EXPECT_TRUE(types.contains("out.maybe = Codec<std::optional<std::string>>::from("))
<< types.toStdString();
EXPECT_TRUE(types.contains("j.contains(\"maybe\") ? j.at(\"maybe\") : nlohmann::json()"))
<< types.toStdString();
// The required field is untouched by any of this.
EXPECT_TRUE(types.contains("out[\"required\"] = Codec<std::string>::to(v.required);"))
<< types.toStdString();
EXPECT_TRUE(types.contains("#include <optional>")) << types.toStdString();
}
// A contract with no optional keeps its generated output byte-for-byte, down to
// the include list — every cpp-sdk change rebuilds the whole module graph, so a
// gratuitous diff here is a rebuild of everything.
TEST(LidlGenCdylib, NoOptionalMeansNoOptionalInclude)
{
ModuleDecl m;
m.name = "o_module";
TypeDecl t;
t.name = "Plain";
t.fields = {field("id", prim("tstr"))};
m.types.push_back(t);
EXPECT_FALSE(lidlMakeTypesHeaderCdylib(m).contains("#include <optional>"));
}
// R2, positional slot: arity never changes on the way OUT, but absent and null
// are the same state coming IN — so the gate admits a missing trailing optional
// and materialises it as null, exactly the way a missing record field already is.
TEST(LidlGenCdylib, OptionalArgumentMayBeAbsentOrNull)
{
ModuleDecl m;
m.name = "o_module";
m.methods.push_back(method("f", prim("tstr"),
{param("required", prim("tstr")),
param("maybe", opt(prim("tstr")))}));
const QString src = lidlMakeModuleImplExports(m, "OImpl", "o_impl.h");
EXPECT_TRUE(src.contains("if (args.size() < 1) return nullptr;")) << src.toStdString();
EXPECT_TRUE(src.contains("(args.size() > 1 ? args.at(1) : nlohmann::json())"))
<< src.toStdString();
EXPECT_TRUE(src.contains("logos::fromJson<std::optional<std::string>>"))
<< src.toStdString();
// The REQUIRED argument keeps the hard gate and the plain accessor.
EXPECT_TRUE(src.contains("logos::fromJson<std::string>(args.at(0), \"arg0\")"))
<< src.toStdString();
}
// ...and a method with no optional parameter emits the gate it always did.
TEST(LidlGenCdylib, RequiredOnlyArityGateIsUnchanged)
{
ModuleDecl m;
m.name = "o_module";
m.methods.push_back(method("f", prim("tstr"),
{param("a", prim("tstr")), param("b", prim("tstr"))}));
const QString src = lidlMakeModuleImplExports(m, "OImpl", "o_impl.h");
EXPECT_TRUE(src.contains("if (args.size() < 2) return nullptr;")) << src.toStdString();
EXPECT_FALSE(src.contains("args.size() > ")) << src.toStdString();
}
// R4. Optional widens the accepted domain by exactly ONE inhabitant (empty); a
// present value is still decoded as T. For `bstr` that has to be the LENIENT
// decode a bare `bstr` argument gets, or the identical value would be accepted
// in a required slot and rejected in an optional one.
TEST(LidlGenCdylib, OptionalBytesArgumentKeepsTheLenientDecode)
{
ModuleDecl m;
m.name = "o_module";
m.methods.push_back(method("f", prim("bool"), {param("v", opt(prim("bstr")))}));
const QString src = lidlMakeModuleImplExports(m, "OImpl", "o_impl.h");
EXPECT_TRUE(src.contains("logos::bytesFromJsonLenient")) << src.toStdString();
EXPECT_TRUE(src.contains(".is_null() ? std::optional<std::vector<uint8_t>>()"))
<< src.toStdString();
}
// `?any` collapses onto `any`. nlohmann::json already HAS null among its
// inhabitants, so std::optional<LogosMap> would give the slot two spellings of
// empty — three-state, which is the one thing `?T` may never be.
TEST(LidlGenCdylib, OptionalAnyCollapsesOntoAny)
{
ModuleDecl m;
m.name = "o_module";
TypeDecl t;
t.name = "Loose";
t.fields = {field("blob", opt(prim("any")))};
m.types.push_back(t);
m.methods.push_back(method("f", prim("bool"), {param("v", opt(prim("any")))}));
QString error;
ASSERT_TRUE(lidlCdylibSupported(m, &error)) << error.toStdString();
const QString types = lidlMakeTypesHeaderCdylib(m);
EXPECT_TRUE(types.contains("Codec<LogosMap>::to(v.blob)")) << types.toStdString();
EXPECT_FALSE(types.contains("std::optional<LogosMap>")) << types.toStdString();
const QString src = lidlMakeModuleImplExports(m, "OImpl", "o_impl.h");
EXPECT_FALSE(src.contains("std::optional<LogosMap>")) << src.toStdString();
}
// An event parameter is a POSITIONAL slot: empty is null, and the argument list
// keeps its length. It is also taken by const reference, like every other
// non-scalar, so the generated definition matches the author's declaration in
// the `logos_events:` block.
TEST(LidlGenCdylib, OptionalEventParamIsConstRefAndNullWhenEmpty)
{
const ModuleDecl m = moduleWithEvent("changed", {
param("name", prim("tstr")),
param("instance", opt(prim("tstr"))),
});
const QString src = eventsSourceFor(m);
EXPECT_TRUE(src.contains("const std::optional<std::string>& instance"))
<< src.toStdString();
EXPECT_TRUE(src.contains("args.push_back(logos::toJson<std::optional<std::string>>(instance));"))
<< src.toStdString();
EXPECT_TRUE(src.contains("#include <optional>")) << src.toStdString();
}