Skip to content
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/build: MultiplePackageError values swapped. #9409

Closed
dmitshur opened this issue Dec 21, 2014 · 6 comments
Closed

go/build: MultiplePackageError values swapped. #9409

dmitshur opened this issue Dec 21, 2014 · 6 comments
Milestone

Comments

@dmitshur
Copy link
Contributor

This issue is present in the latest commit on master branch, f34964e.

MultiplePackageError is defined in src/go/build/build.go#L420-L426 with Dir, then Packages, and finally Files:

// MultiplePackageError describes a directory containing
// multiple buildable Go source files for multiple packages.
type MultiplePackageError struct {
    Dir      string   // directory containing files
    Packages []string // package names found
    Files    []string // corresponding files: Files[i] declares package Packages[i]
}

It is used in the following incorrect way in src/go/build/build.go#L690-L692:

} else if pkg != p.Name {
    return p, &MultiplePackageError{p.Dir, []string{firstFile, name}, []string{p.Name, pkg}}
}

If one rewrites that composite literal to use keyed fields, the mistake becomes obvious:

return p, &MultiplePackageError{
    Dir:      p.Dir,
    Packages: []string{firstFile, name}, // Packages being set to filenames.
    Files:    []string{p.Name, pkg},     // Files being set to package names.
}

Notably, the last 2 parameter values should be swapped.

@bradfitz
Copy link
Contributor

Nice find. We should have a vet rule detect all unkeyed literals when the struct has any two fields with the same type.

/cc @adonovan

@dmitshur
Copy link
Contributor Author

That sounds like a good idea.

For posterity, this issue was introduced in commit 4f80b50.

@alandonovan
Copy link
Contributor

We should have a vet rule detect all unkeyed literals when the struct has any two fields with the same type.

Perhaps we should also consider the case when field value in a literal is assignable to more than one field. This is a much larger set. For example:

type T struct { uint; int; float64 }
var _ = T{0, 1, 2}

@bradfitz
Copy link
Contributor

About two seconds after I wrote my comment I knew somebody would point that out. Yes, @alandonovan, assignable. :-)

@josharian
Copy link
Contributor

Huh. Apparently github doesn't send emails from linked issues. Anyway, I filed #9421 to track this proposed vet rule.

@bradfitz
Copy link
Contributor

Sent https://go-review.googlesource.com/2040 for this bug.

@bradfitz bradfitz self-assigned this Dec 22, 2014
@golang golang locked and limited conversation to collaborators Jun 25, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants