A type alias that contains itself is an error, not a stack overflow - #411
Merged
Merged
Conversation
`type Node = [value: s32, next: Reference<Node>]`, `{ value: number,
next: Node }`, mutual aliases, a generic `List<T>` inside itself: every
mention of an alias expanded its declaration again, and one that names
itself did so until the stack ran out - exit 0xC00000FD, no message.
An alias being expanded is now remembered (expandTypeAlias, used by the
plain and both generic resolutions), and one that meets itself is
reported: "type alias 'Node' circularly references itself; use an
interface or a class for a recursive type". A type here has a fixed shape
- a tuple holds its fields in place - so none can contain itself, not
even behind Reference<T>, whose tuple type would have to name itself; an
interface or a class is the recursive type.
The errors that only follow from it are left out: those raised while the
expansion unwinds (a scoped handler swallows them until the outermost
expansion of that alias ends), and "can't find type" / "can't resolve
name" / "generic type ... can't be found" at later mentions of it. An
exported alias is registered before it is resolved, so it is found - and
found circular - the same way.
Tests: nine shapes in type-alias-circular/ (tuple through Reference,
object, optional field, declared function parameter, mutual, exported,
generic, union through an array, in a namespace), each passing only on
the message and failing on the follow-on ones; and positive.ts, aliases
naming other aliases and themselves one after another, JIT and AOT.
Fixes #370.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…helper #407 included MLIRRTTIHelperVC.h after MLIRGenImpl.h, and the helper undefines DEBUG_TYPE on the way out, so every LLVM_DEBUG in the file was an error in a Debug build (Release compiles LLVM_DEBUG away, so CI never saw it). MLIRGenStatements.cpp does the same after the same include. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #370: a type alias that refers to itself crashed the compiler with a stack overflow (0xC00000FD, no message). It's now a compile error.
Cause
Every mention of a type alias expanded its declaration again, and nothing noticed when the same alias was already being expanded higher up the stack.
Fix
expandTypeAlias). This covers the plain resolution and both generic ones:resolveTypeByNameInNamespace,getTypeByTypeReferenceandresolveGenericTypeInNamespace. An alias that meets itself is reported:Reference<T>: its tuple type would have to name itself, and the dialect's tuples are structural. A real recursive alias would need a tuple type that can refer to itself, which is a separate design. An interface or a class is the recursive type today. tsbindgen's rule of mapping self-pointers toOpaquetherefore stays.Tests
type-alias-circular/has nine shapes, each passing only on the message and failing on the follow-on messages or a stack dump:Reference;List<T>;positive.ts: aliases that name other aliases, and nested generic ones (Pair<Pair<number>>,Box<Box<T>>), run JIT and AOT.Also: debug build fix (separate commit)
MLIRGenCast.cppdidn't compile in a Debug build after #407. TheMLIRRTTIHelperVC.hinclude undefinesDEBUG_TYPE, and the file usesLLVM_DEBUG. It now definesDEBUG_TYPEagain after that include, asMLIRGenStatements.cppalready does.🤖 Generated with Claude Code