From dd32414bd7332c55dc37ea2972ffcca73328d834 Mon Sep 17 00:00:00 2001 From: Yong He Date: Thu, 21 Mar 2024 15:45:27 -0700 Subject: Diagnose cyclic references in inheritance graph. (#3811) --- source/slang/slang-check-inheritance.cpp | 14 ++++++++++++++ source/slang/slang-diagnostic-defs.h | 2 ++ 2 files changed, 16 insertions(+) (limited to 'source') diff --git a/source/slang/slang-check-inheritance.cpp b/source/slang/slang-check-inheritance.cpp index 5fff47cf1..18624424c 100644 --- a/source/slang/slang-check-inheritance.cpp +++ b/source/slang/slang-check-inheritance.cpp @@ -569,6 +569,18 @@ namespace Slang // later. } + // If we still cannot find a facet, then there is a true cycle in + // the inheritance graph, which is an error in the user code. + if (!foundFacet.getImpl()) + { + if (!bases.isEmpty()) + { + auto baseDecl = (*bases.begin())->facetImpl.origin.declRef.getDecl(); + getSink()->diagnose(baseDecl, Diagnostics::cyclicReferenceInInheritance, baseDecl); + } + return; + } + // At this point we definitely have a facet we'd like to // add to the output, whether it was found via the true // C3 approach, or our relaxed rule above. @@ -793,6 +805,8 @@ namespace Slang { for (auto base : *this) { + if (base->facets.isEmpty()) + continue; if (base->facets.getTail().containsMatchFor(facet)) return true; } diff --git a/source/slang/slang-diagnostic-defs.h b/source/slang/slang-diagnostic-defs.h index 7a72daa5e..6299d6666 100644 --- a/source/slang/slang-diagnostic-defs.h +++ b/source/slang/slang-diagnostic-defs.h @@ -470,6 +470,8 @@ DIAGNOSTIC(30510, Error, loopInDiffFuncRequireUnrollOrMaxIters, "loops inside a // TODO: need to assign numbers to all these extra diagnostics... DIAGNOSTIC(39999, Fatal, cyclicReference, "cyclic reference '$0'.") +DIAGNOSTIC(39999, Error, cyclicReferenceInInheritance, "cyclic reference in inheritance graph '$0'.") + DIAGNOSTIC(39999, Error, localVariableUsedBeforeDeclared, "local variable '$0' is being used before its declaration.") DIAGNOSTIC(39999, Error, variableUsedInItsOwnDefinition, "the initial-value expression for variable '$0' depends on the value of the variable itself") DIAGNOSTIC(39901, Fatal , cannotProcessInclude, "internal compiler error: cannot process '__include' in the current semantic checking context.") -- cgit v1.2.3