Skip to content

Commit fdf3496

Browse files
committed
[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 <[email protected]> Trust: Robert Griesemer <[email protected]> Run-TryBot: Dan Scales <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent 12e15d4 commit fdf3496

File tree

3 files changed

+33
-27
lines changed

3 files changed

+33
-27
lines changed

src/cmd/compile/internal/noder/helpers.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ func Call(pos src.XPos, typ *types.Type, fun ir.Node, args []ir.Node, dots bool)
8484
if fun.Op() == ir.OTYPE {
8585
// Actually a type conversion, not a function call.
8686
n := ir.NewCallExpr(pos, ir.OCALL, fun, args)
87+
if fun.Type().Kind() == types.TTYPEPARAM {
88+
// For type params, don't typecheck until we actually know
89+
// the type.
90+
return typed(typ, n)
91+
}
8792
return typecheck.Expr(n)
8893
}
8994

src/cmd/compile/internal/noder/stencil.go

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -174,30 +174,36 @@ func (subst *subster) node(n ir.Node) ir.Node {
174174
}
175175
ir.EditChildren(m, edit)
176176

177-
// A method value/call via a type param will have been left as an
178-
// OXDOT. When we see this during stenciling, finish the
179-
// typechecking, now that we have the instantiated receiver type.
180-
// We need to do this now, since the access/selection to the
181-
// method for the real type is very different from the selection
182-
// for the type param.
183177
if x.Op() == ir.OXDOT {
184-
// Will transform to an OCALLPART
178+
// A method value/call via a type param will have been left as an
179+
// OXDOT. When we see this during stenciling, finish the
180+
// typechecking, now that we have the instantiated receiver type.
181+
// We need to do this now, since the access/selection to the
182+
// method for the real type is very different from the selection
183+
// for the type param.
185184
m.SetTypecheck(0)
185+
// m will transform to an OCALLPART
186186
typecheck.Expr(m)
187187
}
188188
if x.Op() == ir.OCALL {
189189
call := m.(*ir.CallExpr)
190-
if call.X.Op() != ir.OCALLPART {
191-
base.FatalfAt(call.Pos(), "Expecting OXDOT with CALL")
190+
if call.X.Op() == ir.OTYPE {
191+
// Do typechecking on a conversion, now that we
192+
// know the type argument.
193+
m.SetTypecheck(0)
194+
m = typecheck.Expr(m)
195+
} else if call.X.Op() == ir.OCALLPART {
196+
// Redo the typechecking, now that we know the method
197+
// value is being called.
198+
call.X.(*ir.SelectorExpr).SetOp(ir.OXDOT)
199+
call.X.SetTypecheck(0)
200+
call.X.SetType(nil)
201+
typecheck.Callee(call.X)
202+
m.SetTypecheck(0)
203+
typecheck.Call(m.(*ir.CallExpr))
204+
} else {
205+
base.FatalfAt(call.Pos(), "Expecting OCALLPART or OTYPE with CALL")
192206
}
193-
// Redo the typechecking, now that we know the method
194-
// value is being called
195-
call.X.(*ir.SelectorExpr).SetOp(ir.OXDOT)
196-
call.X.SetTypecheck(0)
197-
call.X.SetType(nil)
198-
typecheck.Callee(call.X)
199-
m.SetTypecheck(0)
200-
typecheck.Call(m.(*ir.CallExpr))
201207
}
202208

203209
if x.Op() == ir.OCLOSURE {

test/typeparam/fact.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,15 @@ package main
88

99
import "fmt"
1010

11-
// TODO Stenciling doesn't do the right thing for T(1) at the moment.
12-
1311
func fact[T interface { type int, int64, float64 }](n T) T {
14-
// TODO remove this return in favor of the correct computation below
15-
return n
16-
// if n == T(1) {
17-
// return T(1)
18-
// }
19-
// return n * fact(n - T(1))
12+
if n == T(1) {
13+
return T(1)
14+
}
15+
return n * fact(n - T(1))
2016
}
2117

2218
func main() {
23-
// TODO change this to 120 once we can compile the function body above
24-
const want = 5 // 120
19+
const want = 120
2520

2621
if got := fact(5); got != want {
2722
panic(fmt.Sprintf("got %d, want %d", got, want))

0 commit comments

Comments
 (0)