diff --git a/include/null.h b/include/null.h index 4db3f72c..6b8e056c 100644 --- a/include/null.h +++ b/include/null.h @@ -183,17 +183,20 @@ namespace hx template struct Null { - inline Null() : isNull(true) { } + // A null leaves value cleared, not unset: a compiler may read it when it + // computes Default(), as GCC at -O2 turns `isNull ? true : value` into + // `isNull | value`, and an omitted Bool argument came out as stack bytes + inline Null() : isNull(true), value() { } inline Null(const Null &inOther) : isNull(inOther.isNull), value(inOther.value) { } inline Null(const T& inVal) : isNull(false), value(inVal) { } - inline Null(const null &) : isNull(true) { } - inline Null(const Dynamic &inVal) + inline Null(const null &) : isNull(true), value() { } + inline Null(const Dynamic &inVal) : value() { isNull = null() == inVal; if (!isNull) value = inVal; } - inline Null(const cpp::Variant &inVal) + inline Null(const cpp::Variant &inVal) : value() { isNull = inVal.isNull(); if (!isNull) diff --git a/test/regression/NullDefaultArgument/Main.hx b/test/regression/NullDefaultArgument/Main.hx new file mode 100644 index 00000000..0c33ea71 --- /dev/null +++ b/test/regression/NullDefaultArgument/Main.hx @@ -0,0 +1,44 @@ +// A Bool argument with a default, left out, comes as an hx::Null built +// from null. Its value used to be left unset: GCC at -O2 can turn +// `isNull ? true : value` into `isNull | value`, which then read the bytes the +// caller's frame held, and an omitted `compress:Bool = true` arrived as 23. +// Here each kind of Null is built over bytes that are not a bool, so the value +// it leaves shows whatever the optimizer does with it. +@:cppFileCode(' +#include + +template +static int valueOver(F make) +{ + alignas(hx::Null) unsigned char buffer[sizeof(hx::Null)]; + // Through volatile, or the compiler drops the stores, as the bytes of a new object are indeterminate + volatile unsigned char *fill = buffer; + for (int i = 0; i < (int)sizeof(buffer); i++) + fill[i] = 0xFE; + hx::Null *built = make(buffer); + const volatile unsigned char *value = (const volatile unsigned char *)&built->value; + int result = 0; + for (int i = 0; i < (int)sizeof(T); i++) + result |= value[i]; + return result; +} +') +class Main { + static function report(what:String, value:Int):Void { + Sys.println(what + ": " + (value == 0 ? "cleared" : "left as " + value)); + } + + static function main() { + report("Null()", untyped __cpp__("valueOver([](void *at) { return new (at) hx::Null(); })")); + report("Null(null)", untyped __cpp__("valueOver([](void *at) { return new (at) hx::Null(null()); })")); + report("Null(null Dynamic)", untyped __cpp__("valueOver([](void *at) { return new (at) hx::Null(Dynamic()); })")); + report("Null(null Variant)", untyped __cpp__("valueOver([](void *at) { return new (at) hx::Null(cpp::Variant()); })")); + report("Null(null)", untyped __cpp__("valueOver([](void *at) { return new (at) hx::Null(null()); })")); + report("Null(null)", untyped __cpp__("valueOver([](void *at) { return new (at) hx::Null(null()); })")); + Sys.println("default true: " + defaulted()); + } + + static function defaulted(flag:Bool = true):Bool { + return flag; + } +} diff --git a/test/regression/NullDefaultArgument/build.hxml b/test/regression/NullDefaultArgument/build.hxml new file mode 100644 index 00000000..b484e67e --- /dev/null +++ b/test/regression/NullDefaultArgument/build.hxml @@ -0,0 +1,2 @@ +-m Main +--cpp bin diff --git a/test/regression/NullDefaultArgument/stdout.txt b/test/regression/NullDefaultArgument/stdout.txt new file mode 100644 index 00000000..e0e56962 --- /dev/null +++ b/test/regression/NullDefaultArgument/stdout.txt @@ -0,0 +1,7 @@ +Null(): cleared +Null(null): cleared +Null(null Dynamic): cleared +Null(null Variant): cleared +Null(null): cleared +Null(null): cleared +default true: true