Skip to content

cmd/compile: incorrect invalid field error in generic code #50148

Closed

Description

@randall77

a.go:

package a

type S[T any] struct {
}

func (b *S[T]) build() *X[T] {
	return &X[T]{f:0}
}
type X[T any] struct {
	f int
}

b.go:

package b

import "./a"

func B() {
	var x a.S[int]
	_ = x
}

main.go:

package main

import "./b"

func main() {
	b.B()
}

This should compile successfully. Instead we get the error:

a.go:7:16: invalid field name a.f in struct initializer

This is an error from the old typechecker when importing code (from b into main, I think). The error occurs in the phase where we're building more instantiations after inlining.

This example is reduced from an example generated by my generifier. It was originally package html_test importing package html importing a generified version of package string.

@danscales

Activity

  1. self-assigned this
    on Dec 14, 2021
  2. added this to the Go1.18 milestone on Dec 14, 2021
  3. added
    okay-after-beta1Used by release team to mark a release-blocker issue as okay to resolve either before or after beta1
    on Dec 14, 2021
  4. gopherbot commented on Dec 14, 2021

    @gopherbot
    Contributor

    Change https://golang.org/cl/371554 mentions this issue: cmd/compile: allow field keys from different packages

  5. randall77 commented on Dec 14, 2021

    @randall77
    ContributorAuthor

    This error occurs as part of typechecking a new generic instantiation, with the generic definition being in a separate package. It's unhappy about typechecking a unexported field reference in a package other than the one being compiled.
    A similar error does not occur in 1.17, because during inlining we don't ever typecheck imported code - we just deserialize the types that were serialized on export.
    A similar error does not occur in 1.16, because the type checker is happy to typecheck unexported field references in other packages. The error case was added here (as part of 1.17 development): https://go-review.googlesource.com/c/go/+/277713 @mdempsky

    So I think we're ok allowing the typechecker to pass in this situation, as it used to 1.16 and earlier.
    I do want to understand more about why we're calling the typechecker here. I don't think it should be necessary, which would probably be a better fix if it works out.

  6. mdempsky commented on Dec 14, 2021

    @mdempsky
    Contributor

    Removing the types.LocalPkg check seems reasonable.

  7. locked and limited conversation to collaborators on Jun 22, 2023
  8. added a commit that references this issue on Sep 19, 2024
    7f23145
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

    FrozenDueToAgegenericsIssue is related to genericsokay-after-beta1Used by release team to mark a release-blocker issue as okay to resolve either before or after beta1

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions