]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: non-dep init folding and access checking [PR109480]
authorPatrick Palka <ppalka@redhat.com>
Sun, 7 May 2023 14:24:52 +0000 (10:24 -0400)
committerPatrick Palka <ppalka@redhat.com>
Sun, 7 May 2023 14:24:52 +0000 (10:24 -0400)
enforce_access currently checks processing_template_decl to decide
whether to defer the given access check until instantiation time.
But using this flag is unreliable because it gets cleared during e.g.
non-dependent initializer folding, and so can lead to premature access
check failures as in the below testcase.  It seems better to check
current_template_parms instead.

PR c++/109480

gcc/cp/ChangeLog:

* semantics.cc (enforce_access): Check current_template_parms
instead of processing_template_decl when deciding whether to
defer the access check.

gcc/testsuite/ChangeLog:

* g++.dg/template/non-dependent25a.C: New test.

gcc/cp/semantics.cc
gcc/testsuite/g++.dg/template/non-dependent25a.C [new file with mode: 0644]

index 9ba316ab3be4176ee1517328e2f9612140069c5c..474da71bff62adc7b2629090d4f5b1d1844f3f52 100644 (file)
@@ -346,7 +346,7 @@ enforce_access (tree basetype_path, tree decl, tree diag_decl,
     }
 
   tree cs = current_scope ();
-  if (processing_template_decl
+  if (current_template_parms
       && (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL))
     if (tree template_info = get_template_info (cs))
       {
diff --git a/gcc/testsuite/g++.dg/template/non-dependent25a.C b/gcc/testsuite/g++.dg/template/non-dependent25a.C
new file mode 100644 (file)
index 0000000..902e537
--- /dev/null
@@ -0,0 +1,17 @@
+// PR c++/109480
+// A version of non-dependent25.C where b's initializer is a constant
+// expression.
+// { dg-do compile { target c++11 } }
+
+template<class T>
+struct A {
+  void f() {
+    constexpr A<int> a;
+    const bool b = a.g(); // { dg-bogus "private" }
+  }
+
+private:
+  constexpr bool g() const { return true; }
+};
+
+template struct A<int>;