]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
vala: Report error for yield statements without async context
authorRico Tzschichholz <ricotz@ubuntu.com>
Thu, 14 Mar 2019 08:16:51 +0000 (09:16 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Sun, 17 Mar 2019 18:40:19 +0000 (19:40 +0100)
tests/Makefile.am
tests/semantic/yield-statement-requires-async-context.test [new file with mode: 0644]
vala/valayieldstatement.vala

index 41481e7e85f2b738de7b8986d40873260beb3bfa..f0000a94b3aa29cfeb22fb0978f49b7fc19a4ea2 100644 (file)
@@ -715,6 +715,7 @@ TESTS = \
        semantic/yield-call-requires-async-method.test \
        semantic/yield-creation-requires-async-context.test \
        semantic/yield-creation-requires-async-method.test \
+       semantic/yield-statement-requires-async-context.test \
        $(NULL)
 
 NON_NULL_TESTS = \
diff --git a/tests/semantic/yield-statement-requires-async-context.test b/tests/semantic/yield-statement-requires-async-context.test
new file mode 100644 (file)
index 0000000..8dd0395
--- /dev/null
@@ -0,0 +1,5 @@
+Invalid Code
+
+void main () {
+       yield;
+}
index 795de27610417c7cc3815de08e6e1c8c4673c76d..91d8e7f9f67acdea4514c7dbb2644a84ab38a21d 100644 (file)
@@ -35,6 +35,21 @@ public class Vala.YieldStatement : CodeNode, Statement {
                this.source_reference = source_reference;
        }
 
+       public override bool check (CodeContext context) {
+               if (checked) {
+                       return !error;
+               }
+
+               checked = true;
+
+               if (context.analyzer.current_method == null || !context.analyzer.current_method.coroutine) {
+                       error = true;
+                       Report.error (source_reference, "yield statement not available outside async method");
+               }
+
+               return !error;
+       }
+
        public override void emit (CodeGenerator codegen) {
                codegen.visit_yield_statement (this);
        }