Patchwork C++ PATCH to diagnose parms used outside the function

login
register
mail settings
Submitter Jason Merrill
Date Sept. 15, 2010, 11:58 p.m.
Message ID <4C915D96.9050206@redhat.com>
Download mbox | patch
Permalink /patch/64927/
State New
Headers show

Comments

Jason Merrill - Sept. 15, 2010, 11:58 p.m.
A constexpr testcase was crashing on trying to use a function parm in an 
evaluated constant expression in the late-specified return type.  This 
patch makes it give a better error.  The testcase in the patch doesn't 
crash without it, but the patch does produce a better error.

Tested x86_64-pc-linux-gnu, applied to trunk.

Patch

commit ff67fd57b978582fcc5f8818a8192a25c507ac34
Author: Jason Merrill <jason@redhat.com>
Date:   Wed Sep 15 16:07:37 2010 -0400

    	* semantics.c (finish_id_expression): Diagnose use of function
    	parms in evaluated context outside function body.

diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c
index dc81568..b73dffb 100644
--- a/gcc/cp/semantics.c
+++ b/gcc/cp/semantics.c
@@ -2864,6 +2864,16 @@  finish_id_expression (tree id_expression,
 	      return error_mark_node;
 	    }
 	}
+
+      /* Also disallow uses of function parameters outside the function
+	 body, except inside an unevaluated context (i.e. decltype).  */
+      if (TREE_CODE (decl) == PARM_DECL
+	  && DECL_CONTEXT (decl) == NULL_TREE
+	  && !cp_unevaluated_operand)
+	{
+	  error ("use of parameter %qD outside function body", decl);
+	  return error_mark_node;
+	}
     }
 
   /* If we didn't find anything, or what we found was a type,
diff --git a/gcc/testsuite/g++.dg/parse/parameter-declaration-2.C b/gcc/testsuite/g++.dg/parse/parameter-declaration-2.C
new file mode 100644
index 0000000..7a9a24f
--- /dev/null
+++ b/gcc/testsuite/g++.dg/parse/parameter-declaration-2.C
@@ -0,0 +1 @@ 
+void f (int i, int p[i]); // { dg-error "use of parameter .i. outside function body" }