From c1296af151f5682f6e0cd88cd0372aca5a464a97 Mon Sep 17 00:00:00 2001 From: Dan Scales Date: Mon, 17 Jan 2022 13:24:06 -0800 Subject: cmd/compile: add early a CONVIFACE normally created in the order phase Most CONVIFACEs are created in the transform phase (or old typechecker, in -G=0 mode). But if the main result of a multi-value assignment (map, channel, or dot-type) must be converted to an interface during the assignment, that CONVIFACE is not created until (*orderState).as2ok in the order phase (because the AS2* ops and their sub-ops are so tightly intertwined). But we need to create the CONVIFACE during the stenciling/transform phase to enable dictionary lookups. So, in transformAssign(), if we are doing a special multi-value assignment involving a type-param-derived type, assign the results first to temps, so that we can manifest the CONVIFACE during the transform in assigning the first temp to lhs[0]. Added a test for both AS2RECV (channel receives) and AS2MAPR (maps). I don't think we can have a type assertion on a type-param-derived type. Fixes #50642 Change-Id: I4d079fc46c93d8494d7db4ea8234d91522edb02a Reviewed-on: https://go-review.googlesource.com/c/go/+/379054 Reviewed-by: Matthew Dempsky Trust: Dan Scales Run-TryBot: Dan Scales TryBot-Result: Gopher Robot --- src/cmd/compile/internal/noder/transform.go | 31 +++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'src/cmd/compile/internal/noder/transform.go') diff --git a/src/cmd/compile/internal/noder/transform.go b/src/cmd/compile/internal/noder/transform.go index 6f49106f5e..5f1f41163b 100644 --- a/src/cmd/compile/internal/noder/transform.go +++ b/src/cmd/compile/internal/noder/transform.go @@ -356,6 +356,37 @@ assignOK: } checkLHS(0, r.Type()) checkLHS(1, types.UntypedBool) + t := lhs[0].Type() + if t != nil && rhs[0].Type().HasShape() && t.IsInterface() && !types.IdenticalStrict(t, rhs[0].Type()) { + // This is a multi-value assignment (map, channel, or dot-type) + // where the main result is converted to an interface during the + // assignment. Normally, the needed CONVIFACE is not created + // until (*orderState).as2ok(), because the AS2* ops and their + // sub-ops are so tightly intertwined. But we need to create the + // CONVIFACE now to enable dictionary lookups. So, assign the + // results first to temps, so that we can manifest the CONVIFACE + // in assigning the first temp to lhs[0]. If we added the + // CONVIFACE into rhs[0] directly, we would break a lot of later + // code that depends on the tight coupling between the AS2* ops + // and their sub-ops. (Issue #50642). + v := typecheck.Temp(rhs[0].Type()) + ok := typecheck.Temp(types.Types[types.TBOOL]) + as := ir.NewAssignListStmt(base.Pos, stmt.Op(), []ir.Node{v, ok}, []ir.Node{r}) + as.Def = true + as.PtrInit().Append(ir.NewDecl(base.Pos, ir.ODCL, v)) + as.PtrInit().Append(ir.NewDecl(base.Pos, ir.ODCL, ok)) + as.SetTypecheck(1) + // Change stmt to be a normal assignment of the temps to the final + // left-hand-sides. We re-create the original multi-value assignment + // so that it assigns to the temps and add it as an init of stmt. + // + // TODO: fix the order of evaluation, so that the lval of lhs[0] + // is evaluated before rhs[0] (similar to problem in #50672). + stmt.SetOp(ir.OAS2) + stmt.PtrInit().Append(as) + // assignconvfn inserts the CONVIFACE. + stmt.Rhs = []ir.Node{assignconvfn(v, t), ok} + } return } -- cgit v1.3-5-g9baa