diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -12794,6 +12794,12 @@ return; } + // Reference type without initializer doesn't work. + if (VD->getType()->isReferenceType()) { + VD->setInvalidDecl(); + return; + } + QualType Ty = VD->getType(); if (Ty->isDependentType()) return; diff --git a/clang/test/AST/ast-dump-invalid-initialized.cpp b/clang/test/AST/ast-dump-invalid-initialized.cpp --- a/clang/test/AST/ast-dump-invalid-initialized.cpp +++ b/clang/test/AST/ast-dump-invalid-initialized.cpp @@ -24,4 +24,6 @@ auto b4 = A(1); // CHECK: `-VarDecl {{.*}} invalid b5 'auto' auto b5 = A{1}; -} \ No newline at end of file + // CHECK: `-VarDecl {{.*}} invalid b6 'A &' + A& b6 = garbage[1]; +} diff --git a/clang/test/SemaCXX/arrow-operator.cpp b/clang/test/SemaCXX/arrow-operator.cpp --- a/clang/test/SemaCXX/arrow-operator.cpp +++ b/clang/test/SemaCXX/arrow-operator.cpp @@ -65,3 +65,24 @@ } } // namespace arrow_suggest + +namespace no_crash_dependent_type { + +template +struct A { + void call(); + A* operator->(); +}; + +template +void foo() { + // x is dependent. + A& x = blah[7]; // expected-error {{use of undeclared identifier 'blah'}} + x->call(); +} + +void test() { + foo(); +} + +} // namespace no_crash_dependent_type