Skip to content

cmd/vet: detect impossible interface-interface type assertions #4483

Closed

Description

@griesemer
For:

$ cat test.go
package main

import "io"

func main() {
    var y interface {
        Read()
    }
    _ = y.(io.Reader)
}

Neither compiler complains:

$ gccgo test.go
$ go tool 6g test.go

And according to the spec it is a valid program.

However, any concrete value assigned to y must implement Read(), and thus cannot
possibly implement io.Reader.Read. Thus, this type assertion will fail and we know this
at compile time.

The spec should require this check and compilers should implement it. This would be a
backward-incompatible language change, albeit with a very small chance of actually
breaking existing code.

Activity

  1. rsc commented on Dec 6, 2012

    @rsc
    Contributor

    Comment 1:

    For what it's worth, I don't believe this is something we can change
    during Go 1.
    Russ
  2. rsc commented on Jul 30, 2013

    @rsc
    Contributor

    Comment 2:

    Labels changed: added go2.

  3. rsc commented on Dec 4, 2013

    @rsc
    Contributor

    Comment 3:

    Labels changed: added repo-main.

  4. griesemer commented on Feb 27, 2014

    @griesemer
    ContributorAuthor

    Comment 4:

    Status changed to LongTerm.

  5. griesemer commented on Aug 25, 2014

    @griesemer
    ContributorAuthor

    Comment 5:

    Labels changed: added release-none.

  6. griesemer commented on Aug 28, 2014

    @griesemer
    ContributorAuthor

    Comment 6:

    Issue #8610 has been merged into this issue.

  7. griesemer commented on Aug 28, 2014

    @griesemer
    ContributorAuthor

    Comment 7:

    From issue #8610, a point to consider:
    When it comes to interface assignments ("implements" relation), conflicting method
    signatures are not permitted.
  8. self-assigned this
    on Aug 28, 2014
  9. added this to the Unplanned milestone on Apr 10, 2015
  10. 31 remaining items

  11. toothrot commented on May 14, 2020

    @toothrot
    Contributor

    @smasher164 @ianlancetaylor @griesemer: Does this issue need to be updated now that https://golang.org/cl/232660 is merged? It's labeled as blocking the Go 1.15 beta.

  12. smasher164 commented on May 14, 2020

    @smasher164
    Member

    @toothrot Since CL 232660 has been merged, this issue is resolved, and no longer a release blocker.

  13. ianlancetaylor commented on May 14, 2020

    @ianlancetaylor
    Contributor

    I'm planning to review this and #32479 to make sure they are done and to write release notes.

  14. added
    okay-after-beta1Used by release team to mark a release-blocker issue as okay to resolve either before or after beta1
    on May 14, 2020
  15. gopherbot commented on May 18, 2020

    @gopherbot
    Contributor

    Change https://golang.org/cl/234518 mentions this issue: doc/go1.15: mention vet warning for impossible type assertions

  16. locked and limited conversation to collaborators on May 18, 2021
  17. moved this to Accepted in Proposalson Aug 10, 2022
  18. removed this from Proposalson Oct 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions