[clang] [clang-repl] Allow private type aliases in out-of-line member function return types (PR #178842)

via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 9 06:12:45 PST 2026


https://github.com/fogsong233 updated https://github.com/llvm/llvm-project/pull/178842

>From d79e92e4b955be1f134a91ea47761043ddf85aab Mon Sep 17 00:00:00 2001
From: fogsong233 <fogsong233 at gmail.com>
Date: Fri, 30 Jan 2026 14:37:57 +0800
Subject: [PATCH 1/4] fix.

---
 clang/lib/Parse/ParseTentative.cpp                | 13 ++++++++++++-
 clang/test/Interpreter/disambiguate-decl-stmt.cpp |  5 ++---
 2 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/clang/lib/Parse/ParseTentative.cpp b/clang/lib/Parse/ParseTentative.cpp
index 9622a00687ca5..2a24256929ea9 100644
--- a/clang/lib/Parse/ParseTentative.cpp
+++ b/clang/lib/Parse/ParseTentative.cpp
@@ -12,6 +12,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "clang/Parse/Parser.h"
+#include "clang/Parse/RAIIObjectsForParser.h"
 #include "clang/Sema/ParsedTemplate.h"
 using namespace clang;
 
@@ -78,7 +79,17 @@ bool Parser::isCXXDeclarationStatement(
     [[fallthrough]];
     // simple-declaration
   default:
-    return isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false);
+    // Ignore access restrictions during tentative parsing. A private type used
+    // as a return value would otherwise trigger a lookup error. We only need
+    // to identify if the sequence represents a type; the actual access rights
+    // will be verified during formal parsing when the member function's
+    // context is fully known.
+    if (DisambiguatingWithExpression) {
+      SuppressAccessChecks AccessExporter(*this, true);
+      return isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false);
+    } else {
+      return isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false);
+    }
   }
 }
 
diff --git a/clang/test/Interpreter/disambiguate-decl-stmt.cpp b/clang/test/Interpreter/disambiguate-decl-stmt.cpp
index 1f4d5e267288b..24902cee8ed1f 100644
--- a/clang/test/Interpreter/disambiguate-decl-stmt.cpp
+++ b/clang/test/Interpreter/disambiguate-decl-stmt.cpp
@@ -70,11 +70,10 @@ class PrivateUsingFriendVar { static PrivateUsingFriend::T i; };
 PrivateUsingFriend::T PrivateUsingFriendVar::i = 42;
 
 // The following should still diagnose (inspired by PR13642)
-// FIXME: Should not be diagnosed twice!
 class PR13642 { class Inner { public: static int i; }; };
-// expected-note at -1 2 {{implicitly declared private here}}
+// expected-note at -1 {{implicitly declared private here}}
 PR13642::Inner::i = 5;
-// expected-error at -1 2 {{'Inner' is a private member of 'PR13642'}}
+// expected-error at -1 {{'Inner' is a private member of 'PR13642'}}
 
 // Deduction guide
 template<typename T> struct A { A(); A(T); };

>From 39ee8a8b56dcc1355e07cfa1268ea46ae7ee40b4 Mon Sep 17 00:00:00 2001
From: fogsong233 <fogsong233 at gmail.com>
Date: Sun, 8 Feb 2026 18:51:00 +0800
Subject: [PATCH 2/4] using revert

---
 clang/lib/Parse/ParseTentative.cpp | 18 ++++++++++++------
 clang/test/Interpreter/access.cpp  | 15 +++++++++++++++
 2 files changed, 27 insertions(+), 6 deletions(-)
 create mode 100644 clang/test/Interpreter/access.cpp

diff --git a/clang/lib/Parse/ParseTentative.cpp b/clang/lib/Parse/ParseTentative.cpp
index 2a24256929ea9..985563d737a93 100644
--- a/clang/lib/Parse/ParseTentative.cpp
+++ b/clang/lib/Parse/ParseTentative.cpp
@@ -79,14 +79,20 @@ bool Parser::isCXXDeclarationStatement(
     [[fallthrough]];
     // simple-declaration
   default:
-    // Ignore access restrictions during tentative parsing. A private type used
-    // as a return value would otherwise trigger a lookup error. We only need
-    // to identify if the sequence represents a type; the actual access rights
-    // will be verified during formal parsing when the member function's
-    // context is fully known.
+
     if (DisambiguatingWithExpression) {
+      TentativeParsingAction TPA(*this, true);
+      // We will do not check access checks here, because we want to allow
+      // parsing of declarations. Access will be checked later.
       SuppressAccessChecks AccessExporter(*this, true);
-      return isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false);
+      if (isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false)) {
+        // Do not annotate the tokens, otherwise access will be neglected later.
+        TPA.Revert();
+        return true;
+      }
+      TPA.Commit();
+      return false;
+
     } else {
       return isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false);
     }
diff --git a/clang/test/Interpreter/access.cpp b/clang/test/Interpreter/access.cpp
new file mode 100644
index 0000000000000..3b23c2947d319
--- /dev/null
+++ b/clang/test/Interpreter/access.cpp
@@ -0,0 +1,15 @@
+// REQUIRES: host-supports-jit
+// RUN: cat %s | clang-repl 2>&1 | FileCheck %s
+
+struct A { };
+
+class B { using u = A; const static int p = 1; public: u *foo(); };
+
+B::u* foo() { return nullptr; } 
+// CHECK: error: 'u' is a private member of 'B'
+
+B::u * B::foo() { return nullptr; }
+// CHECK-NOT: error: 'u' is a private member of 'B'
+
+int p = B::p; 
+// CHECK: error: 'p' is a private member of 'B'
\ No newline at end of file

>From 2dffdee5173635881c62f09b7e1fc74d931eb331 Mon Sep 17 00:00:00 2001
From: fogsong233 <kacent at clice.io>
Date: Mon, 9 Feb 2026 18:07:56 +0800
Subject: [PATCH 3/4] remove else return

Co-authored-by: Vassil Vassilev <v.g.vassilev at gmail.com>
---
 clang/lib/Parse/ParseTentative.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Parse/ParseTentative.cpp b/clang/lib/Parse/ParseTentative.cpp
index 985563d737a93..639b6f9b80663 100644
--- a/clang/lib/Parse/ParseTentative.cpp
+++ b/clang/lib/Parse/ParseTentative.cpp
@@ -93,8 +93,8 @@ bool Parser::isCXXDeclarationStatement(
       TPA.Commit();
       return false;
 
-    } else {
-      return isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false);
+    }
+    return isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false);
     }
   }
 }

>From d59cdad8084c865bd46255fecd0ce664cce02401 Mon Sep 17 00:00:00 2001
From: fogsong233 <fogsong233 at gmail.com>
Date: Mon, 9 Feb 2026 22:12:30 +0800
Subject: [PATCH 4/4] remove an extra brace

---
 clang/lib/Parse/ParseTentative.cpp | 1 -
 1 file changed, 1 deletion(-)

diff --git a/clang/lib/Parse/ParseTentative.cpp b/clang/lib/Parse/ParseTentative.cpp
index d34f3fb7e4e0f..0b677f13f3082 100644
--- a/clang/lib/Parse/ParseTentative.cpp
+++ b/clang/lib/Parse/ParseTentative.cpp
@@ -96,7 +96,6 @@ bool Parser::isCXXDeclarationStatement(
     }
     return isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false);
     }
-  }
 }
 
 bool Parser::isCXXSimpleDeclaration(bool AllowForRangeDecl) {



More information about the cfe-commits mailing list