cmd/compile,cmd/gofmt: use reflect.TypeFor

Use "reflect.TypeFor" to simplify the code.

Updates #60088

Change-Id: I93db6cbd4f02813d9a81f5d02996db8128cb81a9
GitHub-Last-Rev: 2aee64dac6
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>
This commit is contained in:
apocelipes 2025-09-11 19:08:37 +00:00 committed by Gopher Robot
parent 8320fe8f0e
commit 30d510ca2d
4 changed files with 30 additions and 30 deletions

View file

@ -383,14 +383,14 @@ func ParseFlags() {
// See the comment on type CmdFlags for the rules. // See the comment on type CmdFlags for the rules.
func registerFlags() { func registerFlags() {
var ( var (
boolType = reflect.TypeOf(bool(false)) boolType = reflect.TypeFor[bool]()
intType = reflect.TypeOf(int(0)) intType = reflect.TypeFor[int]()
stringType = reflect.TypeOf(string("")) stringType = reflect.TypeFor[string]()
ptrBoolType = reflect.TypeOf(new(bool)) ptrBoolType = reflect.TypeFor[*bool]()
ptrIntType = reflect.TypeOf(new(int)) ptrIntType = reflect.TypeFor[*int]()
ptrStringType = reflect.TypeOf(new(string)) ptrStringType = reflect.TypeFor[*string]()
countType = reflect.TypeOf(CountFlag(0)) countType = reflect.TypeFor[CountFlag]()
funcType = reflect.TypeOf((func(string))(nil)) funcType = reflect.TypeFor[func(string)]()
) )
v := reflect.ValueOf(&Flag).Elem() v := reflect.ValueOf(&Flag).Elem()

View file

@ -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) { func dumpNodes(w io.Writer, list Nodes, depth int) {
if len(list) == 0 { if len(list) == 0 {

View file

@ -49,25 +49,25 @@ func Init() {
// Note: this has to be called explicitly instead of being // Note: this has to be called explicitly instead of being
// an init function so it runs after the types package has // an init function so it runs after the types package has
// been properly initialized. // been properly initialized.
Type = FromReflect(reflect.TypeOf(abi.Type{})) Type = FromReflect(reflect.TypeFor[abi.Type]())
ArrayType = FromReflect(reflect.TypeOf(abi.ArrayType{})) ArrayType = FromReflect(reflect.TypeFor[abi.ArrayType]())
ChanType = FromReflect(reflect.TypeOf(abi.ChanType{})) ChanType = FromReflect(reflect.TypeFor[abi.ChanType]())
FuncType = FromReflect(reflect.TypeOf(abi.FuncType{})) FuncType = FromReflect(reflect.TypeFor[abi.FuncType]())
InterfaceType = FromReflect(reflect.TypeOf(abi.InterfaceType{})) InterfaceType = FromReflect(reflect.TypeFor[abi.InterfaceType]())
MapType = FromReflect(reflect.TypeOf(abi.MapType{})) MapType = FromReflect(reflect.TypeFor[abi.MapType]())
PtrType = FromReflect(reflect.TypeOf(abi.PtrType{})) PtrType = FromReflect(reflect.TypeFor[abi.PtrType]())
SliceType = FromReflect(reflect.TypeOf(abi.SliceType{})) SliceType = FromReflect(reflect.TypeFor[abi.SliceType]())
StructType = FromReflect(reflect.TypeOf(abi.StructType{})) StructType = FromReflect(reflect.TypeFor[abi.StructType]())
IMethod = FromReflect(reflect.TypeOf(abi.Imethod{})) IMethod = FromReflect(reflect.TypeFor[abi.Imethod]())
Method = FromReflect(reflect.TypeOf(abi.Method{})) Method = FromReflect(reflect.TypeFor[abi.Method]())
StructField = FromReflect(reflect.TypeOf(abi.StructField{})) StructField = FromReflect(reflect.TypeFor[abi.StructField]())
UncommonType = FromReflect(reflect.TypeOf(abi.UncommonType{})) UncommonType = FromReflect(reflect.TypeFor[abi.UncommonType]())
InterfaceSwitch = FromReflect(reflect.TypeOf(abi.InterfaceSwitch{})) InterfaceSwitch = FromReflect(reflect.TypeFor[abi.InterfaceSwitch]())
TypeAssert = FromReflect(reflect.TypeOf(abi.TypeAssert{})) 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 // Make sure abi functions are correct. These functions are used
// by the linker which doesn't have the ability to do type layout, // by the linker which doesn't have the ability to do type layout,

View file

@ -105,11 +105,11 @@ var (
objectPtrNil = reflect.ValueOf((*ast.Object)(nil)) objectPtrNil = reflect.ValueOf((*ast.Object)(nil))
scopePtrNil = reflect.ValueOf((*ast.Scope)(nil)) scopePtrNil = reflect.ValueOf((*ast.Scope)(nil))
identType = reflect.TypeOf((*ast.Ident)(nil)) identType = reflect.TypeFor[*ast.Ident]()
objectPtrType = reflect.TypeOf((*ast.Object)(nil)) objectPtrType = reflect.TypeFor[*ast.Object]()
positionType = reflect.TypeOf(token.NoPos) positionType = reflect.TypeFor[token.Pos]()
callExprType = reflect.TypeOf((*ast.CallExpr)(nil)) callExprType = reflect.TypeFor[*ast.CallExpr]()
scopePtrType = reflect.TypeOf((*ast.Scope)(nil)) scopePtrType = reflect.TypeFor[*ast.Scope]()
) )
// apply replaces each AST field x in val with f(x), returning val. // apply replaces each AST field x in val with f(x), returning val.