Skip to content

Commit 9b9de26

Browse files
adonovangopherbot
authored andcommitted
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 <[email protected]> Auto-Submit: Alan Donovan <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent e689118 commit 9b9de26

File tree

7 files changed

+45
-17
lines changed

7 files changed

+45
-17
lines changed

api/next/66559.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pkg go/types, method (*Alias) Rhs() Type #66559
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The [Alias] type now has an [Rhs] method that returns the type on the
2+
right-hand side of its declaration: given `type A = B`, the `Rhs` of A
3+
is B. ([#66559](/issue/12345))

src/cmd/compile/internal/types2/alias.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,9 @@ func (a *Alias) Obj() *TypeName { return a.obj }
3232
func (a *Alias) Underlying() Type { return unalias(a).Underlying() }
3333
func (a *Alias) String() string { return TypeString(a, nil) }
3434

35-
// TODO(adonovan): uncomment when proposal #66559 is accepted.
36-
//
37-
// // Rhs returns the type R on the right-hand side of an alias
38-
// // declaration "type A = R", which may be another alias.
39-
// func (a *Alias) Rhs() Type { return a.fromRHS }
35+
// Rhs returns the type R on the right-hand side of an alias
36+
// declaration "type A = R", which may be another alias.
37+
func (a *Alias) Rhs() Type { return a.fromRHS }
4038

4139
// Unalias returns t if it is not an alias type;
4240
// otherwise it follows t's alias chain until it

src/cmd/compile/internal/types2/api.go

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

src/cmd/compile/internal/types2/api_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -3005,3 +3005,20 @@ type B = T[A]
30053005
t.Errorf("Unalias(type B = T[A]) = %q, want %q", got, want)
30063006
}
30073007
}
3008+
3009+
func TestAlias_Rhs(t *testing.T) {
3010+
const src = `package p
3011+
3012+
type A = B
3013+
type B = C
3014+
type C = int
3015+
`
3016+
3017+
pkg := mustTypecheck(src, &Config{EnableAlias: true}, nil)
3018+
A := pkg.Scope().Lookup("A")
3019+
3020+
got, want := A.Type().(*Alias).Rhs().String(), "p.B"
3021+
if got != want {
3022+
t.Errorf("A.Rhs = %s, want %s", got, want)
3023+
}
3024+
}

src/go/types/alias.go

+3-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/go/types/api_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -3014,3 +3014,21 @@ type B = T[A]
30143014
t.Errorf("Unalias(type B = T[A]) = %q, want %q", got, want)
30153015
}
30163016
}
3017+
3018+
func TestAlias_Rhs(t *testing.T) {
3019+
t.Setenv("GODEBUG", "gotypesalias=1")
3020+
const src = `package p
3021+
3022+
type A = B
3023+
type B = C
3024+
type C = int
3025+
`
3026+
3027+
pkg := mustTypecheck(src, nil, nil)
3028+
A := pkg.Scope().Lookup("A")
3029+
3030+
got, want := A.Type().(*Alias).Rhs().String(), "p.B"
3031+
if got != want {
3032+
t.Errorf("A.Rhs = %s, want %s", got, want)
3033+
}
3034+
}

0 commit comments

Comments
 (0)