diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp --- a/clang/lib/Sema/SemaStmt.cpp +++ b/clang/lib/Sema/SemaStmt.cpp @@ -2434,8 +2434,11 @@ QualType RangeType = Range->getType(); if (RequireCompleteType(RangeLoc, RangeType, - diag::err_for_range_incomplete_type)) + diag::err_for_range_incomplete_type)) { + if (LoopVar->getType()->isUndeducedType()) + LoopVar->setInvalidDecl(); return StmtError(); + } // Build auto __begin = begin-expr, __end = end-expr. // Divide by 2, since the variables are in the inner scope (loop body). diff --git a/clang/test/SemaCXX/for-range-crash.cpp b/clang/test/SemaCXX/for-range-crash.cpp new file mode 100644 --- /dev/null +++ b/clang/test/SemaCXX/for-range-crash.cpp @@ -0,0 +1,10 @@ +// RUN: %clang_cc1 -fsyntax-only -verify -std=c++17 %s + +template +class Bar { + Bar *variables_to_modify; + foo() { // expected-error {{C++ requires a type specifier for all declarations}} + for (auto *c : *variables_to_modify) + delete c; + } +}; \ No newline at end of file