Bug 96330 - Constexpr variables cannot be used in the template context.
Summary: Constexpr variables cannot be used in the template context.
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 10.1.0
: P3 normal
Target Milestone: 11.0
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2020-07-27 11:55 UTC by steve02081504
Modified: 2021-03-06 15:26 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description steve02081504 2020-07-27 11:55:47 UTC
demo code:
struct foo_t {
    template<class T>
        static constexpr bool bar = true;
};
inline constexpr foo_t foo{};

template<class T>
    struct baz {
        static_assert(foo.bar<T>); // bug in clang (before instantiation) and gcc (during instantiation)
        static_assert(foo_t::bar<T>); // OK
    };

int main()
{
    static_assert(foo.bar<void>, ""); // OK
    static_assert(foo_t::bar<void>, ""); // OK
    static_cast<void>(baz<void>{});
}
Comment 1 Jakub Jelinek 2020-07-27 15:35:54 UTC
Tried
--- gcc/cp/pt.c.jj	2020-07-27 10:38:19.000000000 +0200
+++ gcc/cp/pt.c	2020-07-27 17:25:09.748240198 +0200
@@ -16838,14 +16838,17 @@ tsubst_copy (tree t, tree args, tsubst_f
     case TEMPLATE_ID_EXPR:
       {
 	/* Substituted template arguments */
-	tree fn = TREE_OPERAND (t, 0);
+	tree expr = TREE_OPERAND (t, 0);
 	tree targs = TREE_OPERAND (t, 1);
 
-	fn = tsubst_copy (fn, args, complain, in_decl);
+	expr = tsubst_copy (expr, args, complain, in_decl);
 	if (targs)
 	  targs = tsubst_template_args (targs, args, complain, in_decl);
 
-	return lookup_template_function (fn, targs);
+        if (variable_template_p (expr))
+	  return lookup_and_finish_template_variable (expr, targs, complain);
+	else
+	  return lookup_template_function (expr, targs);
       }
 
     case TREE_LIST:
but that just means an ICE elsewhere.
Comment 2 Patrick Palka 2021-03-05 16:43:45 UTC
(In reply to Jakub Jelinek from comment #1)
> Tried
> --- gcc/cp/pt.c.jj	2020-07-27 10:38:19.000000000 +0200
> +++ gcc/cp/pt.c	2020-07-27 17:25:09.748240198 +0200
> @@ -16838,14 +16838,17 @@ tsubst_copy (tree t, tree args, tsubst_f
>      case TEMPLATE_ID_EXPR:
>        {
>  	/* Substituted template arguments */
> -	tree fn = TREE_OPERAND (t, 0);
> +	tree expr = TREE_OPERAND (t, 0);
>  	tree targs = TREE_OPERAND (t, 1);
>  
> -	fn = tsubst_copy (fn, args, complain, in_decl);
> +	expr = tsubst_copy (expr, args, complain, in_decl);
>  	if (targs)
>  	  targs = tsubst_template_args (targs, args, complain, in_decl);
>  
> -	return lookup_template_function (fn, targs);
> +        if (variable_template_p (expr))
> +	  return lookup_and_finish_template_variable (expr, targs, complain);
> +	else
> +	  return lookup_template_function (expr, targs);
>        }
>  
>      case TREE_LIST:
> but that just means an ICE elsewhere.

Hmm, seems using lookup_template_variable instead of lookup_and_finish_template_variable works.
Comment 3 GCC Commits 2021-03-06 05:08:14 UTC
The master branch has been updated by Patrick Palka <ppalka@gcc.gnu.org>:

https://gcc.gnu.org/g:574e7601829733d7cae20b5dc7034b876cc76b30

commit r11-7541-g574e7601829733d7cae20b5dc7034b876cc76b30
Author: Patrick Palka <ppalka@redhat.com>
Date:   Sat Mar 6 00:07:43 2021 -0500

    c++: Fix tsubsting member variable template-id [PR96330]
    
    This makes tsubst_copy appropriately handle a variable template-id, which
    in turn fixes tsubsting a COMPONENT_REF whose member operand is known at
    parse time to be a variable template-id, as in the initialization of 'x'
    in the first testcase.  Previously, we rejected this testcase with the
    error "foo_t::bar<T> is not a function template", issued from
    lookup_template_fuction.
    
    We were already properly handling the analagous case where the object
    operand of the COMPONENT_REF is dependent (and so the member operand is
    a dependent template name), but there doesn't seems to be existing test
    coverage for this, hence the second testcase below.
    
    gcc/cp/ChangeLog:
    
            PR c++/96330
            * pt.c (tsubst_copy) <case TEMPLATE_ID_EXPR>: Rename local
            variable 'fn' to 'tmpl'.  Handle a variable template-id by
            calling lookup_template_variable.
    
    gcc/testsuite/ChangeLog:
    
            PR c++/96330
            * g++.dg/cpp1y/var-templ68.C: New test.
            * g++.dg/cpp1y/var-templ68a.C: New test.
    
    Co-authored-by: Jakub Jelinek <jakub@redhat.com>
Comment 4 Patrick Palka 2021-03-06 15:26:58 UTC
Fixed for GCC 11.  Thanks for the bug report!