Repository navigation
fix(compiler): await interface calls in a literal passed to ServeMux.HandleFunc - #216
Merged
Merged
Conversation
…HandleFunc A function literal passed to an override call lowered with async calls disallowed unless the callee itself was async. ServeMux.HandleFunc is synchronous and takes an unnamed func type, so the async metadata of HandlerFunc.ServeHTTP never reached its handler literal. Interface calls such as w.Header() and w.Write stayed unawaited, failing the type check, or were awaited only when another file in the program made WriteHeader async. Override metadata gains asyncCallbacks, which maps a function name or Type.Method key to the parameters the runtime stores and awaits. The net/http override names the handler of HandleFunc and ServeMux.HandleFunc. lowerFixedCallArgs allows async calls in a literal passed for such a parameter, so every interface call in it awaits. The net_http_servemux_handlefunc fixture registers handlers that call w.Header().Set, w.WriteHeader and w.Write and serves them through httptest.NewRecorder. Signed-off-by: Christian Stewart <[email protected]>
| mux.HandleFunc("/mux", func(w http.ResponseWriter, r *http.Request) { | ||
| w.Header().Set("X-Route", "mux") | ||
| w.WriteHeader(http.StatusAccepted) | ||
| if _, err := w.Write([]byte("hello " + r.URL.Path)); err != nil { |
| }) | ||
| http.HandleFunc("/default", func(w http.ResponseWriter, r *http.Request) { | ||
| w.Header().Set("X-Route", "default") | ||
| if _, err := w.Write([]byte("hello " + r.URL.Path)); err != nil { |
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The metadata contract, lowering behavior, runtime implementation, documentation, and regression coverage are consistent.
0 open findings
What changed in this PR
Adds callback-specific async metadata so net/http handler literals correctly await interface calls.
Changes:
- Adds
asyncCallbacksmetadata parsing, cloning, lookup, and documentation. - Applies callback metadata during fixed argument lowering.
- Adds compliance coverage for both
HandleFuncforms.
| File | Description |
|---|---|
compiler/lowering.go |
Enables async lowering for designated callback literals. |
compiler/override-facts.go |
Loads, queries, and clones callback metadata. |
compiler/override-registry.go |
Defines callback metadata. |
compiler/override-registry_test.go |
Tests lookup and immutability. |
gs/net/http/meta.json |
Marks both handler parameters as async-capable. |
design/OVERRIDES.md |
Documents asyncCallbacks. |
tests/tests/net_http_servemux_handlefunc/net_http_servemux_handlefunc.go |
Adds compliance fixture source. |
tests/tests/net_http_servemux_handlefunc/net_http_servemux_handlefunc.gs.ts |
Checks generated async handlers. |
tests/tests/net_http_servemux_handlefunc/expected.log |
Defines expected runtime output. |
tests/tests/net_http_servemux_handlefunc/tsconfig.json |
Configures fixture type-checking. |
tests/tests/net_http_servemux_handlefunc/index.ts |
Adds the fixture entry module. |
🧠 Review effort: Balanced
Give feedback about Copilot approvals in this survey to enter a drawing for a $150 gift card.
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.
A handler literal passed to ServeMux.HandleFunc or http.HandleFunc lowered with async calls disallowed, because HandleFunc is synchronous and takes an unnamed func type. Interface calls such as w.Header() and w.Write stayed unawaited and failed the type check, or were awaited only when another file in the program made WriteHeader async. Override metadata gains an optional asyncCallbacks key naming the callback parameters the runtime stores and awaits; the net/http override names the handler of both HandleFunc forms, and lowerFixedCallArgs allows async calls in such a literal. The new net_http_servemux_handlefunc fixture registers handlers that call w.Header().Set, w.WriteHeader and w.Write and serves them through httptest.NewRecorder.