diff options
author | Robert Griesemer <gri@golang.org> | 2023-05-04 14:09:27 -0700 |
---|---|---|
committer | Gopher Robot <gobot@golang.org> | 2023-05-05 22:31:54 +0000 |
commit | 05ca41d3efdd1fc676bf3ff10afa95f91607eb0a (patch) | |
tree | 3075a1d1f17f2c88bf46bf6ff6e5e482718a95e5 /src/go/types/predicates.go | |
parent | 445e520d494aec4f4f34d6a67a0a6231a44fe1b5 (diff) | |
download | go-05ca41d3efdd1fc676bf3ff10afa95f91607eb0a.tar.gz go-05ca41d3efdd1fc676bf3ff10afa95f91607eb0a.zip |
go/types, types2: factor out maximum type computation
For untyped constant binary operations we need to determine the
"maximum" (untyped) type which includes both constant types.
Factor out this functionality.
Change-Id: If42bd793d38423322885a3063a4321bd56443b36
Reviewed-on: https://go-review.googlesource.com/c/go/+/492619
Reviewed-by: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'src/go/types/predicates.go')
-rw-r--r-- | src/go/types/predicates.go | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/go/types/predicates.go b/src/go/types/predicates.go index e09e774f2a..b821b584c1 100644 --- a/src/go/types/predicates.go +++ b/src/go/types/predicates.go @@ -512,3 +512,23 @@ func Default(t Type) Type { } return t } + +// maxType returns the "largest" type that encompasses both x and y. +// If x and y are different untyped numeric types, the result is the type of x or y +// that appears later in this list: integer, rune, floating-point, complex. +// Otherwise, if x != y, the result is nil. +func maxType(x, y Type) Type { + // We only care about untyped types (for now), so == is good enough. + // TODO(gri) investigate generalizing this function to simplify code elsewhere + if x == y { + return x + } + if isUntyped(x) && isUntyped(y) && isNumeric(x) && isNumeric(y) { + // untyped types are basic types + if x.(*Basic).kind > y.(*Basic).kind { + return x + } + return y + } + return nil +} |