Skip to content

cmd/compile: failure to reject unused dot import #22072

Closed

Description

@mdempsky

cmd/compile accepts this package, but it should be rejected with an error that import "bytes" is unused:

package p

import . "bytes"

var _ = struct{ Buffer int }{Buffer: 0}

Activity

  1. odeke-em commented on Sep 28, 2017

    @odeke-em
    Member

    @mdempsky am sure you already know but the same applies when we import pretty much any package and have the same symbol matched and used in a struct e.g

    import . "bytes"
    import . "strconv"
    import . "net/http"
    
    var _ = struct {
            Server   int
            Reader   int
            NumError string
    }{Reader: 0, NumError: "a", Server: 0}
  2. mdempsky commented on Sep 28, 2017

    @mdempsky
    ContributorAuthor

    @odeke-em Yep. The problem is that when we're noding the Buffer: 0 composite literal element, we don't have enough information to know whether it's a struct literal (which means Buffer is just a raw symbol) or an array/slice/map literal (where Buffer can be a named object, like a const or variable). Currently, we parse it as a named object, and then later take just the name if we realize it's a struct literal.

    The problem with this approach is when we access the named object that's been dot imported, we eagerly mark the import as used, even if it turns out later we didn't need the object.

  3. odeke-em commented on Dec 23, 2017

    @odeke-em
    Member

    @mdempsky aha, turns out that @griesemer had already reported this bug back in September 2013 with #6428. How should we handle the dups?

  4. griesemer commented on Dec 24, 2017

    @griesemer
    Contributor

    Duplicate of #6428.

  5. locked and limited conversation to collaborators on Dec 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions