Bug 90505 - [9 Regression] g++ rejects valid code
Summary: [9 Regression] g++ rejects valid code
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 9.1.0
: P2 normal
Target Milestone: 9.3
Assignee: Marek Polacek
URL:
Keywords: patch, rejects-valid
: 91479 92221 (view as bug list)
Depends on:
Blocks:
 
Reported: 2019-05-16 11:20 UTC by Mykhailo Kremniov
Modified: 2020-03-04 23:53 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work: 8.3.0
Known to fail: 10.0, 9.1.0
Last reconfirmed: 2019-05-16 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Mykhailo Kremniov 2019-05-16 11:20:37 UTC
GCC 9.1 fails to compile the following code (previous versions compile it just fine).

=== test.cpp ===
template <class T>
struct S
{
    template <class U, class V>
    static void foo(V)
    {
    }
    
    void bar()
    {
        foo<void>(1);
    }
};
===

=== Console ===
~/tmp$ g++-9.1.0 -c test.cpp
test.cpp: In member function ‘void S<T>::bar()’:
test.cpp:11:20: error: no matching function for call to ‘S<T>::foo<void>(int)’
   11 |         foo<void>(1);
      |                    ^
test.cpp:5:17: note: candidate: ‘template<class T> template<class U, class V> static void S<T>::foo(V)’
    5 |     static void foo(V)
      |                 ^~~
test.cpp:5:17: note:   template argument deduction/substitution failed:
test.cpp:11:20: note:   mismatched types ‘V’ and ‘int’
   11 |         foo<void>(1);
      |       
===

=== Compiler version ===
~/tmp$ g++-9.1.0 -v
Using built-in specs.
COLLECT_GCC=g++-9.1.0
COLLECT_LTO_WRAPPER=/home/brd/soft/gcc-9.1.0/libexec/gcc/x86_64-pc-linux-gnu/9.1.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ./configure --prefix=/home/brd/soft/gcc-9.1.0
Thread model: posix
gcc version 9.1.0 (GCC) 
===
Comment 1 Martin Liška 2019-05-16 11:58:19 UTC
Started with r265734.
Comment 2 Marek Polacek 2019-05-16 11:59:50 UTC
Mine then.
Comment 3 Marek Polacek 2019-05-16 16:31:47 UTC
Seems to be caused by this change:

@@ -16327,15 +16388,6 @@ cp_parser_template_name (cp_parser* parser,
    }
     }
 
-  /* If DECL is dependent, and refers to a function, then just return
-     its name; we will look it up again during template instantiation.  */
-  if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
-    {
-      tree scope = ovl_scope (decl);
-      if (TYPE_P (scope) && dependent_type_p (scope))
-   return identifier;
-    }
-
   return decl;
 }
Comment 4 Jakub Jelinek 2019-08-12 08:57:02 UTC
GCC 9.2 has been released.
Comment 5 Marek Polacek 2019-08-17 17:11:22 UTC
*** Bug 91479 has been marked as a duplicate of this bug. ***
Comment 6 Marek Polacek 2019-08-17 17:12:21 UTC
Test from 91479:

template <class T> struct foo
{
  template <typename U, typename=void>
  static void bar(const U&) {}

  static void bar(int x) { bar<int>(x); }
};
Comment 7 Marek Polacek 2019-10-25 14:47:24 UTC
*** Bug 92221 has been marked as a duplicate of this bug. ***
Comment 8 Marek Polacek 2019-10-25 14:48:00 UTC
Test from Bug 92221:

template <typename> class a {
  using b = int;
  using c = int;
  b d;
  void e() { g<c>(d); }
  template <typename... f> static void g(f...);
};
Comment 9 Marek Polacek 2020-03-04 00:59:00 UTC
Finally posted a patch:
https://gcc.gnu.org/ml/gcc-patches/2020-03/msg00181.html
Comment 10 GCC Commits 2020-03-04 04:24:20 UTC
The master branch has been updated by Marek Polacek <mpolacek@gcc.gnu.org>:

https://gcc.gnu.org/g:6b3302da9ef26aa11940f8c0dc92bec19e15c09b

commit r10-7007-g6b3302da9ef26aa11940f8c0dc92bec19e15c09b
Author: Marek Polacek <polacek@redhat.com>
Date:   Tue Mar 3 17:56:44 2020 -0500

    c++: Fix mismatch in template argument deduction [PR90505]
    
    My GCC 9 patch for C++20 P0846R0 (ADL and function templates) tweaked
    cp_parser_template_name to only return an identifier if name lookup
    didn't find anything.  In the deduce4.C case it means that we now
    return an OVERLOAD.  That means that cp_parser_template_id will call
    lookup_template_function whereby producing a TEMPLATE_ID_EXPR with
    unknown_type_node.  Previously, we created a TEMPLATE_ID_EXPR with
    no type, making it type-dependent.  What we have now is no longer
    type-dependent.  And so, when we call finish_call_expr after we've
    parsed "foo<int>(10)", even though we're in a template, we still do
    the normal processing, thus perform overload resolution.  When adding
    the template candidate foo we need to deduce the template arguments,
    and that is where things go downhill.
    
    When fn_type_unification sees that we have explicit template arguments,
    but they aren't complete, it will use them to substitute the function
    type.  So we substitute e.g. "void <T33d> (U)".  But the explicit
    template argument was for a different parameter so we don't actually
    substitute anything.  But the problem here was that we reduced the
    template level of 'U' anyway.  So then when we're actually deducing
    the template arguments via type_unification_real, we fail in unify:
    22932       if (TEMPLATE_TYPE_LEVEL (parm)
    22933           != template_decl_level (tparm))
    22934         /* The PARM is not one we're trying to unify.  Just check
    22935            to see if it matches ARG.  */
    because 'parm' has been reduced but 'tparm' has not yet.
    
    Therefore we shouldn't reduce the template level of template parameters
    when tf_partial aka template argument deduction substitution.  But we
    can only return after performing the cp_build_qualified_type etc.
    business otherwise things break horribly.
    
    2020-03-03  Jason Merrill  <jason@redhat.com>
    	    Marek Polacek  <polacek@redhat.com>
    
    	PR c++/90505 - mismatch in template argument deduction.
    	* pt.c (tsubst): Don't reduce the template level of template
    	parameters when tf_partial.
    
    	* g++.dg/template/deduce4.C: New test.
    	* g++.dg/template/deduce5.C: New test.
    	* g++.dg/template/deduce6.C: New test.
    	* g++.dg/template/deduce7.C: New test.
Comment 11 Marek Polacek 2020-03-04 04:28:30 UTC
Fixed on trunk so far.
Comment 12 GCC Commits 2020-03-04 23:52:32 UTC
The releases/gcc-9 branch has been updated by Marek Polacek <mpolacek@gcc.gnu.org>:

https://gcc.gnu.org/g:581825efc30ce79d86dfb0ebf378913fdec44adf

commit r9-8333-g581825efc30ce79d86dfb0ebf378913fdec44adf
Author: Marek Polacek <polacek@redhat.com>
Date:   Wed Mar 4 18:49:37 2020 -0500

    c++: Fix mismatch in template argument deduction [PR90505]
    
    2020-03-03  Jason Merrill  <jason@redhat.com>
    	    Marek Polacek  <polacek@redhat.com>
    
    	PR c++/90505 - mismatch in template argument deduction.
    	* pt.c (tsubst): Don't reduce the template level of template
    	parameters when tf_partial.
    
    	* g++.dg/template/deduce4.C: New test.
    	* g++.dg/template/deduce5.C: New test.
    	* g++.dg/template/deduce6.C: New test.
    	* g++.dg/template/deduce7.C: New test.
Comment 13 Marek Polacek 2020-03-04 23:53:30 UTC
Fixed.