diff --git a/compiler/compile.go b/compiler/compile.go index ff82bb7..89e27cd 100644 --- a/compiler/compile.go +++ b/compiler/compile.go @@ -276,7 +276,6 @@ func (c *compiler) compilePackage(p *packages.Package, colors functionColors) er } } - clearPos(gen) generateFunctypes(p, gen, colorsByFunc) // Find all the required imports for this file. diff --git a/compiler/pos.go b/compiler/pos.go deleted file mode 100644 index f6bc85a..0000000 --- a/compiler/pos.go +++ /dev/null @@ -1,135 +0,0 @@ -package compiler - -import ( - "go/ast" - "go/token" -) - -// clearPos resets the token.Pos field(s) in each type of ast.Node. -// When AST nodes are generated alongside nodes that have position -// information, it can cause formatting to produce invalid results. -// This function clears the information from all nodes so that the -// formatter produces correct results. -func clearPos(tree ast.Node) { - ast.Inspect(tree, func(node ast.Node) bool { - switch n := node.(type) { - case *ast.ArrayType: - n.Lbrack = token.NoPos - case *ast.AssignStmt: - n.TokPos = token.NoPos - case *ast.BasicLit: - n.ValuePos = token.NoPos - case *ast.BinaryExpr: - n.OpPos = token.NoPos - case *ast.BlockStmt: - n.Rbrace = token.NoPos - n.Lbrace = token.NoPos - case *ast.BranchStmt: - n.TokPos = token.NoPos - case *ast.CallExpr: - n.Ellipsis = token.NoPos - n.Lparen = token.NoPos - n.Rparen = token.NoPos - case *ast.CaseClause: - n.Colon = token.NoPos - n.Case = token.NoPos - case *ast.ChanType: - n.Arrow = token.NoPos - n.Begin = token.NoPos - case *ast.CommClause: - n.Case = token.NoPos - n.Colon = token.NoPos - case *ast.Comment: - n.Slash = token.NoPos - case *ast.CommentGroup: - case *ast.CompositeLit: - n.Lbrace = token.NoPos - n.Rbrace = token.NoPos - case *ast.DeclStmt: - case *ast.DeferStmt: - n.Defer = token.NoPos - case *ast.Ellipsis: - n.Ellipsis = token.NoPos - case *ast.EmptyStmt: - n.Semicolon = token.NoPos - case *ast.ExprStmt: - case *ast.Field: - case *ast.FieldList: - n.Closing = token.NoPos - n.Opening = token.NoPos - case *ast.File: - n.Package = token.NoPos - n.FileStart = token.NoPos - n.FileEnd = token.NoPos - case *ast.ForStmt: - n.For = token.NoPos - case *ast.FuncDecl: - case *ast.FuncLit: - case *ast.FuncType: - n.Func = token.NoPos - case *ast.GenDecl: - n.Lparen = token.NoPos - n.Rparen = token.NoPos - n.TokPos = token.NoPos - case *ast.GoStmt: - n.Go = token.NoPos - case *ast.Ident: - n.NamePos = token.NoPos - case *ast.IfStmt: - n.If = token.NoPos - case *ast.ImportSpec: - n.EndPos = token.NoPos - case *ast.IncDecStmt: - n.TokPos = token.NoPos - case *ast.IndexExpr: - n.Lbrack = token.NoPos - n.Rbrack = token.NoPos - case *ast.IndexListExpr: - n.Lbrack = token.NoPos - n.Rbrack = token.NoPos - case *ast.InterfaceType: - n.Interface = token.NoPos - case *ast.KeyValueExpr: - n.Colon = token.NoPos - case *ast.LabeledStmt: - n.Colon = token.NoPos - case *ast.MapType: - n.Map = token.NoPos - case *ast.Package: - case *ast.ParenExpr: - n.Lparen = token.NoPos - n.Rparen = token.NoPos - case *ast.RangeStmt: - n.TokPos = token.NoPos - n.For = token.NoPos - n.Range = token.NoPos - case *ast.ReturnStmt: - n.Return = token.NoPos - case *ast.SelectStmt: - n.Select = token.NoPos - case *ast.SelectorExpr: - case *ast.SendStmt: - n.Arrow = token.NoPos - case *ast.SliceExpr: - n.Lbrack = token.NoPos - n.Rbrack = token.NoPos - case *ast.StarExpr: - n.Star = token.NoPos - case *ast.StructType: - n.Struct = token.NoPos - case *ast.SwitchStmt: - n.Switch = token.NoPos - case *ast.TypeAssertExpr: - n.Lparen = token.NoPos - n.Rparen = token.NoPos - case *ast.TypeSpec: - n.Assign = token.NoPos - case *ast.TypeSwitchStmt: - n.Switch = token.NoPos - case *ast.UnaryExpr: - n.OpPos = token.NoPos - case *ast.ValueSpec: - } - return true - }) -} diff --git a/compiler/testdata/coroutine_generated.go b/compiler/testdata/coroutine_generated.go index 5526102..d42fa91 100644 --- a/compiler/testdata/coroutine_generated.go +++ b/compiler/testdata/coroutine_generated.go @@ -13,9 +13,7 @@ func SomeFunctionThatShouldExistInTheCompiledFile() { } //go:noinline -func Identity(n int) { - coroutine.Yield[int, any](n) -} +func Identity(n int) { coroutine.Yield[int, any](n) } //go:noinline func SquareGenerator(n int) { @@ -280,10 +278,12 @@ func FizzBuzzIfGenerator(n int) { fallthrough case _f.IP < 7: for ; _f0.X1 <= _f0.X0; _f0.X1, _f.IP = _f0.X1+1, 2 { - if _f0.X1%3 == 0 && _f0.X1%5 == 0 { + if _f0.X1% + 3 == 0 && _f0.X1%5 == 0 { coroutine.Yield[int, any](FizzBuzz) } else { - if _f0.X1%3 == 0 { + if _f0.X1% + 3 == 0 { coroutine.Yield[int, any](Fizz) } else { switch { @@ -295,6 +295,7 @@ func FizzBuzzIfGenerator(n int) { if _f0.X2 == 0 { coroutine.Yield[int, any](Buzz) } else { + coroutine.Yield[int, any](_f0.X1) } } @@ -351,7 +352,8 @@ func FizzBuzzSwitchGenerator(n int) { default: switch { case _f.IP < 3: - _f0.X2 = _f0.X1%3 == 0 && _f0.X1%5 == 0 + _f0.X2 = _f0.X1% + 3 == 0 && _f0.X1%5 == 0 _f.IP = 3 fallthrough case _f.IP < 9: @@ -360,7 +362,8 @@ func FizzBuzzSwitchGenerator(n int) { } else { switch { case _f.IP < 5: - _f0.X3 = _f0.X1%3 == 0 + _f0.X3 = _f0.X1% + 3 == 0 _f.IP = 5 fallthrough case _f.IP < 9: @@ -369,13 +372,15 @@ func FizzBuzzSwitchGenerator(n int) { } else { switch { case _f.IP < 7: - _f0.X4 = _f0.X1%5 == 0 + _f0.X4 = _f0.X1% + 5 == 0 _f.IP = 7 fallthrough case _f.IP < 9: if _f0.X4 { coroutine.Yield[int, any](Buzz) } else { + coroutine.Yield[int, any](_f0.X1) } } @@ -392,13 +397,20 @@ func FizzBuzzSwitchGenerator(n int) { func Shadowing(_ int) { _c := coroutine.LoadContext[int, any]() _f, _fp := _c.Push() + const _o0 = 11 + const _o1 = 12 + type _o2 uint16 + type _o3 uint32 + const _o4 = 1 type _o5 [_o4]uint8 + type _o6 [_o4]uint8 + const _o7 = unsafe.Sizeof(_o6{}) * 2 type _o8 [_o7]uint8 var _f0 *struct { @@ -510,6 +522,7 @@ func Shadowing(_ int) { _f.IP = 5 fallthrough case _f.IP < 6: + coroutine.Yield[int, any](_f0.X0) _f.IP = 6 fallthrough @@ -527,6 +540,7 @@ func Shadowing(_ int) { _f.IP = 8 fallthrough case _f.IP < 9: + coroutine.Yield[int, any](_f0.X0) _f.IP = 9 fallthrough @@ -545,7 +559,8 @@ func Shadowing(_ int) { default: switch { case _f.IP < 12: - _f0.X5 = _f0.X4 == 1 + _f0.X5 = _f0.X4 == + 1 _f.IP = 12 fallthrough case _f.IP < 16: @@ -564,12 +579,14 @@ func Shadowing(_ int) { case _f.IP < 15: switch { default: + coroutine.Yield[int, any](_f0.X6) } } _f.IP = 15 fallthrough case _f.IP < 16: + coroutine.Yield[int, any](_f0.X3) } } @@ -579,6 +596,7 @@ func Shadowing(_ int) { _f.IP = 16 fallthrough case _f.IP < 17: + coroutine.Yield[int, any](_f0.X0) _f.IP = 17 fallthrough @@ -600,11 +618,13 @@ func Shadowing(_ int) { _f.IP = 20 fallthrough case _f.IP < 21: + coroutine.Yield[int, any](_f0.X8) } _f.IP = 21 fallthrough case _f.IP < 22: + coroutine.Yield[int, any](_f0.X0) _f.IP = 22 fallthrough @@ -624,6 +644,7 @@ func Shadowing(_ int) { _f.IP = 25 fallthrough case _f.IP < 26: + coroutine.Yield[int, any](_f0.X10) _f.IP = 26 fallthrough @@ -641,11 +662,13 @@ func Shadowing(_ int) { _f.IP = 28 fallthrough case _f.IP < 29: + coroutine.Yield[int, any](_o1) } _f.IP = 29 fallthrough case _f.IP < 30: + coroutine.Yield[int, any](_o0) _f.IP = 30 fallthrough @@ -972,12 +995,14 @@ func LoopBreakAndContinue(_ int) { _f.IP = 4 fallthrough case _f.IP < 5: - if _f0.X0 > 5 { + if _f0.X0 > + 5 { break _l0 } _f.IP = 5 fallthrough case _f.IP < 6: + coroutine.Yield[int, any](_f0.X0) } } @@ -1012,22 +1037,30 @@ func LoopBreakAndContinue(_ int) { switch { default: { - _f0.X5 = _f0.X4 == 0 + _f0.X5 = _f0.X4 == + + 0 if _f0.X5 { continue _l2 } else { - _f0.X6 = _f0.X4 == 1 + _f0.X6 = _f0.X4 == + + 1 if _f0.X6 { { _f0.X7 = _f0.X2 switch { default: { - _f0.X8 = _f0.X7 == 0 + _f0.X8 = _f0.X7 == + + 0 if _f0.X8 { continue _l1 } else { - _f0.X9 = _f0.X7 == 1 + _f0.X9 = _f0.X7 == + + 1 if _f0.X9 { break _l1 } @@ -1191,6 +1224,7 @@ func RangeOverMaps(n int) { fallthrough case _f.IP < 9: for ; _f0.X3 < len(_f0.X2); _f0.X3, _f.IP = _f0.X3+1, 8 { + coroutine.Yield[int, any](0) } } @@ -1239,6 +1273,7 @@ func RangeOverMaps(n int) { fallthrough case _f.IP < 17: if _f0.X9 { + coroutine.Yield[int, any](_f0.X8) } } @@ -1293,6 +1328,7 @@ func RangeOverMaps(n int) { if _f0.X16 { switch { case _f.IP < 25: + coroutine.Yield[int, any](_f0.X14) _f.IP = 25 fallthrough @@ -1309,14 +1345,12 @@ func RangeOverMaps(n int) { _f.IP = 26 fallthrough case _f.IP < 27: - _f0.X17 = make(map[int]struct { - }, _f0.X0) + _f0.X17 = make(map[int]struct{}, _f0.X0) _f.IP = 27 fallthrough case _f.IP < 28: for _f0.X18 = 0; _f0.X18 < _f0.X0; _f0.X18++ { - _f0.X17[_f0.X18] = struct { - }{} + _f0.X17[_f0.X18] = struct{}{} } _f.IP = 28 fallthrough @@ -1368,6 +1402,7 @@ func RangeOverMaps(n int) { if _f0.X24 { switch { case _f.IP < 37: + delete(_f0.X17, _f0.X23) _f.IP = 37 fallthrough @@ -1427,15 +1462,11 @@ func Range(n int, do func(int)) { } //go:noinline -func Double(n int) { - coroutine.Yield[int, any](2 * n) -} +func Double(n int) { coroutine.Yield[int, any](2 * n) } //go:noinline func RangeTriple(n int) { - Range(n, func(i int) { - coroutine.Yield[int, any](3 * i) - }) + Range(n, func(i int) { coroutine.Yield[int, any](3 * i) }) } //go:noinline @@ -1467,12 +1498,11 @@ func RangeTripleFuncValue(n int) { }() switch { case _f.IP < 2: - _f0.X1 = func(i int) { - coroutine.Yield[int, any](3 * i) - } + _f0.X1 = func(i int) { coroutine.Yield[int, any](3 * i) } _f.IP = 2 fallthrough case _f.IP < 3: + Range(_f0.X0, _f0.X1) } } @@ -1513,9 +1543,7 @@ func RangeReverseClosureCaptureByValue(n int) { _f.IP = 2 fallthrough case _f.IP < 3: - _f0.X2 = func() { - coroutine.Yield[int, any](_f0.X0 - (_f0.X1 + 1)) - } + _f0.X2 = func() { coroutine.Yield[int, any](_f0.X0 - (_f0.X1 + 1)) } _f.IP = 3 fallthrough case _f.IP < 5: @@ -1617,6 +1645,7 @@ func Range10ClosureCapturingValues() { _f.IP = 4 fallthrough case _f.IP < 5: + return false } return @@ -1739,6 +1768,7 @@ func Range10ClosureCapturingPointers() { _f.IP = 4 fallthrough case _f.IP < 5: + return false } return @@ -1841,9 +1871,7 @@ func Range10ClosureHeterogenousCapture() { _f1.X6 = 6 _f1.X7 = 7 _f1.X8 = 8 - _f1.X9 = func() int { - return int(_f1.X8) + 1 - } + _f1.X9 = func() int { return int(_f1.X8) + 1 } } _f.IP = 11 fallthrough @@ -1921,25 +1949,36 @@ func Range10ClosureHeterogenousCapture() { case _f.IP < 13: switch { default: - if _f0.X2 = _f0.X1 == 0; _f0.X2 { + if _f0.X2 = _f0.X1 == + + 0; _f0.X2 { _f0.X0 = int(_f1.X0) - } else if _f0.X3 = _f0.X1 == 1; _f0.X3 { + } else if _f0.X3 = _f0.X1 == + 1; _f0.X3 { _f0.X0 = int(_f1.X1) - } else if _f0.X4 = _f0.X1 == 2; _f0.X4 { + } else if _f0.X4 = _f0.X1 == + 2; _f0.X4 { _f0.X0 = int(_f1.X2) - } else if _f0.X5 = _f0.X1 == 3; _f0.X5 { + } else if _f0.X5 = _f0.X1 == + 3; _f0.X5 { _f0.X0 = int(_f1.X3) - } else if _f0.X6 = _f0.X1 == 4; _f0.X6 { + } else if _f0.X6 = _f0.X1 == + 4; _f0.X6 { _f0.X0 = int(_f1.X4) - } else if _f0.X7 = _f0.X1 == 5; _f0.X7 { + } else if _f0.X7 = _f0.X1 == + 5; _f0.X7 { _f0.X0 = int(_f1.X5) - } else if _f0.X8 = _f0.X1 == 6; _f0.X8 { + } else if _f0.X8 = _f0.X1 == + 6; _f0.X8 { _f0.X0 = int(_f1.X6) - } else if _f0.X9 = _f0.X1 == 7; _f0.X9 { + } else if _f0.X9 = _f0.X1 == + 7; _f0.X9 { _f0.X0 = int(_f1.X7) - } else if _f0.X10 = _f0.X1 == 8; _f0.X10 { + } else if _f0.X10 = _f0.X1 == + 8; _f0.X10 { _f0.X0 = int(_f1.X8) - } else if _f0.X11 = _f0.X1 == 9; _f0.X11 { + } else if _f0.X11 = _f0.X1 == + 9; _f0.X11 { _f0.X0 = _f1.X9() } } @@ -1947,6 +1986,7 @@ func Range10ClosureHeterogenousCapture() { _f.IP = 13 fallthrough case _f.IP < 14: + coroutine.Yield[int, any](_f0.X0) _f.IP = 14 fallthrough @@ -2064,6 +2104,7 @@ func Range10Heterogenous() { _f.IP = 12 fallthrough case _f.IP < 22: + switch _f0.X9 { case 0: _f0.X10 = int(_f0.X0) @@ -2207,6 +2248,7 @@ func Select(n int) { fallthrough case _f.IP < 6: if _f0.X3 { + coroutine.Yield[int, any](-1) } } @@ -2266,15 +2308,18 @@ func Select(n int) { if _f0.X9 { switch { case _f.IP < 15: - if _f0.X4 >= 5 { + if _f0.X4 >= + 5 { break _l2 } _f.IP = 15 fallthrough case _f.IP < 16: + coroutine.Yield[int, any](_f0.X4) } } else if _f0.X10 = _f0.X8 == 2; _f0.X10 { + panic("unreachable") } } @@ -2319,12 +2364,14 @@ func Select(n int) { if _f0.X14 { switch { case _f.IP < 23: - if _f0.X4 >= 6 { + if _f0.X4 >= + 6 { break _l3 } _f.IP = 23 fallthrough case _f.IP < 24: + coroutine.Yield[int, any](_f0.X4 * 10) } } @@ -2962,7 +3009,8 @@ func YieldingDurations() { }() switch { case _f.IP < 2: - _f0.X0 = _f1.X0.Nanoseconds() + _f0.X0 = _f1.X0. + Nanoseconds() _f.IP = 2 fallthrough case _f.IP < 3: @@ -3084,9 +3132,7 @@ func RangeYieldAndDeferAssign(n int) { } } -type MethodGeneratorState struct { - i int -} +type MethodGeneratorState struct{ i int } //go:noinline func (s *MethodGeneratorState) MethodGenerator(n int) { @@ -3117,7 +3163,8 @@ func (s *MethodGeneratorState) MethodGenerator(n int) { }() switch { case _f.IP < 2: - _f0.X0.i = 0 + _f0.X0. + i = 0 _f.IP = 2 fallthrough case _f.IP < 3: diff --git a/compiler/testdata/http/main_generated.go b/compiler/testdata/http/main_generated.go index 13faaf0..a5904eb 100644 --- a/compiler/testdata/http/main_generated.go +++ b/compiler/testdata/http/main_generated.go @@ -3,15 +3,13 @@ package main import ( + fmt "fmt" http "net/http" coroutine "github.com/stealthrocket/coroutine" - coroutine "github.com/stealthrocket/coroutine" - fmt "fmt" - _types "github.com/stealthrocket/coroutine/types" ) +import _types "github.com/stealthrocket/coroutine/types" -type yieldingRoundTripper struct { -} +type yieldingRoundTripper struct{} //go:noinline func (*yieldingRoundTripper) RoundTrip(req *http.Request) (_ *http.Response, _ error) { _c := coroutine.LoadContext[*http.Request, *http.Response]() @@ -91,13 +89,18 @@ func work() { fmt.Println(_f0.X0.StatusCode) } } + func main() { http.DefaultTransport = &yieldingRoundTripper{} + c := coroutine.New[*http.Request, *http.Response](work) + for c.Next() { req := c.Recv() fmt.Println("Requesting", req.URL.String()) - c.Send(&http.Response{StatusCode: 200}) + c.Send(&http.Response{ + StatusCode: 200, + }) } } func init() {