-
Notifications
You must be signed in to change notification settings - Fork 17.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[dev.typeparams] cmd/compile: allow generic funcs to call other gener…
…ic funcs for stenciling - Handle generic function calling itself or another generic function in stenciling. This is easy - after it is created, just scan an instantiated generic function for function instantiations (that may needed to be stenciled), just like non-generic functions. The types in the function instantiation will already have been set by the stenciling. - Handle OTYPE nodes in subster.node() (allows for generic type conversions). - Eliminated some duplicated work in subster.typ(). - Added new test case fact.go that tests a generic function calling itself, and simple generic type conversions. - Cause an error if a generic function is to be exported (which we don't handle yet). - Fixed some suggested changes in the add.go test case that I missed in the last review. Change-Id: I5d61704254c27962f358d5a3d2e0c62a5099f148 Reviewed-on: https://go-review.googlesource.com/c/go/+/290469 Trust: Dan Scales <danscales@google.com> Trust: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
- Loading branch information
Showing
4 changed files
with
59 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// run -gcflags=-G=3 | ||
|
||
// Copyright 2021 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
|
||
func fact[T interface { type float64 }](n T) T { | ||
if n == T(1) { | ||
return T(1) | ||
} | ||
return n * fact(n - T(1)) | ||
} | ||
|
||
func main() { | ||
got := fact(4.0) | ||
want := 24.0 | ||
if got != want { | ||
panic(fmt.Sprintf("Got %f, want %f", got, want)) | ||
} | ||
|
||
// Re-enable when types2 bug is fixed (can't do T(1) with more than one | ||
// type in the type list). | ||
//got = fact(5) | ||
//want = 120 | ||
//if want != got { | ||
// panic(fmt.Sprintf("Want %d, got %d", want, got)) | ||
//} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters