mirror of
https://github.com/golang/go
synced 2024-09-04 23:44:16 +00:00
cmd/compile: pop instantiations of local types when leaving scope
Since we use existing instantiations from the symbol table when possible (to make sure each instantiation is unique), we need to pop instantiations of local types when leaving the containing scope. g.stmts() now pushes and pops scope, and we do a Pushdcl() in g.typ0() when creating an instantiation of a local type. Non-instantiated local types (generic or not) are translated directly from types2, so they don't need to be pushed/popped. We don't export function bodies with local types, so there is no issue during import. We still don't support local types in generic functions/methods. Fixes #50177 Change-Id: If2d2fe71aec003d13f0338565c7a0da2c9580a14 Reviewed-on: https://go-review.googlesource.com/c/go/+/372654 Reviewed-by: Keith Randall <khr@golang.org> Trust: Dan Scales <danscales@google.com> Run-TryBot: Dan Scales <danscales@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
parent
1c8f9d2c97
commit
e39ab9b01c
|
@ -13,8 +13,10 @@ import (
|
|||
"cmd/internal/src"
|
||||
)
|
||||
|
||||
// stmts creates nodes for a slice of statements that form a scope.
|
||||
func (g *irgen) stmts(stmts []syntax.Stmt) []ir.Node {
|
||||
var nodes []ir.Node
|
||||
types.Markdcl()
|
||||
for _, stmt := range stmts {
|
||||
switch s := g.stmt(stmt).(type) {
|
||||
case nil: // EmptyStmt
|
||||
|
@ -24,6 +26,7 @@ func (g *irgen) stmts(stmts []syntax.Stmt) []ir.Node {
|
|||
nodes = append(nodes, s)
|
||||
}
|
||||
}
|
||||
types.Popdcl()
|
||||
return nodes
|
||||
}
|
||||
|
||||
|
|
|
@ -123,7 +123,14 @@ func (g *irgen) typ0(typ types2.Type) *types.Type {
|
|||
// Make sure the base generic type exists in type1 (it may
|
||||
// not yet if we are referecing an imported generic type, as
|
||||
// opposed to a generic type declared in this package).
|
||||
_ = g.obj(typ.Origin().Obj())
|
||||
base := g.obj(typ.Origin().Obj())
|
||||
if base.Class == ir.PAUTO {
|
||||
// If the base type is a local type, we want to pop
|
||||
// this instantiated type symbol/definition when we
|
||||
// leave the containing block, so we don't use it
|
||||
// incorrectly later.
|
||||
types.Pushdcl(s)
|
||||
}
|
||||
|
||||
// Create a forwarding type first and put it in the g.typs
|
||||
// map, in order to deal with recursive generic types
|
||||
|
|
101
test/typeparam/issue50177.go
Normal file
101
test/typeparam/issue50177.go
Normal file
|
@ -0,0 +1,101 @@
|
|||
// compile -G=3
|
||||
|
||||
// Copyright 2021 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Fn[T any] func(T)
|
||||
type FnErr[T any] func(T) error
|
||||
|
||||
// Test that local generic types across functions don't conflict, and they also don't
|
||||
// conflict with local non-generic types and local variables.
|
||||
func caller0() {
|
||||
type X[T any] struct {
|
||||
fn Fn[int]
|
||||
}
|
||||
|
||||
x := X[int]{func(v int) { fmt.Println(v) }}
|
||||
x.fn(0)
|
||||
}
|
||||
|
||||
func caller1(val int) {
|
||||
type X[T any] struct {
|
||||
fn FnErr[int]
|
||||
}
|
||||
|
||||
x := X[int]{func(v int) error { fmt.Println(v); return nil }}
|
||||
x.fn(0)
|
||||
}
|
||||
|
||||
func caller1a(val int) {
|
||||
type X struct {
|
||||
fn func(float64) error
|
||||
}
|
||||
|
||||
x := X{func(v float64) error { fmt.Println(v); return nil }}
|
||||
x.fn(float64(3.2))
|
||||
}
|
||||
|
||||
func caller1b(val int) {
|
||||
type Y struct {
|
||||
fn func(float64) error
|
||||
}
|
||||
|
||||
X := Y{func(v float64) error { fmt.Println(v); return nil }}
|
||||
X.fn(float64(3.2))
|
||||
}
|
||||
|
||||
// Test that local generic types within different if clauses don't conflict.
|
||||
func caller2(val int) {
|
||||
if val > 2 {
|
||||
type X[T any] struct {
|
||||
fn func(v int) float64
|
||||
}
|
||||
|
||||
x := X[int]{func(v int) float64 { fmt.Println(v); return 1.5 }}
|
||||
x.fn(0)
|
||||
} else {
|
||||
type X[T any] struct {
|
||||
fn func(v int) int
|
||||
}
|
||||
x := X[int]{func(v int) int { fmt.Println(v); return 5 }}
|
||||
x.fn(0)
|
||||
}
|
||||
}
|
||||
|
||||
// Test that local generic types within different cases don't conflict with each
|
||||
// other or with local non-generic types or local variables.
|
||||
func caller3(val int) {
|
||||
switch val {
|
||||
case 0:
|
||||
type X[T any] struct {
|
||||
fn func(v int) float64
|
||||
}
|
||||
|
||||
x := X[int]{func(v int) float64 { fmt.Println(v); return 1.5 }}
|
||||
x.fn(0)
|
||||
case 1:
|
||||
type X[T any] struct {
|
||||
fn func(v int) int
|
||||
}
|
||||
x := X[int]{func(v int) int { fmt.Println(v); return 5 }}
|
||||
x.fn(0)
|
||||
case 2:
|
||||
type X struct {
|
||||
fn func(v int) bool
|
||||
}
|
||||
x := X{func(v int) bool { fmt.Println(v); return false }}
|
||||
x.fn(0)
|
||||
case 3:
|
||||
type Y struct {
|
||||
fn func(v int) bool
|
||||
}
|
||||
X := Y{func(v int) bool { fmt.Println(v); return false }}
|
||||
X.fn(0)
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue