]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: ICE with scoped member variable template-id [PR123143]
authorMarek Polacek <polacek@redhat.com>
Wed, 25 Feb 2026 21:05:17 +0000 (16:05 -0500)
committerMarek Polacek <polacek@redhat.com>
Sun, 1 Mar 2026 00:05:26 +0000 (19:05 -0500)
In this test we crash in lookup_member with

  s.S::template a<42> == 42

but not without the "S::".  The problem is that lookup_member gets
a TEMPLATE_DECL and not an identifier.

In tsubst_expr/COMPONENT_REF for variable templates we should not
go to the "Lookup the template functions" block; we should let
finish_class_member_access_expr do the job.

PR c++/123143

gcc/cp/ChangeLog:

* pt.cc (tsubst_expr) <case COMPONENT_REF>: Check identifier_p
before doing lookup for a template function.

gcc/testsuite/ChangeLog:

* g++.dg/cpp1y/var-templ88.C: New test.

Reviewed-by: Jason Merrill <jason@redhat.com>
gcc/cp/pt.cc
gcc/testsuite/g++.dg/cpp1y/var-templ88.C [new file with mode: 0644]

index bcc1525ff8b6911041980962ce825a0051c31f81..140d232aa85c980b97507f08fa358b05d39786da 100644 (file)
@@ -22453,7 +22453,8 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
              }
          }
        else if (TREE_CODE (member) == SCOPE_REF
-                && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
+                && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR
+                && identifier_p (TREE_OPERAND (TREE_OPERAND (member, 1), 0)))
          {
            /* Lookup the template functions now that we know what the
               scope is.  */
diff --git a/gcc/testsuite/g++.dg/cpp1y/var-templ88.C b/gcc/testsuite/g++.dg/cpp1y/var-templ88.C
new file mode 100644 (file)
index 0000000..6316006
--- /dev/null
@@ -0,0 +1,20 @@
+// PR c++/123143
+// { dg-do compile { target c++14 } }
+
+constexpr int x = 42;
+struct S { static constexpr int x = 20; template <int N> static constexpr int a = N; };
+
+template<typename>
+void
+f ()
+{
+  constexpr S s;
+  static_assert (s.template a<42> == 42, "");
+  static_assert (s.S::template a<42> == 42, "");
+}
+
+void
+g ()
+{
+  f<int>();
+}