]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: add Alias.Rhs
authorAlan Donovan <adonovan@google.com>
Wed, 24 Apr 2024 20:51:20 +0000 (16:51 -0400)
committerGopher Robot <gobot@golang.org>
Wed, 24 Apr 2024 21:50:16 +0000 (21:50 +0000)
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>
api/next/66559.txt [new file with mode: 0644]
doc/next/6-stdlib/99-minor/go/types/66559.md [new file with mode: 0644]
src/cmd/compile/internal/types2/alias.go
src/cmd/compile/internal/types2/api.go
src/cmd/compile/internal/types2/api_test.go
src/go/types/alias.go
src/go/types/api_test.go

diff --git a/api/next/66559.txt b/api/next/66559.txt
new file mode 100644 (file)
index 0000000..8b83bcc
--- /dev/null
@@ -0,0 +1 @@
+pkg go/types, method (*Alias) Rhs() Type #66559
diff --git a/doc/next/6-stdlib/99-minor/go/types/66559.md b/doc/next/6-stdlib/99-minor/go/types/66559.md
new file mode 100644 (file)
index 0000000..fbaf391
--- /dev/null
@@ -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))
index 030f6cd82770cf2db8147bc7425284a957b6d2a0..9b7a13f81efbe05125ce05e9dee65328ad43af28 100644 (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) 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.
-// func (a *Alias) Rhs() Type { return a.fromRHS }
+// Rhs returns the type R on the right-hand side of an alias
+// 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;
 // otherwise it follows t's alias chain until it
index 36d900401df2393abd97a122658580f79a48aad7..029d105e2e2b0fcf5e8357efc9f50a0ed770709c 100644 (file)
@@ -475,10 +475,3 @@ func (conf *Config) Check(path string, files []*syntax.File, info *Info) (*Packa
        pkg := NewPackage(path, "")
        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 }
index 840a3f3bdceaa561903a43126c98dd064ac14c04..cf3c105f6c638391a8a9848e51db06958199fec3 100644 (file)
@@ -3005,3 +3005,20 @@ type B = T[A]
                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)
+       }
+}
index 963eb92d352a1b0a2f248885171c894984906fc9..56d2ad0c975f90d5f93031f01afb73e4c9c9102d 100644 (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) 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.
-// func (a *Alias) Rhs() Type { return a.fromRHS }
+// Rhs returns the type R on the right-hand side of an alias
+// 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;
 // otherwise it follows t's alias chain until it
index 564bbc2423c0135886a4006e4adf58caaea49a61..6f8dddb936fb572498e4fc05a70eeb764fa21650 100644 (file)
@@ -3014,3 +3014,21 @@ type B = T[A]
                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)
+       }
+}