1
0
mirror of https://github.com/golang/go synced 2024-11-25 11:37:57 -07:00

go/types: add Alias.Rhs

This method returns the type on the right-hand side of an
alias declaration such as type L = R.

Fixes #66559

Change-Id: I396f2d999680ad251f47cdde20856ae20fc1c40a
Reviewed-on: https://go-review.googlesource.com/c/go/+/581615
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Alan Donovan <adonovan@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
This commit is contained in:
Alan Donovan 2024-04-24 16:51:20 -04:00 committed by Gopher Robot
parent e689118852
commit 9b9de261bd
7 changed files with 45 additions and 17 deletions

1
api/next/66559.txt Normal file
View File

@ -0,0 +1 @@
pkg go/types, method (*Alias) Rhs() Type #66559

View File

@ -0,0 +1,3 @@
The [Alias] type now has an [Rhs] method that returns the type on the
right-hand side of its declaration: given `type A = B`, the `Rhs` of A
is B. ([#66559](/issue/12345))

View File

@ -32,11 +32,9 @@ func (a *Alias) Obj() *TypeName { return a.obj }
func (a *Alias) Underlying() Type { return unalias(a).Underlying() } func (a *Alias) Underlying() Type { return unalias(a).Underlying() }
func (a *Alias) String() string { return TypeString(a, nil) } func (a *Alias) String() string { return TypeString(a, nil) }
// TODO(adonovan): uncomment when proposal #66559 is accepted. // Rhs returns the type R on the right-hand side of an alias
// // declaration "type A = R", which may be another alias.
// // Rhs returns the type R on the right-hand side of an alias func (a *Alias) Rhs() Type { return a.fromRHS }
// // declaration "type A = R", which may be another alias.
// func (a *Alias) Rhs() Type { return a.fromRHS }
// Unalias returns t if it is not an alias type; // Unalias returns t if it is not an alias type;
// otherwise it follows t's alias chain until it // otherwise it follows t's alias chain until it

View File

@ -475,10 +475,3 @@ func (conf *Config) Check(path string, files []*syntax.File, info *Info) (*Packa
pkg := NewPackage(path, "") pkg := NewPackage(path, "")
return pkg, NewChecker(conf, pkg, info).Files(files) return pkg, NewChecker(conf, pkg, info).Files(files)
} }
// Rhs returns the type R on the right-hand side of an alias
// declaration "type A = R", which may be another alias.
//
// TODO(adonovan): move to alias.go (common with go/types) once
// proposal #66559 is accepted.
func (a *Alias) Rhs() Type { return a.fromRHS }

View File

@ -3005,3 +3005,20 @@ type B = T[A]
t.Errorf("Unalias(type B = T[A]) = %q, want %q", got, want) t.Errorf("Unalias(type B = T[A]) = %q, want %q", got, want)
} }
} }
func TestAlias_Rhs(t *testing.T) {
const src = `package p
type A = B
type B = C
type C = int
`
pkg := mustTypecheck(src, &Config{EnableAlias: true}, nil)
A := pkg.Scope().Lookup("A")
got, want := A.Type().(*Alias).Rhs().String(), "p.B"
if got != want {
t.Errorf("A.Rhs = %s, want %s", got, want)
}
}

View File

@ -35,11 +35,9 @@ func (a *Alias) Obj() *TypeName { return a.obj }
func (a *Alias) Underlying() Type { return unalias(a).Underlying() } func (a *Alias) Underlying() Type { return unalias(a).Underlying() }
func (a *Alias) String() string { return TypeString(a, nil) } func (a *Alias) String() string { return TypeString(a, nil) }
// TODO(adonovan): uncomment when proposal #66559 is accepted. // Rhs returns the type R on the right-hand side of an alias
// // declaration "type A = R", which may be another alias.
// // Rhs returns the type R on the right-hand side of an alias func (a *Alias) Rhs() Type { return a.fromRHS }
// // declaration "type A = R", which may be another alias.
// func (a *Alias) Rhs() Type { return a.fromRHS }
// Unalias returns t if it is not an alias type; // Unalias returns t if it is not an alias type;
// otherwise it follows t's alias chain until it // otherwise it follows t's alias chain until it

View File

@ -3014,3 +3014,21 @@ type B = T[A]
t.Errorf("Unalias(type B = T[A]) = %q, want %q", got, want) t.Errorf("Unalias(type B = T[A]) = %q, want %q", got, want)
} }
} }
func TestAlias_Rhs(t *testing.T) {
t.Setenv("GODEBUG", "gotypesalias=1")
const src = `package p
type A = B
type B = C
type C = int
`
pkg := mustTypecheck(src, nil, nil)
A := pkg.Scope().Lookup("A")
got, want := A.Type().(*Alias).Rhs().String(), "p.B"
if got != want {
t.Errorf("A.Rhs = %s, want %s", got, want)
}
}