Skip to content

Commit

Permalink
fix: fix crash extracting struct with type parameter (#1521)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
matt2e authored May 17, 2024
1 parent 384c2e7 commit dce2ea4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
6 changes: 5 additions & 1 deletion go-runtime/compile/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
4 changes: 4 additions & 0 deletions go-runtime/compile/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ func TestExtractModuleSchema(t *testing.T) {
field String
}
data DataWithType<T> {
value T
}
export data ExportedData {
field String
}
Expand Down
5 changes: 5 additions & 0 deletions go-runtime/compile/testdata/one/one.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit dce2ea4

Please sign in to comment.