Skip to content

Commit 30d510c

Browse files
apocelipesgopherbot
authored andcommitted
cmd/compile,cmd/gofmt: use reflect.TypeFor
Use "reflect.TypeFor" to simplify the code. Updates golang#60088 Change-Id: I93db6cbd4f02813d9a81f5d02996db8128cb81a9 GitHub-Last-Rev: 2aee64d GitHub-Pull-Request: golang#75349 Reviewed-on: https://go-review.googlesource.com/c/go/+/701676 Reviewed-by: Mark Freeman <[email protected]> Auto-Submit: Keith Randall <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Keith Randall <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 8320fe8 commit 30d510c

File tree

4 files changed

+30
-30
lines changed

4 files changed

+30
-30
lines changed

src/cmd/compile/internal/base/flag.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -383,14 +383,14 @@ func ParseFlags() {
383383
// See the comment on type CmdFlags for the rules.
384384
func registerFlags() {
385385
var (
386-
boolType = reflect.TypeOf(bool(false))
387-
intType = reflect.TypeOf(int(0))
388-
stringType = reflect.TypeOf(string(""))
389-
ptrBoolType = reflect.TypeOf(new(bool))
390-
ptrIntType = reflect.TypeOf(new(int))
391-
ptrStringType = reflect.TypeOf(new(string))
392-
countType = reflect.TypeOf(CountFlag(0))
393-
funcType = reflect.TypeOf((func(string))(nil))
386+
boolType = reflect.TypeFor[bool]()
387+
intType = reflect.TypeFor[int]()
388+
stringType = reflect.TypeFor[string]()
389+
ptrBoolType = reflect.TypeFor[*bool]()
390+
ptrIntType = reflect.TypeFor[*int]()
391+
ptrStringType = reflect.TypeFor[*string]()
392+
countType = reflect.TypeFor[CountFlag]()
393+
funcType = reflect.TypeFor[func(string)]()
394394
)
395395

396396
v := reflect.ValueOf(&Flag).Elem()

src/cmd/compile/internal/ir/fmt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1194,7 +1194,7 @@ func dumpNode(w io.Writer, n Node, depth int) {
11941194
}
11951195
}
11961196

1197-
var nodeType = reflect.TypeOf((*Node)(nil)).Elem()
1197+
var nodeType = reflect.TypeFor[Node]()
11981198

11991199
func dumpNodes(w io.Writer, list Nodes, depth int) {
12001200
if len(list) == 0 {

src/cmd/compile/internal/rttype/rttype.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,25 @@ func Init() {
4949
// Note: this has to be called explicitly instead of being
5050
// an init function so it runs after the types package has
5151
// been properly initialized.
52-
Type = FromReflect(reflect.TypeOf(abi.Type{}))
53-
ArrayType = FromReflect(reflect.TypeOf(abi.ArrayType{}))
54-
ChanType = FromReflect(reflect.TypeOf(abi.ChanType{}))
55-
FuncType = FromReflect(reflect.TypeOf(abi.FuncType{}))
56-
InterfaceType = FromReflect(reflect.TypeOf(abi.InterfaceType{}))
57-
MapType = FromReflect(reflect.TypeOf(abi.MapType{}))
58-
PtrType = FromReflect(reflect.TypeOf(abi.PtrType{}))
59-
SliceType = FromReflect(reflect.TypeOf(abi.SliceType{}))
60-
StructType = FromReflect(reflect.TypeOf(abi.StructType{}))
52+
Type = FromReflect(reflect.TypeFor[abi.Type]())
53+
ArrayType = FromReflect(reflect.TypeFor[abi.ArrayType]())
54+
ChanType = FromReflect(reflect.TypeFor[abi.ChanType]())
55+
FuncType = FromReflect(reflect.TypeFor[abi.FuncType]())
56+
InterfaceType = FromReflect(reflect.TypeFor[abi.InterfaceType]())
57+
MapType = FromReflect(reflect.TypeFor[abi.MapType]())
58+
PtrType = FromReflect(reflect.TypeFor[abi.PtrType]())
59+
SliceType = FromReflect(reflect.TypeFor[abi.SliceType]())
60+
StructType = FromReflect(reflect.TypeFor[abi.StructType]())
6161

62-
IMethod = FromReflect(reflect.TypeOf(abi.Imethod{}))
63-
Method = FromReflect(reflect.TypeOf(abi.Method{}))
64-
StructField = FromReflect(reflect.TypeOf(abi.StructField{}))
65-
UncommonType = FromReflect(reflect.TypeOf(abi.UncommonType{}))
62+
IMethod = FromReflect(reflect.TypeFor[abi.Imethod]())
63+
Method = FromReflect(reflect.TypeFor[abi.Method]())
64+
StructField = FromReflect(reflect.TypeFor[abi.StructField]())
65+
UncommonType = FromReflect(reflect.TypeFor[abi.UncommonType]())
6666

67-
InterfaceSwitch = FromReflect(reflect.TypeOf(abi.InterfaceSwitch{}))
68-
TypeAssert = FromReflect(reflect.TypeOf(abi.TypeAssert{}))
67+
InterfaceSwitch = FromReflect(reflect.TypeFor[abi.InterfaceSwitch]())
68+
TypeAssert = FromReflect(reflect.TypeFor[abi.TypeAssert]())
6969

70-
ITab = FromReflect(reflect.TypeOf(abi.ITab{}))
70+
ITab = FromReflect(reflect.TypeFor[abi.ITab]())
7171

7272
// Make sure abi functions are correct. These functions are used
7373
// by the linker which doesn't have the ability to do type layout,

src/cmd/gofmt/rewrite.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ var (
105105
objectPtrNil = reflect.ValueOf((*ast.Object)(nil))
106106
scopePtrNil = reflect.ValueOf((*ast.Scope)(nil))
107107

108-
identType = reflect.TypeOf((*ast.Ident)(nil))
109-
objectPtrType = reflect.TypeOf((*ast.Object)(nil))
110-
positionType = reflect.TypeOf(token.NoPos)
111-
callExprType = reflect.TypeOf((*ast.CallExpr)(nil))
112-
scopePtrType = reflect.TypeOf((*ast.Scope)(nil))
108+
identType = reflect.TypeFor[*ast.Ident]()
109+
objectPtrType = reflect.TypeFor[*ast.Object]()
110+
positionType = reflect.TypeFor[token.Pos]()
111+
callExprType = reflect.TypeFor[*ast.CallExpr]()
112+
scopePtrType = reflect.TypeFor[*ast.Scope]()
113113
)
114114

115115
// apply replaces each AST field x in val with f(x), returning val.

0 commit comments

Comments
 (0)