From: Jürg Billeter Date: Thu, 17 Sep 2009 16:32:34 +0000 (+0200) Subject: Report error when capturing uninitialized variables in closures X-Git-Tag: 0.7.6~9 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=873e330917ecced15684491a511c38b45baf761f;p=thirdparty%2Fvala.git Report error when capturing uninitialized variables in closures --- diff --git a/vala/valalambdaexpression.vala b/vala/valalambdaexpression.vala index b7bb1f3c5..16a51c1c3 100644 --- a/vala/valalambdaexpression.vala +++ b/vala/valalambdaexpression.vala @@ -226,4 +226,11 @@ public class Vala.LambdaExpression : Expression { return !error; } + + public override void get_used_variables (Collection collection) { + // require captured variables to be initialized + if (method.closure) { + method.get_captured_variables (collection); + } + } } diff --git a/vala/valamemberaccess.vala b/vala/valamemberaccess.vala index b7dc62ac4..70f3a260c 100644 --- a/vala/valamemberaccess.vala +++ b/vala/valamemberaccess.vala @@ -426,6 +426,7 @@ public class Vala.MemberAccess : Expression { local.captured = true; block.captured = true; analyzer.current_method.closure = true; + analyzer.current_method.add_captured_variable (local); } } else if (member is FormalParameter) { var param = (FormalParameter) member; diff --git a/vala/valamethod.vala b/vala/valamethod.vala index 327cdb1d8..05b78936f 100644 --- a/vala/valamethod.vala +++ b/vala/valamethod.vala @@ -243,6 +243,9 @@ public class Vala.Method : Member { Method? callback_method; + // only valid for closures + Gee.List captured_variables; + /** * Creates a new method. * @@ -1025,6 +1028,23 @@ public class Vala.Method : Member { return params; } + + public void add_captured_variable (LocalVariable local) { + assert (this.closure); + + if (captured_variables == null) { + captured_variables = new ArrayList (); + } + captured_variables.add (local); + } + + public void get_captured_variables (Collection variables) { + if (captured_variables != null) { + foreach (var local in captured_variables) { + variables.add (local); + } + } + } } // vim:sw=8 noet