Index: llvm/trunk/include/llvm/TableGen/Record.h =================================================================== --- llvm/trunk/include/llvm/TableGen/Record.h +++ llvm/trunk/include/llvm/TableGen/Record.h @@ -1200,7 +1200,7 @@ Init *getBit(unsigned Bit) const override; Init *resolveReferences(Resolver &R) const override; - Init *Fold() const; + Init *Fold(Record *CurRec) const; std::string getAsString() const override { return Rec->getAsString() + "." + FieldName->getValue().str(); Index: llvm/trunk/lib/TableGen/Record.cpp =================================================================== --- llvm/trunk/lib/TableGen/Record.cpp +++ llvm/trunk/lib/TableGen/Record.cpp @@ -1684,13 +1684,19 @@ Init *FieldInit::resolveReferences(Resolver &R) const { Init *NewRec = Rec->resolveReferences(R); if (NewRec != Rec) - return FieldInit::get(NewRec, FieldName)->Fold(); + return FieldInit::get(NewRec, FieldName)->Fold(R.getCurrentRecord()); return const_cast(this); } -Init *FieldInit::Fold() const { +Init *FieldInit::Fold(Record *CurRec) const { if (DefInit *DI = dyn_cast(Rec)) { - Init *FieldVal = DI->getDef()->getValue(FieldName)->getValue(); + Record *Def = DI->getDef(); + if (Def == CurRec) + PrintFatalError(CurRec->getLoc(), + Twine("Attempting to access field '") + + FieldName->getAsUnquotedString() + "' of '" + + Rec->getAsString() + "' is a forbidden self-reference"); + Init *FieldVal = Def->getValue(FieldName)->getValue(); if (FieldVal->isComplete()) return FieldVal; } Index: llvm/trunk/lib/TableGen/TGParser.cpp =================================================================== --- llvm/trunk/lib/TableGen/TGParser.cpp +++ llvm/trunk/lib/TableGen/TGParser.cpp @@ -1943,7 +1943,7 @@ Result->getAsString() + "'"); return nullptr; } - Result = FieldInit::get(Result, FieldName)->Fold(); + Result = FieldInit::get(Result, FieldName)->Fold(CurRec); Lex.Lex(); // eat field name break; } Index: llvm/trunk/test/TableGen/self-reference-recursion.td =================================================================== --- llvm/trunk/test/TableGen/self-reference-recursion.td +++ llvm/trunk/test/TableGen/self-reference-recursion.td @@ -0,0 +1,9 @@ +// RUN: not llvm-tblgen %s 2>&1 | FileCheck %s +// XFAIL: vg_leak + +class A { + int x = !cast(self).x; +} + +// CHECK: error: Attempting to access field 'x' of 'A0' is a forbidden self-reference +def A0 : A<"A0">;