Hello,

New bugfix for PR19377
(https://gcc.gnu.org/bugzilla/show_bug.cgi?id=19377). This is
basically an extension of what I did before for PR17314 except it also
fixes this other bug.

I hope I didn't over-comment in the code ... better to say too much
than too little! It's a niche bug so I thought it could do with a
little explanation.

Added 1 new regression test.

Bootstraps fine on x86_64-pc-linux-gnu for x86_64-pc-linux-gnu.

Hopefully no formatting problems; checked with git gcc-verify and
check_GNU_style.sh.

----------- REGRESSION ANALYSIS -----------

No regressions reported.

I only ran the c++ regression tests since this is a c++ front-end
diagnostics code bug (i.e. it should have no effect on compilation, or
any other languages).

G++ (CLEAN) RESULTS

# of expected passes        203705
# of unexpected failures    2
# of expected failures        989
# of unsupported tests        8714

G++ (PATCHED) RESULTS

# of expected passes        203717
# of unexpected failures    2
# of expected failures        989
# of unsupported tests        8714

The extra passes are from my new regression test.

Let me know if there are any issues.

Kind regards,
Anthony Sharp
From e064f8d010baee288c47cce1981be80515101f0d Mon Sep 17 00:00:00 2001
From: Anthony Sharp <anthonyshar...@gmail.com>
Date: Thu, 4 Feb 2021 12:01:59 +0000
Subject: [PATCH] c++: Check for using decl in private parent access [PR19377]

This bug was already mostly fixed by the patch for PR17314. This
patch continues that by ensuring that where a using decl is used,
causing an access failure to a child class because the using decl is
private, the compiler correctly points to the using decl as the
source of the problem.

Checks for the use of using decls in a parent that had
private access to decl (but the child had no access) in order
to ascertain the best diagnostic decl location.

gcc/cp/ChangeLog:

	* semantics.c (get_class_access_diagnostic_decl): New function.
	(enforce_access): Altered function.

gcc/testsuite/ChangeLog:

	* g++.dg/pr19377.C: New test.
---
 gcc/cp/semantics.c             | 93 +++++++++++++++++++++++++++-------
 gcc/testsuite/g++.dg/pr19377.C | 21 ++++++++
 2 files changed, 95 insertions(+), 19 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/pr19377.C

diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c
index 73834467fca..6d4ef683d65 100644
--- a/gcc/cp/semantics.c
+++ b/gcc/cp/semantics.c
@@ -256,6 +256,62 @@ pop_to_parent_deferring_access_checks (void)
     }
 }
 
+/* Called from enforce_access.  A class has attempted (but failed) to access
+   DECL.  It is already established that a baseclass of that class,
+   PARENT_BINFO, has private access to DECL.  Examine certain special cases to
+   generate a diagnostic decl location.  If no special cases are found, simply
+   return DECL.  */
+
+static tree
+get_class_access_diagnostic_decl (tree parent_binfo, tree decl)
+{
+  /* When a class is denied access to a decl in a baseclass, most of the
+     time it is because the decl itself was declared as private at the point
+     of declaration.  So, by default, DECL is at fault.
+
+     However, in C++, there are (at least) two situations in which a decl
+     can be private even though it was not originally defined as such.  */
+
+  /* These two situations only apply if a baseclass had private access to
+     DECL (this function is only called if that is the case).  We must however
+     also ensure that the reason the parent had private access wasn't simply
+     because it was declared as private in the parent.  */
+  if (context_for_name_lookup (decl) == BINFO_TYPE (parent_binfo))
+    return decl;
+
+  /* 1.  If the "using" keyword is used to inherit DECL within a baseclass,
+     this may cause DECL to be private, so we return the using statement as
+     the source of the problem.
+
+     Scan the fields of PARENT_BINFO and see if there are any using decls.  If
+     there are, see if they inherit DECL.  If they do, that's where DECL was
+     truly declared private.  */
+  for (tree parent_field = TYPE_FIELDS (BINFO_TYPE (parent_binfo));
+      parent_field;
+      parent_field = DECL_CHAIN (parent_field))
+    {
+      if (TREE_CODE (parent_field) == USING_DECL)
+	{
+	  if (cp_tree_equal (decl,
+			     lookup_member (parent_binfo,
+					    DECL_NAME (parent_field),
+					    /*protect=*/0,
+					    /*want_type=*/false,
+					    tf_warning_or_error)))
+	    return parent_field;
+	}
+    }
+
+  /* 2.  If decl was privately inherited by a baseclass of the current class,
+     then decl will be inaccessible, even though it may originally have
+     been accessible to deriving classes.  In that case, the fault lies with
+     the baseclass that used a private inheritance, so we return the
+     baseclass type as the source of the problem.
+
+     Since this is the last check, we just assume it's true.  */
+  return TYPE_NAME (BINFO_TYPE (parent_binfo));
+}
+
 /* If the current scope isn't allowed to access DECL along
    BASETYPE_PATH, give an error, or if we're parsing a function or class
    template, defer the access check to be performed at instantiation time.
@@ -317,34 +373,33 @@ enforce_access (tree basetype_path, tree decl, tree diag_decl,
 	diag_decl = strip_inheriting_ctors (diag_decl);
       if (complain & tf_error)
 	{
-	  /* We will usually want to point to the same place as
-	     diag_decl but not always.  */
+	  access_kind access_failure_reason = ak_none;
+
+	  /* By default, using the decl as the source of the problem will
+	     usually give correct results.  */
 	  tree diag_location = diag_decl;
-	  access_kind parent_access = ak_none;
 
-	  /* See if any of BASETYPE_PATH's parents had private access
-	     to DECL.  If they did, that will tell us why we don't.  */
+	  /* However, if a parent of BASETYPE_PATH had private access to decl,
+	     then it actually might be the case that the source of the problem
+	     is not DECL.  */
 	  tree parent_binfo = get_parent_with_private_access (decl,
-							      basetype_path);
+							       basetype_path);
 
-	  /* If a parent had private access, then the diagnostic
-	     location DECL should be that of the parent class, since it
-	     failed to give suitable access by using a private
-	     inheritance.  But if DECL was actually defined in the parent,
-	     it wasn't privately inherited, and so we don't need to do
-	     this, and complain_about_access will figure out what to
-	     do.  */
-	  if (parent_binfo != NULL_TREE
-	      && (context_for_name_lookup (decl)
-		  != BINFO_TYPE (parent_binfo)))
+	  /* So if a parent did had private access, then we need to do special
+	     checks to obtain the best diagnostic location decl.  */
+	  if (parent_binfo != NULL_TREE)
 	    {
-	      diag_location = TYPE_NAME (BINFO_TYPE (parent_binfo));
-	      parent_access = ak_private;
+	      diag_location = get_class_access_diagnostic_decl (parent_binfo,
+								 diag_decl);
+
+	      /* We also at this point know that the reason access failed was
+		 because decl was private.  */
+		 access_failure_reason = ak_private;
 	    }
 
 	  /* Finally, generate an error message.  */
 	  complain_about_access (decl, diag_decl, diag_location, true,
-				 parent_access);
+				 access_failure_reason);
 	}
       if (afi)
 	afi->record_access_failure (basetype_path, decl, diag_decl);
diff --git a/gcc/testsuite/g++.dg/pr19377.C b/gcc/testsuite/g++.dg/pr19377.C
new file mode 100644
index 00000000000..356329801ac
--- /dev/null
+++ b/gcc/testsuite/g++.dg/pr19377.C
@@ -0,0 +1,21 @@
+/* { dg-do assemble } */
+
+class A
+{
+  protected:
+  int i;
+};
+
+class B:public A
+{
+  private:
+  using A::i; // { dg-message "declared" }
+};
+
+class C:public B
+{
+  void f()
+  {
+    A::i = 0; // { dg-error "private" }
+  }
+};
\ No newline at end of file
-- 
2.25.1

Reply via email to