The code generated by the coalesce expression could lead to stale
pointers to the stack if the right-side expression is an immediate value
(such as an integer literal or a struct).
Fixes https://gitlab.gnome.org/GNOME/vala/issues/893
control-flow/break.vala \
control-flow/break-invalid.test \
control-flow/coalesce-reference-transfer.vala \
+ control-flow/coalesce-right-value.vala \
control-flow/continue-invalid.test \
control-flow/double-catch.test \
control-flow/expressions-conditional.vala \
--- /dev/null
+struct Foo {
+ public int i;
+}
+
+void main () {
+ {
+ int? null_int = null;
+ int i = null_int ?? 42;
+ assert (i == 42);
+ }
+ {
+ Foo? null_foo = null;
+ Foo right_foo = { 42 };
+ Foo foo = null_foo ?? right_foo;
+ assert (foo.i == 42);
+ }
+}
local_type = right.value_type.copy ();
}
+ if (local_type != null && right.value_type is ValueType && !right.value_type.nullable) {
+ // immediate values in the right expression must always be boxed,
+ // otherwise the local variable may receive a stale pointer to the stack
+ local_type.value_owned = true;
+ }
+
var local = new LocalVariable (local_type, get_temp_name (), left, source_reference);
var decl = new DeclarationStatement (local, source_reference);