diff mbox

Fix PR c++/69283 (auto deduction fails when ADL is required)

Message ID 1453175728-13354-1-git-send-email-patrick@parcs.ath.cx
State New
Headers show

Commit Message

Patrick Palka Jan. 19, 2016, 3:55 a.m. UTC
mark_used is wrongly diagnosing a use of a TEMPLATE_DECL (e.g. the call
to f1 in function f3 of auto-fn29.C below) for having an undeduced
'auto' return type.  This doesn't make sense, because an 'auto' used
inside a template doesn't get deduced until after the template is
instantiated.  So for a TEMPLATE_DECL we shouldn't diagnose a use of
undeduced 'auto' here.  After instantiation, presumably we will call
mark_used on the resulting FUNCTION_DECL which will check for undeduced
auto appropriately.

Bootstrapped and regtested on x86_64-pc-linux-gnu, no new regressions.
Is this OK to commit?

gcc/cp/ChangeLog:

	PR c++/69283
	PR c++/67835
	* decl2.c (mark_used): Don't complain about an undeduced 'auto'
	in a TEMPLATE_DECL.

gcc/testsuite/ChangeLog:

	PR c++/69283
	PR c++/67835
	* g++.dg/cpp1y/auto-fn29.C: New test.
	* g++.dg/cpp1y/auto-fn30.C: New test.
	* g++.dg/cpp1y/auto-fn31.C: New test.
---
 gcc/cp/decl2.c                         |  4 +++-
 gcc/testsuite/g++.dg/cpp1y/auto-fn29.C | 23 +++++++++++++++++++++++
 gcc/testsuite/g++.dg/cpp1y/auto-fn30.C | 21 +++++++++++++++++++++
 gcc/testsuite/g++.dg/cpp1y/auto-fn31.C | 22 ++++++++++++++++++++++
 4 files changed, 69 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp1y/auto-fn29.C
 create mode 100644 gcc/testsuite/g++.dg/cpp1y/auto-fn30.C
 create mode 100644 gcc/testsuite/g++.dg/cpp1y/auto-fn31.C

Comments

Jason Merrill Jan. 19, 2016, 2:56 p.m. UTC | #1
On 01/18/2016 10:55 PM, Patrick Palka wrote:
> mark_used is wrongly diagnosing a use of a TEMPLATE_DECL (e.g. the call
> to f1 in function f3 of auto-fn29.C below) for having an undeduced
> 'auto' return type.  This doesn't make sense, because an 'auto' used
> inside a template doesn't get deduced until after the template is
> instantiated.  So for a TEMPLATE_DECL we shouldn't diagnose a use of
> undeduced 'auto' here.  After instantiation, presumably we will call
> mark_used on the resulting FUNCTION_DECL which will check for undeduced
> auto appropriately.
> @@ -5112,7 +5112,9 @@ mark_used (tree decl, tsubst_flags_t complain)
>         || DECL_LANG_SPECIFIC (decl) == NULL
>         || DECL_THUNK_P (decl))
>       {
> -      if (!processing_template_decl && type_uses_auto (TREE_TYPE (decl)))
> +      if (!processing_template_decl
> +	  && TREE_CODE (decl) != TEMPLATE_DECL
> +	  && type_uses_auto (TREE_TYPE (decl)))

How does a TEMPLATE_DECL get in here?  Does it have null DECL_LANG_SPECIFIC?

I'd think mark_used of a TEMPLATE_DECL should return after setting 
TREE_USED, there's nothing else to do with it.

Jason
Patrick Palka Jan. 19, 2016, 3:41 p.m. UTC | #2
On Tue, Jan 19, 2016 at 9:56 AM, Jason Merrill <jason@redhat.com> wrote:
> On 01/18/2016 10:55 PM, Patrick Palka wrote:
>>
>> mark_used is wrongly diagnosing a use of a TEMPLATE_DECL (e.g. the call
>> to f1 in function f3 of auto-fn29.C below) for having an undeduced
>> 'auto' return type.  This doesn't make sense, because an 'auto' used
>> inside a template doesn't get deduced until after the template is
>> instantiated.  So for a TEMPLATE_DECL we shouldn't diagnose a use of
>> undeduced 'auto' here.  After instantiation, presumably we will call
>> mark_used on the resulting FUNCTION_DECL which will check for undeduced
>> auto appropriately.
>> @@ -5112,7 +5112,9 @@ mark_used (tree decl, tsubst_flags_t complain)
>>         || DECL_LANG_SPECIFIC (decl) == NULL
>>         || DECL_THUNK_P (decl))
>>       {
>> -      if (!processing_template_decl && type_uses_auto (TREE_TYPE (decl)))
>> +      if (!processing_template_decl
>> +         && TREE_CODE (decl) != TEMPLATE_DECL
>> +         && type_uses_auto (TREE_TYPE (decl)))
>
>
> How does a TEMPLATE_DECL get in here?  Does it have null DECL_LANG_SPECIFIC?

(In the test case auto-fn29.C,) When instantiating the template
function f3,we call tsubst on the CALL_EXPR "f1 (v);".  There, ADL is
performed on the identifier f1 (which is the CALL_EXPR_FN) which
returns the TEMPLATE_DECL f1.  Then mark_used is called on this
CALL_EXPR_FN, only if it's a decl.

If in the test case the call to "f1 (v);" is replaced with "Ape::f1
(v);" then the CALL_EXPR_FN is then an OVERLOAD (to the TEMPLATE_DECL
f1), i.e. not a decl, so we don't call mark_used on it in tsubst.

The DECL_LANG_SPECIFIC of this decl is not null.

>
> I'd think mark_used of a TEMPLATE_DECL should return after setting
> TREE_USED, there's nothing else to do with it.

Consider it changed.

>
> Jason
>
>
diff mbox

Patch

diff --git a/gcc/cp/decl2.c b/gcc/cp/decl2.c
index a7212ca0..849375f 100644
--- a/gcc/cp/decl2.c
+++ b/gcc/cp/decl2.c
@@ -5112,7 +5112,9 @@  mark_used (tree decl, tsubst_flags_t complain)
       || DECL_LANG_SPECIFIC (decl) == NULL
       || DECL_THUNK_P (decl))
     {
-      if (!processing_template_decl && type_uses_auto (TREE_TYPE (decl)))
+      if (!processing_template_decl
+	  && TREE_CODE (decl) != TEMPLATE_DECL
+	  && type_uses_auto (TREE_TYPE (decl)))
 	{
 	  if (complain & tf_error)
 	    error ("use of %qD before deduction of %<auto%>", decl);
diff --git a/gcc/testsuite/g++.dg/cpp1y/auto-fn29.C b/gcc/testsuite/g++.dg/cpp1y/auto-fn29.C
new file mode 100644
index 0000000..cd10460
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/auto-fn29.C
@@ -0,0 +1,23 @@ 
+// PR c++/69283
+// { dg-do compile { target c++14 } }
+
+namespace Ape {
+   struct Type {};
+
+   template <typename T>
+   auto f1(T const& v){
+       return true;
+   }
+}
+
+namespace Baboon {
+   template <typename T>
+   bool f3(T const& v){
+       return f1(v);
+   }
+}
+
+int main(){
+   Ape::Type x;
+   Baboon::f3(x);
+}
diff --git a/gcc/testsuite/g++.dg/cpp1y/auto-fn30.C b/gcc/testsuite/g++.dg/cpp1y/auto-fn30.C
new file mode 100644
index 0000000..e005e6e
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/auto-fn30.C
@@ -0,0 +1,21 @@ 
+// PR c++/67835
+// { dg-do compile { target c++14 } }
+
+template<class Tag, class T>
+auto g(Tag tag, T x) {
+ return f(tag, x);
+}
+
+namespace abc {
+struct tag {};
+
+struct A {};
+
+template<class T>
+auto f(tag, T x) { return x; }
+}
+
+int main() {
+ g(abc::tag(), abc::A());
+ return 0;
+}
diff --git a/gcc/testsuite/g++.dg/cpp1y/auto-fn31.C b/gcc/testsuite/g++.dg/cpp1y/auto-fn31.C
new file mode 100644
index 0000000..7849632
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/auto-fn31.C
@@ -0,0 +1,22 @@ 
+// { dg-do compile { target c++14 } }
+
+namespace Ape {
+   struct Type {};
+
+   template <typename T>
+   auto f1(T const& v){
+        return f1(v); // { dg-error "auto" }
+   }
+}
+
+namespace Baboon {
+   template <typename T>
+   void f3(T const& v){
+       f1(v);
+   }
+}
+
+int main(){
+   Ape::Type x;
+   Baboon::f3(x);
+}