{"record":{"id":"983c0606c20e96d7","repo":"apple/pkl","slug":"classcannotextendself","errorCode":"classCannotExtendSelf","errorMessage":"classCannotExtendSelf","messagePattern":"classCannotExtendSelf","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/ast/member/ClassNode.java","lineNumber":189,"sourceCode":"          .withSourceSection(supertypeNode.getSourceSection())\n          .build();\n    }\n    if (moduleInfo != null) {\n      if (cachedClass == superclass) {\n        throw exceptionBuilder()\n            .evalError(\"moduleCannotExtendSelf\", moduleInfo.getModuleName())\n            .withSourceSection(supertypeNode.getSourceSection())\n            .build();\n      }\n      if (superclass.isClosed()) {\n        throw exceptionBuilder()\n            .evalError(\"cannotExtendFinalModule\", superclass.getModuleName())\n            .withSourceSection(supertypeNode.getSourceSection())\n            .build();\n      }\n    } else {\n      if (cachedClass == superclass) {\n        throw exceptionBuilder()\n            .evalError(\"classCannotExtendSelf\", superclass.getDisplayName())\n            .withSourceSection(supertypeNode.getSourceSection())\n            .build();\n      }\n      if (superclass.isClosed()) {\n        throw exceptionBuilder()\n            .evalError(\"cannotExtendFinalClass\", superclass.getDisplayName())\n            .withSourceSection(supertypeNode.getSourceSection())\n            .build();\n      }\n      if (superclass.isExternal() && !classInfo.isStandardLibraryClass()) {\n        throw new VmExceptionBuilder()\n            .evalError(\"cannotExtendExternalClass\", superclass.getDisplayName())\n            .withSourceSection(supertypeNode.getSourceSection())\n            .build();\n      }\n    }\n  }","sourceCodeStart":171,"sourceCodeEnd":207,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/member/ClassNode.java#L171-L207","documentation":"Pkl throws classCannotExtendSelf when a class's `extends` clause resolves to the class being defined. ClassNode.checkSupertype (non-module branch) compares the cached class with the resolved superclass and rejects direct self-inheritance. Self-extension would be a trivially infinite inheritance loop, so it fails immediately at class definition.","triggerScenarios":"Writing `class Foo extends Foo`; a template/render step substituting the class's own name into its supertype slot; a generated Pkl file where generator output names the same class on both sides.","commonSituations":"Code-generation templates with a wrong placeholder; copy-pasting a class declaration and updating the name but not the extends clause; macro-like templating in build tooling resolving to the same identifier.","solutions":["Remove or correct the `extends` clause so it names a different superclass.","Extend the intended base class instead of the class itself.","Fix the code generator/templating variable that emits the class's own name as its supertype.","If no inheritance is needed, delete the extends clause entirely."],"exampleFix":"// before\nclass Server extends Server {}\n\n// after\nclass Server extends BaseServer {}","handlingStrategy":"validation","validationCode":"// Ensure class name and extends name differ; lint or review generated Pkl:\n// bad: class Server extends Server {}\n// pkl eval classes.pkl fails immediately with classCannotExtendSelf.","typeGuard":null,"tryCatchPattern":null,"preventionTips":["After renaming a class, re-check its extends clause.","In code generators, use distinct template variables for class name vs superclass name.","Code-review generated Pkl output before committing.","Run pkl eval in CI to catch self-extension at definition time."],"tags":["pkl","inheritance","self-reference","class"],"backgroundTag":"invalid-state-transition","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}