Skip to content

cmd/compile: panic on generic struct literal #48538

Closed

Description

@rogpeppe

commit a83a558

I compiled this program:

package main

func main() {
}

type C interface {
	struct{ b1, b2 string }
}

func f[A C]() A {
	return A{
		b1: "a",
		b2: "b",
	}
}

I saw this panic:

./tst.go:12:3: internal compiler error: missing type for &{b1 {{{0xc000114000 12 3}}}} (*syntax.Name)

goroutine 1 [running]:
runtime/debug.Stack()
	/home/rogpeppe/go/src/runtime/debug/stack.go:24 +0x65
cmd/compile/internal/base.FatalfAt({0xc61920, 0x0}, {0xce878d, 0x18}, {0xc0000aed30, 0x2, 0x2})
	/home/rogpeppe/go/src/cmd/compile/internal/base/print.go:227 +0x154
cmd/compile/internal/noder.(*irgen).expr(0xc00013c000, {0xe35d48, 0xc0001281c0})
	/home/rogpeppe/go/src/cmd/compile/internal/noder/expr.go:32 +0x214
cmd/compile/internal/noder.(*irgen).compLit(0xc00013c000, {0xe34dc0, 0xc0001145d0}, 0xc000124050)
	/home/rogpeppe/go/src/cmd/compile/internal/noder/expr.go:391 +0x4c5
cmd/compile/internal/noder.(*irgen).expr0(0xc00013c000, {0xe34dc0, 0xc0001145d0}, {0xe359e8, 0xc000124050})
	/home/rogpeppe/go/src/cmd/compile/internal/noder/expr.go:107 +0x7ae
cmd/compile/internal/noder.(*irgen).expr(0xc00013c000, {0xe359e8, 0xc000124050})
	/home/rogpeppe/go/src/cmd/compile/internal/noder/expr.go:81 +0x5ff
cmd/compile/internal/noder.(*irgen).exprs(0xc0000af378, {0xc0000af368, 0x1, 0xc000114000})
	/home/rogpeppe/go/src/cmd/compile/internal/noder/expr.go:369 +0x8e
cmd/compile/internal/noder.(*irgen).exprList(0x40ce0b, {0xe359e8, 0xc000124050})
	/home/rogpeppe/go/src/cmd/compile/internal/noder/expr.go:352 +0x85
cmd/compile/internal/noder.(*irgen).stmt(0xc00013c000, {0xe35dd8, 0xc000128180})
	/home/rogpeppe/go/src/cmd/compile/internal/noder/stmt.go:130 +0x9c5
cmd/compile/internal/noder.(*irgen).stmts(0xc0001307e0, {0xc00011c060, 0x1, 0xc0000af6a0})
	/home/rogpeppe/go/src/cmd/compile/internal/noder/stmt.go:19 +0xaf
cmd/compile/internal/noder.(*irgen).funcBody(0xc00013c000, 0xc00011e420, 0xb7151d, 0xc00012a0c0, 0xc00012a100)
	/home/rogpeppe/go/src/cmd/compile/internal/noder/func.go:45 +0x25f
cmd/compile/internal/noder.(*irgen).funcDecl.func1()
	/home/rogpeppe/go/src/cmd/compile/internal/noder/decl.go:128 +0x68
cmd/compile/internal/noder.(*irgen).generate(0xc00013c000, {0xc000072b50, 0x2, 0xb})
	/home/rogpeppe/go/src/cmd/compile/internal/noder/irgen.go:262 +0x1df
cmd/compile/internal/noder.check2({0xc000072b50, 0x2, 0x2})
	/home/rogpeppe/go/src/cmd/compile/internal/noder/irgen.go:93 +0x175
cmd/compile/internal/noder.LoadPackage({0xc00001e210, 0x2, 0x0})
	/home/rogpeppe/go/src/cmd/compile/internal/noder/noder.go:90 +0x335
cmd/compile/internal/gc.Main(0xd08420)
	/home/rogpeppe/go/src/cmd/compile/internal/gc/main.go:190 +0xaf3
main.main()
	/home/rogpeppe/go/src/cmd/compile/main.go:55 +0xdd

Activity

  1. rogpeppe commented on Sep 22, 2021

    @rogpeppe
    ContributorAuthor

    See also #45396

  2. WangLeonard commented on Sep 22, 2021

    @WangLeonard
    Contributor

    I tried to analyze this problem.
    I think we should record the key of return A{} in

    // 0 <= i < len(fields)

    like this,
    check.recordTypeAndValue(key, variable, etyp, nil)
    or may be need to record the value too, like this
    check.recordTypeAndValue(key, variable, etyp, constant.Make(kv.Value))

    But there are still other problems, will Fatalf when transform A{} on

    base.Fatalf("transformCompLit %v", t.Kind())

    hope it will be helpful to who fix the problem in the future.
    Thank you!

  3. danscales commented on Sep 22, 2021

    @danscales
    Contributor

    @griesemer @findleyr Unusual case where the type of the return value of f is strictly (and only) determined by the type constraint C which specifies a specific struct type.

    I'm assuming that's why types2 is not returning a type (as it normally does) for the b1 field of A in the struct literal. If we don't want to disallow this case somehow, then I guess types2 has to understand and use the type of A from the type constraint, so it can give a real type for b1.

  4. added this to the Go1.18 milestone on Sep 22, 2021
  5. added
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on Sep 22, 2021
  6. griesemer commented on Sep 28, 2021

    @griesemer
    Contributor

    Per discussion, the issue here is that we need to agree upon what to do exactly with underlying/operational types of type parameters. Once we have made that decision, this (and many other cases) should fall out naturally.

  7. toothrot commented on Oct 13, 2021

    @toothrot
    Contributor

    Friendly ping that this issue is marked as a release-blocker for Go 1.18. Are there any updates?

  8. added
    NeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.
    and removed
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on Oct 13, 2021
  9. griesemer commented on Oct 13, 2021

    @griesemer
    Contributor

    This is easy to address but needs a decision. Notably, the type checker accepts this at the moment, but perhaps it shouldn't, at least for now. @ianlancetaylor any thoughts?

  10. danscales commented on Oct 21, 2021

    @danscales
    Contributor

    Robert has a singleUnder() type structure function that he will be checking in (and exporting) in the next few days, which will allow me to fix this bug.

  11. gopherbot commented on Oct 28, 2021

    @gopherbot
    Contributor

    Change https://golang.org/cl/359275 mentions this issue: cmd/compile/internal/types2: export Structure function

  12. griesemer commented on Oct 28, 2021

    @griesemer
    Contributor

    This program is now considered valid and will be accepted by the type checker because A is a type parameter with a single underlying type (this will apply generally to many other operations).

  13. removed their assignment
    on Oct 28, 2021
  14. added
    NeedsFixThe path to resolution is known, but the work has not been done.
    and removed
    NeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.
    on Oct 28, 2021
  15. gopherbot commented on Oct 28, 2021

    @gopherbot
    Contributor

    Change https://golang.org/cl/359335 mentions this issue: cmd/compile: use Structure() to get single underlying type of typeparam.

  16. locked and limited conversation to collaborators on Jun 23, 2023
  17. added a commit that references this issue on Sep 19, 2024
    5d6d9f5
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