Skip to content

cmd/compile: handle alias types in recursive types more consistently #50729

Closed
@griesemer

Description

@griesemer

The following code

package p

var x T[B]

type T[_ any] struct{}
type A T[B /* ERROR invalid use of type alias */ ]
type B = T[A]

reports an error when we use B in a recursive type definition due to the way type aliases are handled inside the type checker. In this (and possibly many other similar) cases, the error can be avoided by restructuring the code slightly. This version compiles without error (declaration of x moved past the type declarations):

package p

type T[_ any] struct{}
type A T[B]
type B = T[A]

var x T[B]

The compiler should be able to avoid this error on its own.

Root cause: Currently, the type checker doesn't have a "forwarding mechanism" for type aliases that are being "used" before their respective type is fully known. To solve this problem in general, a forwarding mechanism/type needs to be introduced. This is only an issue with some recursive type definitions involving type aliases.

cc: @findleyr

Metadata

Metadata

Assignees

Labels

NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.compiler/runtimeIssues related to the Go compiler and/or runtime.

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions