-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Description
Currently if you type check this code with go/types:
var (
_ int = 0
_ = int(0)
_ *int = nil
_ = (*int)(nil)
)
both 0 expressions will have type "int", whereas the first nil expression will have type "untyped nil", and the second will have type "*int". This seems at the least inconsistent. See http://play.golang.org/p/cw8Ldz1U5D
My expectation was that the 0s would type check as "untyped int" and the nils would type check as "untyped nil". The current behavior of rewriting the type of untyped expressions seems to have two negative consequences that I've noticed trying to use go/types:
- It makes it difficult to identify useless conversion operations; i.e., expressions
T(x)wherexis already of typeT. Expressions likeint(0)will trigger as false positives. - It causes
types.TypeAndValue.IsNilto return false for thenilsubexpression in(*int)(nil), because it doesn't have the type "untyped nil" anymore.
It also seems inconsistent with conversions of already typed expressions, where they're not rewritten.
However, I notice api_test.go has a bunch of tests that seem to explicitly test for this behavior, so it seems intentional?
CC @griesemer