Bug 38030 - [4.2/4.3 Regression] name-lookup for non-dependent name in template function is wrong
Summary: [4.2/4.3 Regression] name-lookup for non-dependent name in template function ...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.3.2
: P1 normal
Target Milestone: 4.2.5
Assignee: Jason Merrill
URL:
Keywords: wrong-code
: 38031 39534 (view as bug list)
Depends on:
Blocks:
 
Reported: 2008-11-06 10:24 UTC by Ashutosh Nema
Modified: 2009-03-24 05:14 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work: 3.4.6 4.0.4 4.4.0
Known to fail: 3.3.6 4.1.0 4.1.2 4.3.2
Last reconfirmed: 2008-11-14 18:29:59


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ashutosh Nema 2008-11-06 10:24:11 UTC
Dear All,

I am unable to understand following test case behaviour.
When i compile and execute this with g++ 3.4.6 it works fine.
But when i compile and execute this with g++ 4.3.2 it aborts.

I think the test case is OK.
But g++ 4.3.2 having bug.

Below is test case:
==========================================================

#define ieq(X,Y) if((X)!=(Y)) abort();
extern void abort ();
struct B_
{
  int i;
};
struct D_:public B_
{
};
D_ d_;
int f_ (B_ &)
{
  return 100;
}
template < class T > int g_ (T t)
{
  return f_ (d_) + f_ (t);
}
int f_ (D_ &)
{
  return 1;
}
int main (int argc, char *argv[])
{
  D_ d;
  ieq (g_ (d), 101);
  return 0;
}
==========================================================

Please help me regarding this.
Comment 1 Richard Biener 2008-11-06 11:51:12 UTC
Confirmed.  For the following neither f_ nor d_ are dependent and should be
bound to f_ (B_ &) during first-stage name-lookup.  For some reason this
doesn't work - it does if d_ is of type B_ though.

extern "C" void abort ();
struct B_ { };
struct D_:public B_ { };
D_ d_;
void f_ (B_ &) { }
template < class T >
void g_ (T t)
{
  f_ (d_);
}
void f_ (D_ &) { abort (); }
int main (int argc, char *argv[])
{
  D_ d;
  g_ (d);
  return 0;
}
Comment 2 Richard Biener 2008-11-06 12:08:25 UTC
Even the following fails

extern "C" void abort ();
struct B_ { };
struct D_ : public B_ { };
D_ d_;
void f_ (B_ &) { }
template < class T >
void g_ ()
{
  return f_ (d_);
}
int main ()
{
  g_<int> ();
  return 0;
}
void f_ (D_ &) { abort (); }

where f_ (D_ &) must not be even in the 2nd stage name-lookup overload set.
Comment 3 Richard Biener 2008-11-06 12:54:44 UTC
We seem to not know if we bound f_ already.  Instead another round of koenig
lookup is applied from

#0  perform_koenig_lookup (fn=0x7ffff6458500, args=0x7ffff6463480)
    at /space/rguenther/src/svn/trunk/gcc/cp/semantics.c:1797
#1  0x00000000004c9e14 in tsubst_copy_and_build (t=0x7ffff7ebd870, 
    args=0x7ffff64631b0, complain=tf_warning_or_error, in_decl=0x7ffff6461780, 
    function_p=0 '\0', integral_constant_expression_p=0 '\0')
    at /space/rguenther/src/svn/trunk/gcc/cp/pt.c:11293
#2  0x00000000004c6798 in tsubst_expr (t=0x7ffff7ebd870, args=0x7ffff64631b0, 
    complain=tf_warning_or_error, in_decl=0x7ffff6461780, 
    integral_constant_expression_p=0 '\0')
    at /space/rguenther/src/svn/trunk/gcc/cp/pt.c:10880
#3  0x00000000004bff18 in tsubst_expr (t=0x7ffff644a5c0, args=0x7ffff64631b0, 
    complain=tf_warning_or_error, in_decl=0x7ffff6461780, 
    integral_constant_expression_p=0 '\0')
    at /space/rguenther/src/svn/trunk/gcc/cp/pt.c:10483
#4  0x00000000004c1c98 in tsubst_expr (t=0x7ffff7ebd7d0, args=0x7ffff64631b0, 
    complain=tf_warning_or_error, in_decl=0x7ffff6461780, 
    integral_constant_expression_p=0 '\0')
    at /space/rguenther/src/svn/trunk/gcc/cp/pt.c:10630
#5  0x00000000004e78c5 in instantiate_decl (d=0x7ffff6458900, defer_ok=0, 
    expl_inst_class_mem_p=0 '\0')
    at /space/rguenther/src/svn/trunk/gcc/cp/pt.c:15378
#6  0x00000000004e7f8c in instantiate_pending_templates (retries=0)
    at /space/rguenther/src/svn/trunk/gcc/cp/pt.c:15474
#7  0x0000000000540bb0 in cp_write_global_declarations ()
    at /space/rguenther/src/svn/trunk/gcc/cp/decl2.c:3364

note the instantiation is from cp_write_global_declarations which is after
the whole unit is parsed, which may explain comment #2.
Comment 4 Jakub Jelinek 2008-11-06 12:55:37 UTC
*** Bug 38031 has been marked as a duplicate of this bug. ***
Comment 5 Jason Merrill 2008-11-14 18:29:59 UTC
G++ has never done two-phase name binding; that's an area that needs work for standards conformance.  This testcase worked properly by accident in 3.4 and 4.0, because of an optimization which was disallowed under DR 164.
Comment 6 Jason Merrill 2008-11-14 21:59:02 UTC
Subject: Bug 38030

Author: jason
Date: Fri Nov 14 21:57:34 2008
New Revision: 141866

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=141866
Log:
        PR c++/38030
        * semantics.c (finish_call_expr): Remember the result of
        non-dependent overload resolution.

Added:
    trunk/gcc/testsuite/g++.dg/template/lookup8.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/semantics.c
    trunk/gcc/testsuite/ChangeLog

Comment 7 Jason Merrill 2008-11-14 22:02:39 UTC
Subject: Bug 38030

Author: jason
Date: Fri Nov 14 22:01:12 2008
New Revision: 141868

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=141868
Log:
        PR c++/38030
        * semantics.c (finish_call_expr): Don't repeat arg-dep lookup
        for a non-dependent call.

Added:
    branches/gcc-4_3-branch/gcc/testsuite/g++.dg/template/lookup8.C
      - copied unchanged from r141866, trunk/gcc/testsuite/g++.dg/template/lookup8.C
Modified:
    branches/gcc-4_3-branch/gcc/cp/ChangeLog
    branches/gcc-4_3-branch/gcc/cp/semantics.c
    branches/gcc-4_3-branch/gcc/testsuite/ChangeLog

Comment 8 Jason Merrill 2008-11-14 23:07:36 UTC
Subject: Bug 38030

Author: jason
Date: Fri Nov 14 23:06:11 2008
New Revision: 141872

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=141872
Log:
        PR c++/38030
        * semantics.c (finish_call_expr): Don't repeat arg-dep lookup
        for a non-dependent call.

Added:
    branches/gcc-4_2-branch/gcc/testsuite/g++.dg/template/lookup8.C
      - copied unchanged from r141866, trunk/gcc/testsuite/g++.dg/template/lookup8.C
Modified:
    branches/gcc-4_2-branch/gcc/cp/ChangeLog
    branches/gcc-4_2-branch/gcc/cp/semantics.c
    branches/gcc-4_2-branch/gcc/testsuite/ChangeLog

Comment 9 Jason Merrill 2008-11-14 23:16:02 UTC
Fixed.
Comment 10 rguenther@suse.de 2008-11-14 23:27:42 UTC
Subject: Re:  [4.2/4.3/4.4 Regression] name-lookup for
 non-dependent name in template function is wrong

On Fri, 14 Nov 2008, jason at gcc dot gnu dot org wrote:

