Index: lib/Sema/SemaDecl.cpp =================================================================== --- lib/Sema/SemaDecl.cpp +++ lib/Sema/SemaDecl.cpp @@ -4573,12 +4573,14 @@ RequireCompleteDeclContext(D.getCXXScopeSpec(), DC)) return nullptr; + // If a class is incomplete, do not parse entities inside it. if (isa(DC) && !cast(DC)->hasDefinition()) { Diag(D.getIdentifierLoc(), diag::err_member_def_undefined_record) << Name << DC << D.getCXXScopeSpec().getRange(); - D.setInvalidType(); - } else if (!D.getDeclSpec().isFriendSpecified()) { + return nullptr; + } + if (!D.getDeclSpec().isFriendSpecified()) { if (diagnoseQualifiedDeclaration(D.getCXXScopeSpec(), DC, Name, D.getIdentifierLoc())) { if (DC->isRecord()) Index: test/SemaCXX/incomplete-call.cpp =================================================================== --- test/SemaCXX/incomplete-call.cpp +++ test/SemaCXX/incomplete-call.cpp @@ -47,3 +47,15 @@ void test_incomplete_object_call(C& c) { c(); // expected-error{{incomplete type in call to object of type}} } + +namespace pr18542 { + struct X { + int count; + template class basic_istream; + template + void basic_istream::read() { // expected-error{{out-of-line definition of 'read' from class 'basic_istream' without definition}} + count = 0; + } + }; +} +