llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

template type deduction handled improperly with implicit overload resolution

Open compnerd opened this issue 7 years ago • 14 comments

Bugzilla Link 43287
Version trunk
OS All
CC @dwblaikie,@DougGregor,@zygoloid

Extended Description

I believe that clang is handling template type deduction improperly for forward declarations of templates with overload resolution. Consider the following:

#include <type_traits>

template<typename U>
int f(const U t);

int v;
auto test = f(v);

template<typename U>
int f(U t) {
  static_assert(!std::is_const<decltype(t)>::value, "t should be non-const");
  return 0;
}

GCC correctly identifies this as int rather than const int. I believe that the type information is being preserved from the canonical decl from which this overload is being referenced. This was reduced from another test case which results in an invalid function call.

Sorry, I couldn't really come up with a good summary

compnerd avatar Sep 11 '19 23:09 compnerd

@llvm/issue-subscribers-clang-frontend

Author: Saleem Abdulrasool (compnerd)

| | | | --- | --- | | Bugzilla Link | [43287](https://llvm.org/bz43287) | | Version | trunk | | OS | All | | CC | @dwblaikie,@DougGregor,@zygoloid |

Extended Description

I believe that clang is handling template type deduction improperly for forward declarations of templates with overload resolution. Consider the following:

#include &lt;type_traits&gt;

template&lt;typename U&gt;
int f(const U t);

int v;
auto test = f(v);

template&lt;typename U&gt;
int f(U t) {
  static_assert(!std::is_const&lt;decltype(t)&gt;::value, "t should be non-const");
  return 0;
}

GCC correctly identifies this as int rather than const int. I believe that the type information is being preserved from the canonical decl from which this overload is being referenced. This was reduced from another test case which results in an invalid function call.

Sorry, I couldn't really come up with a good summary

llvmbot avatar Oct 10 '23 16:10 llvmbot

Hi, I think this is false issue and can be closed. Function arguments after implicit conversion to match function parameter is treated as converted value, and not as the value it was passed. Here Implicit conversion transforms the non-const value to const value and that's why the assert fails. https://godbolt.org/z/T6dGGjP3j

mahtohappy avatar Feb 02 '24 15:02 mahtohappy

As @shafik pointed out to me offline, there is split between implementations: https://godbolt.org/z/xcebMo164

Endilll avatar Feb 02 '24 16:02 Endilll

Yes, but if you rearrange the code like this https://godbolt.org/z/j71j58vMG it's treated as int and the code works fine for every implementation. So the problem is only when the template function is forward declared.

mahtohappy avatar Feb 02 '24 16:02 mahtohappy

https://godbolt.org/z/aever9j3h and if the it's normal function instead of a templated function then also it's treated int. So I can work on unifying the behavior where templated functions behave the same way a normal function does? Any Inputs?

mahtohappy avatar Feb 02 '24 17:02 mahtohappy

I still want us to understand Standard's point of view on this example before making any moves. The fact that Clang is not the only compiler that behaves the current way (EDG matches us), makes me even more cautious.

Endilll avatar Feb 02 '24 17:02 Endilll

Clang is definitely wrong here. The parameter types of the function are adjusted during template argument deduction (see [temp.deduct.general] p3), but the types of the parameter declarations does not change (there is also a note in the linked wording which says as much).

This happens because template argument yields the canonical declaration of a specialization, and we add the parameters from that specialization into scope when instantiating the function definition (this is done by Sema::addInstantiatedParametersToScope).

sdkrystian avatar Feb 02 '24 17:02 sdkrystian

See https://cplusplus.github.io/CWG/issues/1001.html for some related fun

cor3ntin avatar Feb 02 '24 20:02 cor3ntin

The behavior of clang is correct here. https://eel.is/c++draft/temp.deduct.general#3 Here in the example we have similar case. template <class X> void g(const X x); the function template is defined and // # 3: function type is g(int), x is const g<int>(1); // # 4: function type is g(int), x is const g<const int>(1); in the both the case is const

mahtohappy avatar Feb 04 '24 21:02 mahtohappy

@mahtohappy

  1. Example in the Standard is not concerned with type deduction: it passes template arguments explicitly. Whereas in this issue type deduction is happening.
  2. Example in the Standard has single declaration (with definition) for each function template. In this example we have two declarations, and I still consider it not proven than they correspond (i.e. they are not two distinct primary templates).

Endilll avatar Feb 05 '24 07:02 Endilll

https://eel.is/c++draft/dcl.fct#5 in this example

void f(char[]) {}               // defines #1
void f(const char*) {}          // OK, another overload

the const makes it another overload function. Similar way,

template<typename U>
int f(const U t); //first

and

template<typename U>
int f(U t); //second

should be two different overload and should be chosen based on the argument we're passing. If the argument is int it compiler should choose second function and if argument is const int compiler should choose first to call. Is my correct here? https://godbolt.org/z/ddeM7f9b8 here clang complains about redefinition of f which it shouldn't, right? https://godbolt.org/z/djWE17vxc in our example, we only have declaration of first and and definition of second, as we're passing int in the function call it should use the //second function to call, where the v isn't const and the static assert should pass.

mahtohappy avatar Feb 05 '24 13:02 mahtohappy

Function templates are not functions, so it's not obvious that [dcl.fct]/5 applies to them. The fact that CWG1322 "Function parameter type decay in templates" is still not resolved underlines that.

Endilll avatar Feb 05 '24 15:02 Endilll

See also the proposed resolution for CWG1668 and relevant discussion on the core reflector.

Assuming template<typename T> void f(T t); and template<typename T> void f(const T t); declare the same function template, the consensus seems to be that the type of t is int in the first declaration and const int in the second when T is int.

sdkrystian avatar Feb 05 '24 17:02 sdkrystian

https://godbolt.org/z/TMMGbc73s but if you try to define these function they're complaining of redefinition. Is this also wrong?

mahtohappy avatar Feb 06 '24 04:02 mahtohappy