[vala/wip/issue/838: 9/9] codegen: Don't unconditionally add/return internal "result" variable



commit 8c9a783277a771d5e135dfb6afcd40daba9d97cb
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Sat Aug 24 18:46:42 2019 +0200

    codegen: Don't unconditionally add/return internal "result" variable
    
    Adds scope-specific "result" variable and avoids it in non-void methods
    which don't have a return statement.
    
    Found by -Werror=unreachable-code
    
    See https://gitlab.gnome.org/GNOME/vala/issues/838

 codegen/valaccodemethodmodule.vala | 21 ++++++++-------------
 1 file changed, 8 insertions(+), 13 deletions(-)
---
diff --git a/codegen/valaccodemethodmodule.vala b/codegen/valaccodemethodmodule.vala
index 864d6a93e..105f64332 100644
--- a/codegen/valaccodemethodmodule.vala
+++ b/codegen/valaccodemethodmodule.vala
@@ -601,12 +601,6 @@ public abstract class Vala.CCodeMethodModule : CCodeStructModule {
                                        }
                                }
 
-                               if (!(m.return_type is VoidType) && 
!m.return_type.is_real_non_null_struct_type () && !m.coroutine) {
-                                       var vardecl = new CCodeVariableDeclarator ("result", 
default_value_for_type (m.return_type, true));
-                                       vardecl.init0 = true;
-                                       ccode.add_declaration (get_ccode_name (m.return_type), vardecl);
-                               }
-
                                if (m is CreationMethod) {
                                        if (in_gobject_creation_method) {
                                                if (!m.coroutine) {
@@ -767,13 +761,6 @@ public abstract class Vala.CCodeMethodModule : CCodeStructModule {
                                        complete_async ();
                                }
 
-                               if (!(m.return_type is VoidType) && 
!m.return_type.is_real_non_null_struct_type () && !m.coroutine) {
-                                       // add dummy return if exit block is known to be unreachable to 
silence C compiler
-                                       if (m.return_block != null && m.return_block.get_predecessors ().size 
== 0) {
-                                               ccode.add_return (new CCodeIdentifier ("result"));
-                                       }
-                               }
-
                                if (m is CreationMethod) {
                                        if (current_type_symbol is Class) {
                                                creturn_type = new ObjectType (current_class);
@@ -840,6 +827,12 @@ public abstract class Vala.CCodeMethodModule : CCodeStructModule {
                        cfile.add_function (ccode);
                }
 
+               if (current_method_return && !(m.return_type is VoidType) && 
!m.return_type.is_real_non_null_struct_type () && !m.coroutine) {
+                       var vardecl = new CCodeVariableDeclarator ("result", default_value_for_type 
(m.return_type, true));
+                       vardecl.init0 = true;
+                       ccode.add_declaration (get_ccode_name (m.return_type), vardecl);
+               }
+
                pop_context ();
 
                if ((m.is_abstract || m.is_virtual) && !m.coroutine &&
@@ -1199,6 +1192,8 @@ public abstract class Vala.CCodeMethodModule : CCodeStructModule {
                }
 
                ccode.add_expression (ccheck);
+
+               current_method_return = true;
        }
 
        public override void visit_creation_method (CreationMethod m) {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]