summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRico Tzschichholz <ricotz@ubuntu.com>2021-12-20 20:40:30 +0100
committerRico Tzschichholz <ricotz@ubuntu.com>2021-12-21 07:42:46 +0100
commita81bc7aa6163eaa7530b1a179dcfbf9de330c8f4 (patch)
treed46813a7393f10a7f508df8ae08ec889ec204198
parent4c2768496938ec2d9c82377a138efd2cc42bbafe (diff)
downloadvala-a81bc7aa6163eaa7530b1a179dcfbf9de330c8f4.tar.gz
vala: Correctly replace "in" expression in pre-/postconditions of method
Fixes https://gitlab.gnome.org/GNOME/vala/issues/1269
-rw-r--r--tests/Makefile.am1
-rw-r--r--tests/methods/prepostconditions-contains.c-expected83
-rw-r--r--tests/methods/prepostconditions-contains.vala13
-rw-r--r--vala/valamethod.vala17
4 files changed, 114 insertions, 0 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am
index ef7444ba0..b3e51dc7b 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -189,6 +189,7 @@ TESTS = \
methods/preconditions-temp-variables.vala \
methods/prepostconditions.vala \
methods/prepostconditions-captured.vala \
+ methods/prepostconditions-contains.vala \
methods/postconditions.vala \
methods/postconditions-temp-variables.vala \
methods/return-unowned-delegate.vala \
diff --git a/tests/methods/prepostconditions-contains.c-expected b/tests/methods/prepostconditions-contains.c-expected
new file mode 100644
index 000000000..f1cd71e0c
--- /dev/null
+++ b/tests/methods/prepostconditions-contains.c-expected
@@ -0,0 +1,83 @@
+/* methods_prepostconditions_contains.c generated by valac, the Vala compiler
+ * generated from methods_prepostconditions_contains.vala, do not modify */
+
+#include <stdlib.h>
+#include <string.h>
+#include <glib.h>
+
+#if !defined(VALA_EXTERN)
+#if defined(_MSC_VER)
+#define VALA_EXTERN __declspec(dllexport) extern
+#elif __GNUC__ >= 4
+#define VALA_EXTERN __attribute__((visibility("default"))) extern
+#else
+#define VALA_EXTERN extern
+#endif
+#endif
+
+#define _g_free0(var) (var = (g_free (var), NULL))
+#define _vala_assert(expr, msg) if G_LIKELY (expr) ; else g_assertion_message_expr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, msg);
+#define _vala_return_if_fail(expr, msg) if G_LIKELY (expr) ; else { g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, msg); return; }
+#define _vala_return_val_if_fail(expr, msg, val) if G_LIKELY (expr) ; else { g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, msg); return val; }
+#define _vala_warn_if_fail(expr, msg) if G_LIKELY (expr) ; else g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, msg);
+
+VALA_EXTERN void foo (const gchar* s);
+static gboolean _vala_string_array_contains (const gchar* * stack,
+ gssize stack_length,
+ const const gchar* needle);
+VALA_EXTERN gchar* bar (void);
+static void _vala_main (void);
+
+const gchar* array[3] = {"foo", "bar", "manam"};
+
+static gboolean
+_vala_string_array_contains (const gchar* * stack,
+ gssize stack_length,
+ const const gchar* needle)
+{
+ gssize i;
+ for (i = 0; i < stack_length; i++) {
+ if (g_strcmp0 (stack[i], needle) == 0) {
+ return TRUE;
+ }
+ }
+ return FALSE;
+}
+
+void
+foo (const gchar* s)
+{
+ g_return_if_fail (s != NULL);
+ _vala_return_if_fail (_vala_string_array_contains (array, G_N_ELEMENTS (array), s), "s in array");
+}
+
+gchar*
+bar (void)
+{
+ gchar* _tmp0_;
+ gchar* result = NULL;
+ _tmp0_ = g_strdup ("manam");
+ result = _tmp0_;
+ _vala_warn_if_fail (_vala_string_array_contains (array, G_N_ELEMENTS (array), result), "result in array");
+ return result;
+}
+
+static void
+_vala_main (void)
+{
+ gchar* _tmp0_;
+ gchar* _tmp1_;
+ foo ("bar");
+ _tmp0_ = bar ();
+ _tmp1_ = _tmp0_;
+ _g_free0 (_tmp1_);
+}
+
+int
+main (int argc,
+ char ** argv)
+{
+ _vala_main ();
+ return 0;
+}
+
diff --git a/tests/methods/prepostconditions-contains.vala b/tests/methods/prepostconditions-contains.vala
new file mode 100644
index 000000000..9135da3c7
--- /dev/null
+++ b/tests/methods/prepostconditions-contains.vala
@@ -0,0 +1,13 @@
+const string[] array = { "foo", "bar", "manam" };
+
+void foo (string s) requires (s in array) {
+}
+
+string bar () ensures (result in array) {
+ return "manam";
+}
+
+void main () {
+ foo ("bar");
+ bar ();
+}
diff --git a/vala/valamethod.vala b/vala/valamethod.vala
index 5eb1b9a95..27e417291 100644
--- a/vala/valamethod.vala
+++ b/vala/valamethod.vala
@@ -577,6 +577,23 @@ public class Vala.Method : Subroutine, Callable {
}
}
+ public override void replace_expression (Expression old_node, Expression new_node) {
+ if (preconditions != null) {
+ var index = preconditions.index_of (old_node);
+ if (index >= 0) {
+ preconditions[index] = new_node;
+ new_node.parent_node = this;
+ }
+ }
+ if (postconditions != null) {
+ var index = postconditions.index_of (old_node);
+ if (index >= 0) {
+ postconditions[index] = new_node;
+ new_node.parent_node = this;
+ }
+ }
+ }
+
public override void replace_type (DataType old_type, DataType new_type) {
if (base_interface_type == old_type) {
base_interface_type = new_type;