diff options
author | wxx <769218589@qq.com> | 2021-12-01 02:54:04 +0800 |
---|---|---|
committer | Rico Tzschichholz <ricotz@ubuntu.com> | 2022-01-26 14:31:28 +0100 |
commit | f853104ebf94ce1f72ab408dc5d5b50c81b69d51 (patch) | |
tree | 4f7d2baae86f7208f116c51f2fa566b7a88941f1 /vala | |
parent | 224051f9c55ac1d9f71efe9a29dfac5d7c02bedf (diff) | |
download | vala-f853104ebf94ce1f72ab408dc5d5b50c81b69d51.tar.gz |
vala: Transform assignment of an array element as needed
Fixes https://gitlab.gnome.org/GNOME/vala/issues/889
Fixes https://gitlab.gnome.org/GNOME/vala/issues/1258
Diffstat (limited to 'vala')
-rw-r--r-- | vala/valaassignment.vala | 35 |
1 files changed, 28 insertions, 7 deletions
diff --git a/vala/valaassignment.vala b/vala/valaassignment.vala index 8c0948ef3..150c34723 100644 --- a/vala/valaassignment.vala +++ b/vala/valaassignment.vala @@ -229,13 +229,36 @@ public class Vala.Assignment : Expression { } unowned MemberAccess? ma = left as MemberAccess; - if (operator != AssignmentOperator.SIMPLE && ma != null - && !(left.value_type.is_non_null_simple_type () && ma.symbol_reference is LocalVariable)) { + unowned ElementAccess? ea = left as ElementAccess; + bool transform_assignment = false; + + if (ma != null && !(ma.symbol_reference is LocalVariable)) { + transform_assignment = true; + } else if (left.value_type != null && !left.value_type.is_non_null_simple_type ()) { + transform_assignment = true; + } else if (ea != null && ea.container.value_type is ArrayType) { + // check if the left is an array and its element is non-null simple type + unowned ArrayType array_type = (ArrayType) ea.container.value_type; + transform_assignment = !array_type.element_type.is_non_null_simple_type (); + } + + if ((operator != AssignmentOperator.SIMPLE) && transform_assignment) { // transform into simple assignment // FIXME: only do this if the backend doesn't support // the assignment natively - - var old_value = new MemberAccess (ma.inner, ma.member_name, source_reference); + Expression old_value = null; + + if (ma != null) { + old_value = new MemberAccess (ma.inner, ma.member_name, left.source_reference); + } else if (ea !=null) { + old_value = new ElementAccess (ea.container, left.source_reference); + var indices = ea.get_indices (); + foreach (var index in indices) { + ((ElementAccess) old_value).append_index (index); + } + } else { + assert_not_reached (); + } BinaryOperator bop; @@ -361,9 +384,7 @@ public class Vala.Assignment : Expression { } } } - } else if (left is ElementAccess) { - unowned ElementAccess ea = (ElementAccess) left; - + } else if (ea != null) { if (!right.value_type.compatible (left.value_type)) { error = true; Report.error (source_reference, "Assignment: Cannot convert from `%s' to `%s'", right.value_type.to_string (), left.value_type.to_string ()); |