[PATCH] D76592: [Parser] Fix the assertion crash in ActOnStartOfSwitch stmt.

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 24 05:21:03 PDT 2020


hokein updated this revision to Diff 252280.
hokein added a comment.

adjust the assertion based on the offline discussion.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D76592/new/

https://reviews.llvm.org/D76592

Files:
  clang/lib/Sema/SemaStmt.cpp
  clang/test/Parser/switch-typo-correction.cpp


Index: clang/test/Parser/switch-typo-correction.cpp
===================================================================
--- /dev/null
+++ clang/test/Parser/switch-typo-correction.cpp
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+namespace c { double xxx; } // expected-note{{'c::xxx' declared here}}
+namespace d { float xxx; }
+namespace z { namespace xxx {} }
+
+void crash() {
+  switch (xxx) {} // expected-error{{use of undeclared identifier 'xxx'; did you mean }}
+}
Index: clang/lib/Sema/SemaStmt.cpp
===================================================================
--- clang/lib/Sema/SemaStmt.cpp
+++ clang/lib/Sema/SemaStmt.cpp
@@ -730,11 +730,11 @@
 
   if (CondExpr && !CondExpr->isTypeDependent()) {
     // We have already converted the expression to an integral or enumeration
-    // type, when we parsed the switch condition. If we don't have an
-    // appropriate type now, enter the switch scope but remember that it's
-    // invalid.
-    assert(CondExpr->getType()->isIntegralOrEnumerationType() &&
-           "invalid condition type");
+    // type, when we parsed the switch condition. There are cases where we don't
+    // have an appropriate type, e.g. a typo-expr Cond was corrected to an
+    // inappropriate-type expr, we just returns an error.
+    if (!CondExpr->getType()->isIntegralOrEnumerationType())
+      return StmtError();
     if (CondExpr->isKnownToHaveBooleanValue()) {
       // switch(bool_expr) {...} is often a programmer error, e.g.
       //   switch(n && mask) { ... }  // Doh - should be "n & mask".


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D76592.252280.patch
Type: text/x-patch
Size: 1575 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200324/6c34143e/attachment.bin>


More information about the cfe-commits mailing list