diff --git a/core/metacling/src/TClingCallbacks.cxx b/core/metacling/src/TClingCallbacks.cxx index edbd33107192a..5a6fe519f27b3 100644 --- a/core/metacling/src/TClingCallbacks.cxx +++ b/core/metacling/src/TClingCallbacks.cxx @@ -845,6 +845,12 @@ bool TClingCallbacks::tryResolveAtRuntimeInternal(LookupResult &R, Scope *S) { return false; } + // Prevent redundant declarations for control statements (e.g., for, if, while) + // that have already been annotated. + if (auto annot = Wrapper->getAttr()) + if (annot->getAnnotation().equals("__ResolveAtRuntime") && S->isControlScope()) + return false; + VarDecl* Result = VarDecl::Create(C, TU, Loc, Loc, II, C.DependentTy, /*TypeSourceInfo*/nullptr, SC_None); diff --git a/core/metacling/test/TClingTests.cxx b/core/metacling/test/TClingTests.cxx index 5caba0a7ea2f0..171bd3322092c 100644 --- a/core/metacling/test/TClingTests.cxx +++ b/core/metacling/test/TClingTests.cxx @@ -422,3 +422,16 @@ TEST_F(TClingTests, RefreshNSShadowing) gInterpreter->Declare("namespace std { namespace Detail {} }; auto c = TClass::GetClass(\"Detail\");"); gInterpreter->ProcessLine("namespace Detail {}"); } + +// #8367 +TEST_F(TClingTests, UndeclaredIdentifierCrash) +{ + auto expectedError = R"(error: use of undeclared identifier 'i' + for(i=0; i < 0;); // the second usage of `i` was enough to get a segfault + ^ +)"; + using namespace ROOT::TestSupport; + CheckDiagsRAII diagRAII; + diagRAII.requiredDiag(kError, "cling", expectedError, false); + gInterpreter->ProcessLine("for(i=0; i < 0;); // the second usage of `i` was enough to get a segfault"); +}