aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorapocelipes <seve3r@outlook.com>2025-09-11 19:08:37 +0000
committerGopher Robot <gobot@golang.org>2025-09-15 05:13:05 -0700
commit30d510ca2de41fd7af21890ee23611b72048e462 (patch)
treeec49c1511b9309a9addf5b1a47372d0b50ba234a /src
parent8320fe8f0e5283eb67429de30b4e24be6a85c7a7 (diff)
downloadgo-30d510ca2de41fd7af21890ee23611b72048e462.tar.xz
cmd/compile,cmd/gofmt: use reflect.TypeFor
Use "reflect.TypeFor" to simplify the code. Updates #60088 Change-Id: I93db6cbd4f02813d9a81f5d02996db8128cb81a9 GitHub-Last-Rev: 2aee64dac6e13ef869aa73f2abf236650e1c1757 GitHub-Pull-Request: golang/go#75349 Reviewed-on: https://go-review.googlesource.com/c/go/+/701676 Reviewed-by: Mark Freeman <markfreeman@google.com> Auto-Submit: Keith Randall <khr@golang.org> Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Keith Randall <khr@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Diffstat (limited to 'src')
-rw-r--r--src/cmd/compile/internal/base/flag.go16
-rw-r--r--src/cmd/compile/internal/ir/fmt.go2
-rw-r--r--src/cmd/compile/internal/rttype/rttype.go32
-rw-r--r--src/cmd/gofmt/rewrite.go10
4 files changed, 30 insertions, 30 deletions
diff --git a/src/cmd/compile/internal/base/flag.go b/src/cmd/compile/internal/base/flag.go
index e87f57cdaa..1b52ab660c 100644
--- a/src/cmd/compile/internal/base/flag.go
+++ b/src/cmd/compile/internal/base/flag.go
@@ -383,14 +383,14 @@ func ParseFlags() {
// See the comment on type CmdFlags for the rules.
func registerFlags() {
var (
- boolType = reflect.TypeOf(bool(false))
- intType = reflect.TypeOf(int(0))
- stringType = reflect.TypeOf(string(""))
- ptrBoolType = reflect.TypeOf(new(bool))
- ptrIntType = reflect.TypeOf(new(int))
- ptrStringType = reflect.TypeOf(new(string))
- countType = reflect.TypeOf(CountFlag(0))
- funcType = reflect.TypeOf((func(string))(nil))
+ boolType = reflect.TypeFor[bool]()
+ intType = reflect.TypeFor[int]()
+ stringType = reflect.TypeFor[string]()
+ ptrBoolType = reflect.TypeFor[*bool]()
+ ptrIntType = reflect.TypeFor[*int]()
+ ptrStringType = reflect.TypeFor[*string]()
+ countType = reflect.TypeFor[CountFlag]()
+ funcType = reflect.TypeFor[func(string)]()
)
v := reflect.ValueOf(&Flag).Elem()
diff --git a/src/cmd/compile/internal/ir/fmt.go b/src/cmd/compile/internal/ir/fmt.go
index 31c610348b..ae4ff62652 100644
--- a/src/cmd/compile/internal/ir/fmt.go
+++ b/src/cmd/compile/internal/ir/fmt.go
@@ -1194,7 +1194,7 @@ func dumpNode(w io.Writer, n Node, depth int) {
}
}
-var nodeType = reflect.TypeOf((*Node)(nil)).Elem()
+var nodeType = reflect.TypeFor[Node]()
func dumpNodes(w io.Writer, list Nodes, depth int) {
if len(list) == 0 {
diff --git a/src/cmd/compile/internal/rttype/rttype.go b/src/cmd/compile/internal/rttype/rttype.go
index 925d3901d4..b8c9533991 100644
--- a/src/cmd/compile/internal/rttype/rttype.go
+++ b/src/cmd/compile/internal/rttype/rttype.go
@@ -49,25 +49,25 @@ func Init() {
// Note: this has to be called explicitly instead of being
// an init function so it runs after the types package has
// been properly initialized.
- Type = FromReflect(reflect.TypeOf(abi.Type{}))
- ArrayType = FromReflect(reflect.TypeOf(abi.ArrayType{}))
- ChanType = FromReflect(reflect.TypeOf(abi.ChanType{}))
- FuncType = FromReflect(reflect.TypeOf(abi.FuncType{}))
- InterfaceType = FromReflect(reflect.TypeOf(abi.InterfaceType{}))
- MapType = FromReflect(reflect.TypeOf(abi.MapType{}))
- PtrType = FromReflect(reflect.TypeOf(abi.PtrType{}))
- SliceType = FromReflect(reflect.TypeOf(abi.SliceType{}))
- StructType = FromReflect(reflect.TypeOf(abi.StructType{}))
+ Type = FromReflect(reflect.TypeFor[abi.Type]())
+ ArrayType = FromReflect(reflect.TypeFor[abi.ArrayType]())
+ ChanType = FromReflect(reflect.TypeFor[abi.ChanType]())
+ FuncType = FromReflect(reflect.TypeFor[abi.FuncType]())
+ InterfaceType = FromReflect(reflect.TypeFor[abi.InterfaceType]())
+ MapType = FromReflect(reflect.TypeFor[abi.MapType]())
+ PtrType = FromReflect(reflect.TypeFor[abi.PtrType]())
+ SliceType = FromReflect(reflect.TypeFor[abi.SliceType]())
+ StructType = FromReflect(reflect.TypeFor[abi.StructType]())
- IMethod = FromReflect(reflect.TypeOf(abi.Imethod{}))
- Method = FromReflect(reflect.TypeOf(abi.Method{}))
- StructField = FromReflect(reflect.TypeOf(abi.StructField{}))
- UncommonType = FromReflect(reflect.TypeOf(abi.UncommonType{}))
+ IMethod = FromReflect(reflect.TypeFor[abi.Imethod]())
+ Method = FromReflect(reflect.TypeFor[abi.Method]())
+ StructField = FromReflect(reflect.TypeFor[abi.StructField]())
+ UncommonType = FromReflect(reflect.TypeFor[abi.UncommonType]())
- InterfaceSwitch = FromReflect(reflect.TypeOf(abi.InterfaceSwitch{}))
- TypeAssert = FromReflect(reflect.TypeOf(abi.TypeAssert{}))
+ InterfaceSwitch = FromReflect(reflect.TypeFor[abi.InterfaceSwitch]())
+ TypeAssert = FromReflect(reflect.TypeFor[abi.TypeAssert]())
- ITab = FromReflect(reflect.TypeOf(abi.ITab{}))
+ ITab = FromReflect(reflect.TypeFor[abi.ITab]())
// Make sure abi functions are correct. These functions are used
// by the linker which doesn't have the ability to do type layout,
diff --git a/src/cmd/gofmt/rewrite.go b/src/cmd/gofmt/rewrite.go
index 8ed093041c..847ac510ce 100644
--- a/src/cmd/gofmt/rewrite.go
+++ b/src/cmd/gofmt/rewrite.go
@@ -105,11 +105,11 @@ var (
objectPtrNil = reflect.ValueOf((*ast.Object)(nil))
scopePtrNil = reflect.ValueOf((*ast.Scope)(nil))
- identType = reflect.TypeOf((*ast.Ident)(nil))
- objectPtrType = reflect.TypeOf((*ast.Object)(nil))
- positionType = reflect.TypeOf(token.NoPos)
- callExprType = reflect.TypeOf((*ast.CallExpr)(nil))
- scopePtrType = reflect.TypeOf((*ast.Scope)(nil))
+ identType = reflect.TypeFor[*ast.Ident]()
+ objectPtrType = reflect.TypeFor[*ast.Object]()
+ positionType = reflect.TypeFor[token.Pos]()
+ callExprType = reflect.TypeFor[*ast.CallExpr]()
+ scopePtrType = reflect.TypeFor[*ast.Scope]()
)
// apply replaces each AST field x in val with f(x), returning val.