]> Cypherpunks.ru repositories - gostls13.git/commitdiff
go/types, cmd/compile/internal/types2: use regular type printing for unsafe.Pointer
authorRobert Griesemer <gri@golang.org>
Tue, 23 Feb 2021 01:02:46 +0000 (17:02 -0800)
committerRobert Griesemer <gri@golang.org>
Tue, 23 Feb 2021 04:13:24 +0000 (04:13 +0000)
Type string printing special-cased printing of unsafe.Pointer because
it's a built-in type; yet it's declared in a package like any other
imported or used-defined type (unlike built-in types such as int).

Use the same mechanism for printing unsafe.Pointer like any other
(non-basic) type. This will make it possible to use the package
Qualifier if so desired.

Fixes #44515.

Change-Id: I0dd1026f850737ecfc4bb99135cfb8e3c18be9e7
Reviewed-on: https://go-review.googlesource.com/c/go/+/295271
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
src/cmd/compile/internal/types2/issues_test.go
src/cmd/compile/internal/types2/typestring.go
src/go/types/issues_test.go
src/go/types/typestring.go

index a36b832f04037b5abb69db3da79a24fb39164773..e1f5c92fc47aabc5efec5da8cc2f0bffe164613e 100644 (file)
@@ -546,3 +546,25 @@ func TestIssue43088(t *testing.T) {
        Comparable(T1)
        Comparable(T2)
 }
+
+func TestIssue44515(t *testing.T) {
+       typ := Unsafe.Scope().Lookup("Pointer").Type()
+
+       got := TypeString(typ, nil)
+       want := "unsafe.Pointer"
+       if got != want {
+               t.Errorf("got %q; want %q", got, want)
+       }
+
+       qf := func(pkg *Package) string {
+               if pkg == Unsafe {
+                       return "foo"
+               }
+               return ""
+       }
+       got = TypeString(typ, qf)
+       want = "foo.Pointer"
+       if got != want {
+               t.Errorf("got %q; want %q", got, want)
+       }
+}
index af44624d2c1eb5a9f74e169e7ee61f8a69e0b944..40016697b7c90f99c6319f0dae6c9079f3db0767 100644 (file)
@@ -98,9 +98,15 @@ func writeType(buf *bytes.Buffer, typ Type, qf Qualifier, visited []Type) {
                buf.WriteString("<nil>")
 
        case *Basic:
-               if t.kind == UnsafePointer {
-                       buf.WriteString("unsafe.")
+               // exported basic types go into package unsafe
+               // (currently this is just unsafe.Pointer)
+               if isExported(t.name) {
+                       if obj, _ := Unsafe.scope.Lookup(t.name).(*TypeName); obj != nil {
+                               writeTypeName(buf, obj, qf)
+                               break
+                       }
                }
+
                if gcCompatibilityMode {
                        // forget the alias names
                        switch t.kind {
index 34850eb03487732119e394218702775a52232f2c..9ed2934c74dc49b286a64f3b7573eab5cc124cfb 100644 (file)
@@ -549,3 +549,25 @@ func TestIssue43088(t *testing.T) {
        Comparable(T1)
        Comparable(T2)
 }
+
+func TestIssue44515(t *testing.T) {
+       typ := Unsafe.Scope().Lookup("Pointer").Type()
+
+       got := TypeString(typ, nil)
+       want := "unsafe.Pointer"
+       if got != want {
+               t.Errorf("got %q; want %q", got, want)
+       }
+
+       qf := func(pkg *Package) string {
+               if pkg == Unsafe {
+                       return "foo"
+               }
+               return ""
+       }
+       got = TypeString(typ, qf)
+       want = "foo.Pointer"
+       if got != want {
+               t.Errorf("got %q; want %q", got, want)
+       }
+}
index 4697bd31e6636a82e60189bfeca783e2b85d87af..a0caded1606777e10bf746276cf485948296c98e 100644 (file)
@@ -9,6 +9,7 @@ package types
 import (
        "bytes"
        "fmt"
+       "go/token"
        "unicode/utf8"
 )
 
@@ -98,9 +99,15 @@ func writeType(buf *bytes.Buffer, typ Type, qf Qualifier, visited []Type) {
                buf.WriteString("<nil>")
 
        case *Basic:
-               if t.kind == UnsafePointer {
-                       buf.WriteString("unsafe.")
+               // exported basic types go into package unsafe
+               // (currently this is just unsafe.Pointer)
+               if token.IsExported(t.name) {
+                       if obj, _ := Unsafe.scope.Lookup(t.name).(*TypeName); obj != nil {
+                               writeTypeName(buf, obj, qf)
+                               break
+                       }
                }
+
                if gcCompatibilityMode {
                        // forget the alias names
                        switch t.kind {