[clang] dfc8004 - [Clang][Sema] Fix crash when rebuilding MemberExprs with invalid object expressions (#97455)

via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 9 10:30:59 PDT 2024


Author: Krystian Stasiowski
Date: 2024-07-09T13:30:55-04:00
New Revision: dfc8004d0b0b85155dad0e9aca84716425b87676

URL: https://github.com/llvm/llvm-project/commit/dfc8004d0b0b85155dad0e9aca84716425b87676
DIFF: https://github.com/llvm/llvm-project/commit/dfc8004d0b0b85155dad0e9aca84716425b87676.diff

LOG: [Clang][Sema] Fix crash when rebuilding MemberExprs with invalid object expressions (#97455)

This patch fixes an assertion failure which occurs when the object expression of a `MemberExpr` is transformed into an expression with a dependent type for which the `DeclContext` cannot be computed (e.g. a `RecoveryExpr`). Fixes #95778.

Added: 
    

Modified: 
    clang/lib/Sema/TreeTransform.h
    clang/test/CXX/temp/temp.res/temp.dep/temp.dep.type/p4.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index 4450ebaf615cd..d26c5a97fa3d2 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -2896,6 +2896,9 @@ class TreeTransform {
     SS.Adopt(QualifierLoc);
 
     Base = BaseResult.get();
+    if (Base->containsErrors())
+      return ExprError();
+
     QualType BaseType = Base->getType();
 
     if (isArrow && !BaseType->isPointerType())

diff  --git a/clang/test/CXX/temp/temp.res/temp.dep/temp.dep.type/p4.cpp b/clang/test/CXX/temp/temp.res/temp.dep/temp.dep.type/p4.cpp
index 982e5372f5b0c..f32f49ef4539a 100644
--- a/clang/test/CXX/temp/temp.res/temp.dep/temp.dep.type/p4.cpp
+++ b/clang/test/CXX/temp/temp.res/temp.dep/temp.dep.type/p4.cpp
@@ -574,3 +574,34 @@ namespace N4 {
     }
   };
 } // namespace N4
+
+namespace N5 {
+  struct A {
+    int x;
+  };
+
+  template<typename T>
+  void f() {
+    A y = T::x; // expected-error {{type 'int' cannot be used prior to '::' because it has no members}}
+    y.x;
+  }
+
+  template void f<int>(); // expected-note {{in instantiation of}}
+
+  struct B {
+    template<typename T>
+    B(T&&);
+
+    int x;
+  };
+
+  template<typename T>
+  void g(T y) {
+    B z([&]() { // expected-note {{while substituting into a lambda expression here}}
+      h(&y); // expected-error {{use of undeclared identifier 'h'}}
+    });
+    z.x;
+  }
+
+  template void g(int); // expected-note {{in instantiation of}}
+} // namespace N5


        


More information about the cfe-commits mailing list