> ------- Comment #5 from jason at gcc dot gnu dot org  2008-11-14 18:29 -------
> G++ has never done two-phase name binding; that's an area that needs work for
> standards conformance.

Doh.  That seems to be a pretty important semantic piece of C++ though ;)

Thanks for fixing this bug so quickly.

Richard.
Comment 11 Andrew Pinski 2009-03-24 05:14:11 UTC
*** Bug 39534 has been marked as a duplicate of this bug. ***
Comment 12 Jason Merrill 2009-03-31 15:43:26 UTC
Subject: Bug 38030

Author: jason
Date: Tue Mar 31 15:42:58 2009
New Revision: 145360

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=145360
Log:
        PR c++/38030, 38850, 39070
        * pt.c (type_dependent_expression_p_push): New fn.
        (tsubst_copy_and_build) [CALL_EXPR]: Only do arg-dep lookup when the
        substitution makes the call non-dependent.  Preserve koenig_p.
        * parser.c (cp_parser_postfix_expression): Only do arg-dep lookup
        for non-dependent calls.
        * semantics.c (finish_call_expr): Revert earlier changes.
        * cp-tree.h: Revert change to finish_call_expr prototype.

Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/cp-tree.h
    trunk/gcc/cp/parser.c
    trunk/gcc/cp/pt.c
    trunk/gcc/cp/semantics.c

Comment 13 Jason Merrill 2009-04-02 18:38:19 UTC
Subject: Bug 38030

Author: jason
Date: Thu Apr  2 18:37:57 2009
New Revision: 145468

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=145468
Log:
        * mangle.c (write_expression): Mangle dependent name as
        source-name.

        PR c++/38030, 38850, 39070
        * pt.c (type_dependent_expression_p_push): New fn.
        (tsubst_copy_and_build) [CALL_EXPR]: Only do arg-dep lookup when the
        substitution makes the call non-dependent.  Preserve koenig_p.
        * parser.c (cp_parser_postfix_expression): Only do arg-dep lookup
        for non-dependent calls.
        * semantics.c (finish_call_expr): Revert earlier changes.
        * cp-tree.h: Revert change to finish_call_expr prototype.

Modified:
    branches/gcc-4_4-branch/gcc/cp/ChangeLog
    branches/gcc-4_4-branch/gcc/cp/cp-tree.h
    branches/gcc-4_4-branch/gcc/cp/mangle.c
    branches/gcc-4_4-branch/gcc/cp/parser.c
    branches/gcc-4_4-branch/gcc/cp/pt.c
    branches/gcc-4_4-branch/gcc/cp/semantics.c
    branches/gcc-4_4-branch/gcc/testsuite/ChangeLog
    branches/gcc-4_4-branch/gcc/testsuite/g++.dg/cpp0x/auto12.C
    branches/gcc-4_4-branch/gcc/testsuite/g++.dg/cpp0x/auto6.C

Comment 14 Jason Merrill 2009-04-03 18:05:05 UTC
Subject: Bug 38030

Author: jason
Date: Fri Apr  3 18:04:39 2009
New Revision: 145511

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=145511
Log:
        PR c++/38030, 38850, 39070
        * pt.c (type_dependent_expression_p_push): New fn.
        (tsubst_copy_and_build) [CALL_EXPR]: Only do arg-dep lookup when the
        substitution makes the call non-dependent.  Preserve koenig_p.
        * parser.c (cp_parser_postfix_expression): Only do arg-dep lookup
        for non-dependent calls.
        * semantics.c (finish_call_expr): Revert earlier changes.
        * cp-tree.h: Revert change to finish_call_expr prototype.

Modified:
    branches/gcc-4_3-branch/gcc/cp/ChangeLog
    branches/gcc-4_3-branch/gcc/cp/cp-tree.h
    branches/gcc-4_3-branch/gcc/cp/parser.c
    branches/gcc-4_3-branch/gcc/cp/pt.c
    branches/gcc-4_3-branch/gcc/cp/semantics.c