typ := r.typ()
pos := r.pos()
typeWord, srcRType := r.convRTTI(pos)
+ dstTypeParam := r.Bool()
x := r.expr()
// TODO(mdempsky): Stop constructing expressions of untyped type.
base.ErrorExit() // harsh, but prevents constructing invalid IR
}
- n := ir.NewConvExpr(pos, ir.OCONV, typ, x)
- n.TypeWord, n.SrcRType = typeWord, srcRType
+ ce := ir.NewConvExpr(pos, ir.OCONV, typ, x)
+ ce.TypeWord, ce.SrcRType = typeWord, srcRType
if implicit {
- n.SetImplicit(true)
+ ce.SetImplicit(true)
}
- return typecheck.Expr(n)
+ n := typecheck.Expr(ce)
+
+ // spec: "If the type is a type parameter, the constant is converted
+ // into a non-constant value of the type parameter."
+ if dstTypeParam && ir.IsConstNode(n) {
+ // Wrap in an OCONVNOP node to ensure result is non-constant.
+ n = Implicit(ir.NewConvExpr(pos, ir.OCONVNOP, n.Type(), n))
+ n.SetTypecheck(1)
+ }
+ return n
}
}
--- /dev/null
+// compile
+
+// Copyright 2022 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 p
+
+func f[Int int, Uint uint]() {
+ _ = uint(Int(-1))
+ _ = uint(Uint(0) - 1)
+}
+
+func g[String string]() {
+ _ = String("")[100]
+}
+
+var _ = f[int, uint]
+var _ = g[string]