]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: base-specifier name lookup is type-only [PR122192]
authorPatrick Palka <ppalka@redhat.com>
Fri, 10 Oct 2025 14:25:25 +0000 (10:25 -0400)
committerPatrick Palka <ppalka@redhat.com>
Tue, 28 Oct 2025 13:59:11 +0000 (09:59 -0400)
The r13-6098 change to make TYPENAME_TYPE no longer always ignore
non-type bindings needs another exception: base-specifiers that are
represented as TYPENAME_TYPE, for which lookup must be type-only (by
[class.derived.general]/2).  This patch fixes this by giving such
TYPENAME_TYPEs a tag type of class_type rather than typename_type so
that we treat them like elaborated-type-specifiers (another type-only
lookup situation).

PR c++/122192

gcc/cp/ChangeLog:

* decl.cc (make_typename_type): Document base-specifier as
another type-only lookup case.
* parser.cc (cp_parser_class_name): Propagate tag_type to
make_typename_type instead of hardcoding typename_type.
(cp_parser_base_specifier): Pass class_type instead of
typename_type as tag_type to cp_parser_class_name.

gcc/testsuite/ChangeLog:

* g++.dg/template/dependent-base6.C: New test.

Reviewed-by: Jason Merrill <jason@redhat.com>
(cherry picked from commit 49ddf362f0a7c1fdeb62f13a852a2fdec9d6fe6d)

gcc/cp/decl.cc
gcc/cp/parser.cc
gcc/testsuite/g++.dg/template/dependent-base6.C [new file with mode: 0644]

index 9cf75936d7af1ce594a68ddcf94a6d61b9443ee8..6d70502aae11ebbd4ec360fbda8d32c8c80f9d69 100644 (file)
@@ -4546,7 +4546,9 @@ make_typename_type (tree context, tree name, enum tag_types tag_type,
           - the tag corresponds to a class-key or 'enum' so
             [basic.lookup.elab] applies, or
           - the tag corresponds to scope_type or tf_qualifying_scope is
-            set so [basic.lookup.qual]/1 applies.
+            set so [basic.lookup.qual]/1 applies, or
+          - we're inside a base-specifier so [class.derived.general]/2 applies;
+            the tag will already be class_type in that case.
         TODO: If we'd set/track the scope_type tag thoroughly on all
         TYPENAME_TYPEs that are followed by :: then we wouldn't need the
         tf_qualifying_scope flag.  */
index 7f1f62c76e7cc3637a36cab33cf31bec1272867b..d5ba1bf3c1c5a0a598a4eb55ea1481f971b47f6a 100644 (file)
@@ -27406,8 +27406,7 @@ cp_parser_class_name (cp_parser *parser,
   /* If this is a typename, create a TYPENAME_TYPE.  */
   if (typename_p && decl != error_mark_node)
     {
-      decl = make_typename_type (scope, decl, typename_type,
-                                /*complain=*/tf_error);
+      decl = make_typename_type (scope, decl, tag_type, /*complain=*/tf_error);
       if (decl != error_mark_node)
        decl = TYPE_NAME (decl);
     }
@@ -29748,7 +29747,7 @@ cp_parser_base_specifier (cp_parser* parser)
       type = cp_parser_class_name (parser,
                                   class_scope_p,
                                   template_p,
-                                  typename_type,
+                                  class_type,
                                   /*check_dependency_p=*/true,
                                   /*class_head_p=*/false,
                                   /*is_declaration=*/true);
diff --git a/gcc/testsuite/g++.dg/template/dependent-base6.C b/gcc/testsuite/g++.dg/template/dependent-base6.C
new file mode 100644 (file)
index 0000000..b4bc5c2
--- /dev/null
@@ -0,0 +1,12 @@
+// PR c++/122192
+// Verify name lookup within a base-specifier is type-only.
+
+struct A {
+  int B;
+  struct B { };
+};
+
+struct S1 : A::B { }; // OK
+
+template<class T> struct S2 : T::B { }; // OK, used to fail
+template struct S2<A>;