]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Backport:
authorVolker Reichelt <reichelt@igpm.rwth-aachen.de>
Mon, 5 Dec 2005 13:12:29 +0000 (13:12 +0000)
committerVolker Reichelt <reichelt@gcc.gnu.org>
Mon, 5 Dec 2005 13:12:29 +0000 (13:12 +0000)
2005-10-13  Mark Mitchell  <mark@codesourcery.com>

PR c++/22464
* semantics.c (finish_id_expression): Issue errors about uses of
local variables in containing functions even in templates.

* g++.dg/template/crash41.C: New test.

From-SVN: r108055

gcc/cp/ChangeLog
gcc/cp/semantics.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/template/crash41.C [new file with mode: 0644]

index 6a78fa502d3086731506993ef8eb830c204d14db..ae8cad7bf6ba46f7a2c9f866072b7be86c155e34 100644 (file)
@@ -1,3 +1,12 @@
+2005-12-05  Volker Reichelt  <reichelt@igpm.rwth-aachen.de>
+
+       Backport:
+       2005-10-13  Mark Mitchell  <mark@codesourcery.com>
+
+       PR c++/22464
+       * semantics.c (finish_id_expression): Issue errors about uses of
+       local variables in containing functions even in templates.
+
 2005-12-05  Volker Reichelt  <reichelt@igpm.rwth-aachen.de>
 
        Backport:
index 55984b75ade24d383e7939ec28abf69ff154fdb1..943e7a1a92a68ede2b3491efdee6d33ce9d232b7 100644 (file)
@@ -2400,6 +2400,21 @@ finish_id_expression (tree id_expression,
         was entirely defined.  */
       if (!scope && decl != error_mark_node)
        maybe_note_name_used_in_class (id_expression, decl);
+
+      /* Disallow uses of local variables from containing functions.  */
+      if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL)
+       {
+         tree context = decl_function_context (decl);
+         if (context != NULL_TREE && context != current_function_decl
+             && ! TREE_STATIC (decl))
+           {
+             error (TREE_CODE (decl) == VAR_DECL
+                    ? "use of `auto' variable from containing function"
+                    : "use of parameter from containing function");
+             cp_error_at ("  `%#D' declared here", decl);
+             return error_mark_node;
+           }
+       }
     }
 
   /* If we didn't find anything, or what we found was a type,
@@ -2666,23 +2681,6 @@ finish_id_expression (tree id_expression,
        }
       else
        {
-         if (TREE_CODE (decl) == VAR_DECL
-             || TREE_CODE (decl) == PARM_DECL
-             || TREE_CODE (decl) == RESULT_DECL)
-           {
-             tree context = decl_function_context (decl);
-             
-             if (context != NULL_TREE && context != current_function_decl
-                 && ! TREE_STATIC (decl))
-               {
-                 error ("use of %s from containing function",
-                        (TREE_CODE (decl) == VAR_DECL
-                         ? "`auto' variable" : "parameter"));
-                 cp_error_at ("  `%#D' declared here", decl);
-                 return error_mark_node;
-               }
-           }
-         
          if (DECL_P (decl) && DECL_NONLOCAL (decl)
              && DECL_CLASS_SCOPE_P (decl)
              && DECL_CONTEXT (decl) != current_class_type)
index 6d38f7e7025f7db1791be31f2ba6cf14bdffeed9..7063568386a56b9b20bafca14d2b61f94f5d6775 100644 (file)
@@ -1,3 +1,11 @@
+2005-12-05  Volker Reichelt  <reichelt@igpm.rwth-aachen.de>
+
+       Backport:
+       2005-10-13  Mark Mitchell  <mark@codesourcery.com>
+
+       PR c++/22464
+       * g++.dg/template/crash41.C: New test.
+
 2005-12-05  Volker Reichelt  <reichelt@igpm.rwth-aachen.de>
 
        Backport:
diff --git a/gcc/testsuite/g++.dg/template/crash41.C b/gcc/testsuite/g++.dg/template/crash41.C
new file mode 100644 (file)
index 0000000..2b8028e
--- /dev/null
@@ -0,0 +1,15 @@
+// PR c++/22464
+
+template<typename T>
+void do_something(const T* A) { // { dg-error "declared" }
+  struct helper_t { 
+    helper_t() {  
+      A[0]; // { dg-error "use" }
+    }
+  } helper;
+}
+
+void sub1() {
+  double A[7];
+  do_something (A);
+}