This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[patch] Fix PR c++/13377, bad diagnostic for ambiguous name-lookup
- From: Volker Reichelt <reichelt at igpm dot rwth-aachen dot de>
- To: gcc-patches at gcc dot gnu dot org
- Date: Mon, 29 Aug 2005 21:22:17 +0200 (CEST)
- Subject: [patch] Fix PR c++/13377, bad diagnostic for ambiguous name-lookup
The following patch improves the diagnostic in case of ambiguous
name-lookup. For the testcase
namespace N
{
int i;
}
int i;
using namespace N;
void foo() { i; }
we get the error message:
ambig.cc: In function 'void foo()':
ambig.cc:10: error: 'i' was not declared in this scope
This is rather misleading, since 'i' *is* declared in this scope.
The patch fixes the problem by passing LOOKUP_COMPLAIN to
lookup_name_real, so that the latter points out the ambiguity of
the declarations caused by the using directive.
The error message now reads:
ambig.cc: In function 'void foo()':
ambig.cc:10: error: use of 'i' is ambiguous
ambig.cc:6: error: first declared as 'int i' here
ambig.cc:3: error: also declared as 'int N::i' here
ambig.cc:10: error: 'i' was not declared in this scope
Bootstrapped and regtested on i686-pc-linux-gnu.
Ok for mainline, 4.0 branch, and 3.4 branch
(since this is a regression)?
Regards,
Volker
2005-08-29 Volker Reichelt <reichelt@igpm.rwth-aachen.de>
PR c++/13377
* parser.c (cp_parser_lookup_name): Pass LOOKUP_COMPLAIN to
lookup_name_real on final parse.
===================================================================
--- gcc/gcc/cp/parser.c 24 Aug 2005 10:21:31 -0000 1.351
+++ gcc/gcc/cp/parser.c 29 Aug 2005 13:27:08 -0000
@@ -14462,9 +14462,13 @@ cp_parser_lookup_name (cp_parser *parser
bool check_dependency,
bool *ambiguous_p)
{
+ int flags = 0;
tree decl;
tree object_type = parser->context->object_type;
+ if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
+ flags |= LOOKUP_COMPLAIN;
+
/* Assume that the lookup will be unambiguous. */
if (ambiguous_p)
*ambiguous_p = false;
@@ -14597,8 +14601,7 @@ cp_parser_lookup_name (cp_parser *parser
/* Look it up in the enclosing context, too. */
decl = lookup_name_real (name, tag_type != none_type,
/*nonclass=*/0,
- /*block_p=*/true, is_namespace,
- /*flags=*/0);
+ /*block_p=*/true, is_namespace, flags);
parser->object_scope = object_type;
parser->qualifying_scope = NULL_TREE;
if (object_decl)
@@ -14608,8 +14611,7 @@ cp_parser_lookup_name (cp_parser *parser
{
decl = lookup_name_real (name, tag_type != none_type,
/*nonclass=*/0,
- /*block_p=*/true, is_namespace,
- /*flags=*/0);
+ /*block_p=*/true, is_namespace, flags);
parser->qualifying_scope = NULL_TREE;
parser->object_scope = NULL_TREE;
}
===================================================================
2005-08-29 Volker Reichelt <reichelt@igpm.rwth-aachen.de>
PR c++/13377
* g++.dg/lookup/ambig4.C: New test.
* g++.dg/tc1/dr101.C: Adjust error markers.
===================================================================
--- gcc/gcc/testsuite/g++.dg/lookup/ambig4.C 2005-08-22 19:21:29 +0200
+++ gcc/gcc/testsuite/g++.dg/lookup/ambig4.C 2005-08-29 21:12:03 +0200
@@ -0,0 +1,14 @@
+// PR c++/13377
+// Origin: Boris Kolpackov <boris@kolpackov.net>
+// { dg-do compile }
+
+namespace N
+{
+ int i; // { dg-error "declared" }
+}
+
+int i; // { dg-error "declared" }
+
+using namespace N;
+
+void foo() { i; } // { dg-error "in this scope|ambiguous" }
===================================================================
--- gcc/gcc/testsuite/g++.dg/tc1/dr101.C 17 Feb 2004 15:33:49 -0000 1.2
+++ gcc/gcc/testsuite/g++.dg/tc1/dr101.C 29 Aug 2005 19:02:13 -0000
@@ -17,10 +17,10 @@ namespace Test1 {
namespace Test2 {
- typedef unsigned int X;
+ typedef unsigned int X; // { dg-bogus "declared" "" { xfail *-*-* } }
extern "C" int f2();
namespace N {
- typedef unsigned int X;
+ typedef unsigned int X; // { dg-bogus "declared" "" { xfail *-*-* } }
extern "C" int f2();
}
using namespace N;
===================================================================