mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
[dev.typeparams] cmd/compile: make type conversions by type parameters work
When doing a type conversion using a type param, delay the transformation to OCONV/OCONVNOP until stenciling, since the nodes created depend on the actual type. Re-enable the fact.go test. Change-Id: I3d5861aab3dd0e781d767f67435afaf951dfe451 Reviewed-on: https://go-review.googlesource.com/c/go/+/290752 Trust: Dan Scales <danscales@google.com> Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Dan Scales <danscales@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
parent
12e15d430d
commit
fdf3496fcc
3 changed files with 33 additions and 27 deletions
|
|
@ -84,6 +84,11 @@ func Call(pos src.XPos, typ *types.Type, fun ir.Node, args []ir.Node, dots bool)
|
||||||
if fun.Op() == ir.OTYPE {
|
if fun.Op() == ir.OTYPE {
|
||||||
// Actually a type conversion, not a function call.
|
// Actually a type conversion, not a function call.
|
||||||
n := ir.NewCallExpr(pos, ir.OCALL, fun, args)
|
n := ir.NewCallExpr(pos, ir.OCALL, fun, args)
|
||||||
|
if fun.Type().Kind() == types.TTYPEPARAM {
|
||||||
|
// For type params, don't typecheck until we actually know
|
||||||
|
// the type.
|
||||||
|
return typed(typ, n)
|
||||||
|
}
|
||||||
return typecheck.Expr(n)
|
return typecheck.Expr(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -174,30 +174,36 @@ func (subst *subster) node(n ir.Node) ir.Node {
|
||||||
}
|
}
|
||||||
ir.EditChildren(m, edit)
|
ir.EditChildren(m, edit)
|
||||||
|
|
||||||
|
if x.Op() == ir.OXDOT {
|
||||||
// A method value/call via a type param will have been left as an
|
// A method value/call via a type param will have been left as an
|
||||||
// OXDOT. When we see this during stenciling, finish the
|
// OXDOT. When we see this during stenciling, finish the
|
||||||
// typechecking, now that we have the instantiated receiver type.
|
// typechecking, now that we have the instantiated receiver type.
|
||||||
// We need to do this now, since the access/selection to the
|
// We need to do this now, since the access/selection to the
|
||||||
// method for the real type is very different from the selection
|
// method for the real type is very different from the selection
|
||||||
// for the type param.
|
// for the type param.
|
||||||
if x.Op() == ir.OXDOT {
|
|
||||||
// Will transform to an OCALLPART
|
|
||||||
m.SetTypecheck(0)
|
m.SetTypecheck(0)
|
||||||
|
// m will transform to an OCALLPART
|
||||||
typecheck.Expr(m)
|
typecheck.Expr(m)
|
||||||
}
|
}
|
||||||
if x.Op() == ir.OCALL {
|
if x.Op() == ir.OCALL {
|
||||||
call := m.(*ir.CallExpr)
|
call := m.(*ir.CallExpr)
|
||||||
if call.X.Op() != ir.OCALLPART {
|
if call.X.Op() == ir.OTYPE {
|
||||||
base.FatalfAt(call.Pos(), "Expecting OXDOT with CALL")
|
// Do typechecking on a conversion, now that we
|
||||||
}
|
// know the type argument.
|
||||||
|
m.SetTypecheck(0)
|
||||||
|
m = typecheck.Expr(m)
|
||||||
|
} else if call.X.Op() == ir.OCALLPART {
|
||||||
// Redo the typechecking, now that we know the method
|
// Redo the typechecking, now that we know the method
|
||||||
// value is being called
|
// value is being called.
|
||||||
call.X.(*ir.SelectorExpr).SetOp(ir.OXDOT)
|
call.X.(*ir.SelectorExpr).SetOp(ir.OXDOT)
|
||||||
call.X.SetTypecheck(0)
|
call.X.SetTypecheck(0)
|
||||||
call.X.SetType(nil)
|
call.X.SetType(nil)
|
||||||
typecheck.Callee(call.X)
|
typecheck.Callee(call.X)
|
||||||
m.SetTypecheck(0)
|
m.SetTypecheck(0)
|
||||||
typecheck.Call(m.(*ir.CallExpr))
|
typecheck.Call(m.(*ir.CallExpr))
|
||||||
|
} else {
|
||||||
|
base.FatalfAt(call.Pos(), "Expecting OCALLPART or OTYPE with CALL")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if x.Op() == ir.OCLOSURE {
|
if x.Op() == ir.OCLOSURE {
|
||||||
|
|
|
||||||
|
|
@ -8,20 +8,15 @@ package main
|
||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
// TODO Stenciling doesn't do the right thing for T(1) at the moment.
|
|
||||||
|
|
||||||
func fact[T interface { type int, int64, float64 }](n T) T {
|
func fact[T interface { type int, int64, float64 }](n T) T {
|
||||||
// TODO remove this return in favor of the correct computation below
|
if n == T(1) {
|
||||||
return n
|
return T(1)
|
||||||
// if n == T(1) {
|
}
|
||||||
// return T(1)
|
return n * fact(n - T(1))
|
||||||
// }
|
|
||||||
// return n * fact(n - T(1))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// TODO change this to 120 once we can compile the function body above
|
const want = 120
|
||||||
const want = 5 // 120
|
|
||||||
|
|
||||||
if got := fact(5); got != want {
|
if got := fact(5); got != want {
|
||||||
panic(fmt.Sprintf("got %d, want %d", got, want))
|
panic(fmt.Sprintf("got %d, want %d", got, want))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue