From: Rico Tzschichholz Date: Thu, 14 Mar 2019 08:16:51 +0000 (+0100) Subject: vala: Report error for yield statements without async context X-Git-Tag: 0.45.1~105 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d19187f3b5f7bdc99bba2c9a856f51af9e8cd520;p=thirdparty%2Fvala.git vala: Report error for yield statements without async context --- diff --git a/tests/Makefile.am b/tests/Makefile.am index 41481e7e8..f0000a94b 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -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 index 000000000..8dd03950b --- /dev/null +++ b/tests/semantic/yield-statement-requires-async-context.test @@ -0,0 +1,5 @@ +Invalid Code + +void main () { + yield; +} diff --git a/vala/valayieldstatement.vala b/vala/valayieldstatement.vala index 795de2761..91d8e7f9f 100644 --- a/vala/valayieldstatement.vala +++ b/vala/valayieldstatement.vala @@ -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); }