From dce2ea40236ea4e74ad1483a22dbee681624c541 Mon Sep 17 00:00:00 2001 From: Matt Toohey Date: Fri, 17 May 2024 15:55:03 +1000 Subject: [PATCH] fix: fix crash extracting struct with type parameter (#1521) FTL panics when compiling code like this: ``` //ftl:data type DataWithType[T any] struct { Value T } ``` `VisitStruct` was iterating through type parameters and type parameter values, but there are no values in this case. --- go-runtime/compile/schema.go | 6 +++++- go-runtime/compile/schema_test.go | 4 ++++ go-runtime/compile/testdata/one/one.go | 5 +++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/go-runtime/compile/schema.go b/go-runtime/compile/schema.go index 2ee51240c7..bffddb739d 100644 --- a/go-runtime/compile/schema.go +++ b/go-runtime/compile/schema.go @@ -1109,7 +1109,11 @@ func visitStruct(pctx *parseContext, pos token.Pos, tnode types.Type, isExported Pos: goPosToSchemaPos(pos), Name: param.Obj().Name(), }) - if typeArg, ok := visitType(pctx, pos, named.TypeArgs().At(i), isExported).Get(); ok { + typeArgs := named.TypeArgs() + if typeArgs == nil { + continue + } + if typeArg, ok := visitType(pctx, pos, typeArgs.At(i), isExported).Get(); ok { dataRef.TypeParameters = append(dataRef.TypeParameters, typeArg) } } diff --git a/go-runtime/compile/schema_test.go b/go-runtime/compile/schema_test.go index 4cb3393072..a13ba66f6e 100644 --- a/go-runtime/compile/schema_test.go +++ b/go-runtime/compile/schema_test.go @@ -96,6 +96,10 @@ func TestExtractModuleSchema(t *testing.T) { field String } + data DataWithType { + value T + } + export data ExportedData { field String } diff --git a/go-runtime/compile/testdata/one/one.go b/go-runtime/compile/testdata/one/one.go index d782ac2fcc..6f7140a4fa 100644 --- a/go-runtime/compile/testdata/one/one.go +++ b/go-runtime/compile/testdata/one/one.go @@ -145,3 +145,8 @@ func Nothing(ctx context.Context) error { func Http(ctx context.Context, req builtin.HttpRequest[Req]) (builtin.HttpResponse[Resp, ftl.Unit], error) { return builtin.HttpResponse[Resp, ftl.Unit]{}, nil } + +//ftl:data +type DataWithType[T any] struct { + Value T +}