Mirror of https://github.com/cue-lang/cue cuelang.org
cue go

internal/core/convert: fix astFromGoType for recursive types and shared AST pointers master

Replace the store-then-mutate pattern in astFromGoType with a typeBuilder that tracks named Go types and generates unique CUE identifiers for them. Named types that are recursive or referenced multiple times get a hidden field definition in a wrapper struct; non-recursive single-use types are inlined directly as before. Also, rather than store a `reflect.Type` to `adt.Expr` mapping, we store the finalized `*adt.Vertex` mapping which means we don't have to mutate the value by invoking `Finalize` on it in a concurrent situation. This fixes three interrelated bugs: - Cyclic AST trees caused by pre-storing a partially-built struct in astTypeCache then mutating it, which made astutil.Resolve loop forever. - A race condition where concurrent goroutines could read the partially-built struct from the cache. - Shared AST pointers when the same type appeared at multiple sites, rather than explicit references. Signed-off-by: Roger Peppe <rogpeppe@gmail.com> Change-Id: I332c0ce58b45decde3eee998a1a8cda820212c95 Reviewed-on: https://cue.gerrithub.io/c/cue-lang/cue/+/1233620 TryBot-Result: CUEcueckoo <cueckoo@cuelang.org> Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com> Reviewed-by: Marcel van Lohuizen <mpvl@gmail.com>


+415 -185
8 changed files