From 97cb0113a358a24931bc91c956da0cb023f2776c Mon Sep 17 00:00:00 2001 From: Dan Scales Date: Wed, 2 Jun 2021 00:03:25 -0700 Subject: [dev.typeparams] cmd/compile: fix export/import of constants with typeparam type A constant will have a TYPEPARAM type if it appears in a place where it must match that typeparam type (e.g. in a binary operation with a variable of that typeparam type). If so, then we must write out its actual constant kind as well, so its constant val can be read in properly during import. Fixed some export/import tests which were casting some untyped constants to avoid this problem. Change-Id: I285ad8f1c8febbe526769c96e6b27acbd23050f0 Reviewed-on: https://go-review.googlesource.com/c/go/+/324189 Run-TryBot: Dan Scales TryBot-Result: Go Bot Reviewed-by: Keith Randall Reviewed-by: Cuong Manh Le --- test/typeparam/fact.go | 6 +++--- test/typeparam/factimp.dir/a.go | 6 +++--- test/typeparam/listimp.dir/a.go | 3 +-- 3 files changed, 7 insertions(+), 8 deletions(-) (limited to 'test/typeparam') diff --git a/test/typeparam/fact.go b/test/typeparam/fact.go index 16b2adf6fb..ea86ae3e02 100644 --- a/test/typeparam/fact.go +++ b/test/typeparam/fact.go @@ -9,10 +9,10 @@ package main import "fmt" func fact[T interface { type int, int64, float64 }](n T) T { - if n == T(1) { - return T(1) + if n == 1 { + return 1 } - return n * fact(n - T(1)) + return n * fact(n - 1) } func main() { diff --git a/test/typeparam/factimp.dir/a.go b/test/typeparam/factimp.dir/a.go index e11575e66e..3552474382 100644 --- a/test/typeparam/factimp.dir/a.go +++ b/test/typeparam/factimp.dir/a.go @@ -5,8 +5,8 @@ package a func Fact[T interface { type int, int64, float64 }](n T) T { - if n == T(1) { - return T(1) + if n == 1 { + return 1 } - return n * Fact(n - T(1)) + return n * Fact(n - 1) } diff --git a/test/typeparam/listimp.dir/a.go b/test/typeparam/listimp.dir/a.go index a4118a0e81..0a4634b7be 100644 --- a/test/typeparam/listimp.dir/a.go +++ b/test/typeparam/listimp.dir/a.go @@ -42,11 +42,10 @@ type ListNum[T OrderedNum] struct { const Clip = 5 // clippedLargest returns the largest in the list of OrderNums, but a max of 5. -// TODO(danscales): fix export/import of an untype constant with typeparam type func (l *ListNum[T]) ClippedLargest() T { var max T for p := l; p != nil; p = p.Next { - if p.Val > max && p.Val < T(Clip) { + if p.Val > max && p.Val < Clip { max = p.Val } } -- cgit v1.3-5-g9baa