Skip to content

go/types: inconsistent handling of untyped expressions #13061

Open
@mdempsky

Description

@mdempsky

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:

  1. It makes it difficult to identify useless conversion operations; i.e., expressions T(x) where x is already of type T. Expressions like int(0) will trigger as false positives.
  2. It causes types.TypeAndValue.IsNil to return false for the nil subexpression 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions