hokein created this revision. hokein added a reviewer: sammccall. Herald added subscribers: usaxena95, kadircet, ilya-biryukov. Herald added a project: clang.
After we parse the switch condition, we don't do the type check for type-dependent expr (e.g. TypoExpr) (in Sema::CheckSwitchCondition), then the TypoExpr is corrected to an invalid-type expr (in Sema::MakeFullExpr) and passed to the ActOnStartOfSwitchStmt, which triggers the assertion. Fix https://github.com/clangd/clangd/issues/311 Repository: rG LLVM Github Monorepo 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; } +namespace d { float xxx; } +namespace z { namespace xxx {} } + +void crash() { + switch (xxx) {} // expected-error{{use of undeclared identifier 'xxx'}} +} Index: clang/lib/Sema/SemaStmt.cpp =================================================================== --- clang/lib/Sema/SemaStmt.cpp +++ clang/lib/Sema/SemaStmt.cpp @@ -706,6 +706,12 @@ llvm_unreachable("conversion functions are permitted"); } } SwitchDiagnoser(Cond); + // The TypoExpr might be corrected to a non-intergral-or-enum type in the + // later stage without the proper type check, which is invalid for switch + // condition, so we fail the check here (this would avoid emitting incorrect + // fixit). + if (llvm::isa<TypoExpr>(Cond)) + return ExprError(); ExprResult CondResult = PerformContextualImplicitConversion(SwitchLoc, Cond, SwitchDiagnoser);
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; } +namespace d { float xxx; } +namespace z { namespace xxx {} } + +void crash() { + switch (xxx) {} // expected-error{{use of undeclared identifier 'xxx'}} +} Index: clang/lib/Sema/SemaStmt.cpp =================================================================== --- clang/lib/Sema/SemaStmt.cpp +++ clang/lib/Sema/SemaStmt.cpp @@ -706,6 +706,12 @@ llvm_unreachable("conversion functions are permitted"); } } SwitchDiagnoser(Cond); + // The TypoExpr might be corrected to a non-intergral-or-enum type in the + // later stage without the proper type check, which is invalid for switch + // condition, so we fail the check here (this would avoid emitting incorrect + // fixit). + if (llvm::isa<TypoExpr>(Cond)) + return ExprError(); ExprResult CondResult = PerformContextualImplicitConversion(SwitchLoc, Cond, SwitchDiagnoser);
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits