-
Notifications
You must be signed in to change notification settings - Fork 17.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
go/types: missing Info.Types for *ast.FuncType #70908
Comments
Related Issues (Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.) |
Hi @adonovan, I’d like to work on this if @madelinekalil approve the issue. I’ve recently added a PR on a mini parser for go.mod, and the Go codebase looks clean and approachable. I am particularly interested in the parser, AST, and type-checking stages, and I want to contribute to these areas as they seem beginner-friendly and a great way to start learning a language's type system through hands-on experience. patch: diff --git a/src/go/types/signature.go b/src/go/types/signature.go
index 681eb85fd7..590e0a982b 100644
--- a/src/go/types/signature.go
+++ b/src/go/types/signature.go
@@ -106,12 +106,18 @@ func (s *Signature) String() string { return TypeString(s, nil) }
// funcType type-checks a function or method type.
func (check *Checker) funcType(sig *Signature, recvPar *ast.FieldList, ftyp *ast.FuncType) {
check.openScope(ftyp, "function")
check.scope.isFunc = true
check.recordScope(ftyp, check.scope)
sig.scope = check.scope
defer check.closeScope()
+ if check.Info.Types != nil {
+ check.recordTypeAndValue(ftyp, typexpr, sig, nil)
+ }
+
// collect method receiver, if any
var recv *Var
var rparams *TypeParamList |
Thanks for your interest! @madelinekalil discovered the bug, but I think @griesemer is the best person to decide whether the simple fix you suggest is the right one. I know in the past there have been several discussions about when exactly the type checker guarantees to record a type for an expression, and it's possible this one lies in a grey area since, in context, the FuncType it isn't strictly a term or a type. Perhaps the most significant part of the fix will be to clarify the documentation on precisely what the user can safely assume.
I have never heard go/types described that way before! ;-) |
no problem. I can understand, I will keep looking at the issues and PRs and learning.
No no, I mean I am interested, and at least I can understand issues associated with these layers. |
I looked into this in fair detail: For this program: package p
var x int
var y = 1.0
func f() (int, string) {
return 0, ""
}
var g func() we get the following type recordings (playground):
The type for Presumably, the argument is that since we record the type for the type expression Yet, we don't do it for function declarations, because there's no obvious syntactic function type expression present in the source (e.g. we don't write Note that we definitely record the function type in I am not convinced we should make the suggested change. For one it has been working like this for more than a decade and nobody complained. Second, I am concerned that recording this information may cause unexpected bugs (due to tools not expecting the data). Finally, for generic functions, As an aside, the suggested fix doesn't look right; that type information is recorded externally, and doing it in All in all, I believe we should leave things as they are. |
Yeah, I agree it's a grey area. I think a reasonable resolution would be to document more explicitly at Info.Types what the client can expect. |
Change https://go.dev/cl/640776 mentions this issue: |
It is probably simplest not to change this behavior, since this has been the case for a long time. Improving documentation sounds like a reasonable solution. |
Change https://go.dev/cl/640596 mentions this issue: |
Function types for function (and method) declarations do not appear in Info.Types maps, only Info.Defs maps, because the function type is implicit in the declaration and not a proper (function) type expression. This is true even though the AST represents these types via an (artificial) FuncType node. Document this explicitly in the API. No functional code changes. Fixes golang#70908. Change-Id: I2aa897daed04e7ad0fa8b625d9adc7b423c57387 Reviewed-on: https://go-review.googlesource.com/c/go/+/640776 Reviewed-by: Alan Donovan <[email protected]> Auto-Submit: Robert Griesemer <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
Methods declared in an interface have a signature and FuncType in the AST, but they do not express a syntactic function type expression. Treat them like ordinary function/method declarations and do not record them in the Info.Types map. This removes an inconsistency in the way function types are recorded. Follow-up on CL 640776. For #70908. Change-Id: I60848f209b40b008039c014fb8b7b279361487b9 Reviewed-on: https://go-review.googlesource.com/c/go/+/640596 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Auto-Submit: Robert Griesemer <[email protected]> Reviewed-by: Alan Donovan <[email protected]>
In this program, types.Info.Types records no type for f's ast.FuncType node. I think it should:
@madelinekalil
The text was updated successfully, but these errors were encountered: