go/types: added testdata/linalg.go2 to test suite, disabled some code

Context-specific customization of parameterized interface methods of
type parameter bounds is missing; as a result some of linalg.go2 fails.

Change-Id: I3e749ee040d2b3ae8f73ae26680984bc1b4b79ef
This commit is contained in:
Robert Griesemer 2019-12-11 16:44:11 -08:00
parent 2aeeba6836
commit ed8d54fb3a
6 changed files with 34 additions and 10 deletions

View File

@ -7,7 +7,8 @@ TODO
OPEN ISSUES
- contracts slip through in places where only types are permitted
- conversions against parameterized types are not implemented
- parameterized interface methods (of type bounds) need to be customized (subst) for context
DESIGN DECISIONS
- 12/4/2019: do not allow parenthesized generic uninstantiated types (unless instantiated implicitly)

View File

@ -475,6 +475,7 @@ func (check *Checker) selector(x *operand, e *ast.SelectorExpr) {
// methods may not have a fully set up signature yet
if m, _ := obj.(*Func); m != nil {
// check.dump("### found method %s", m)
check.objDecl(m, nil)
// If m has a parameterized receiver type, infer the type parameter
// values from the actual receiver provided and then substitute the
@ -491,6 +492,8 @@ func (check *Checker) selector(x *operand, e *ast.SelectorExpr) {
copy.typ = check.subst(e.Pos(), m.typ, m.tparams, targs)
obj = &copy
}
// TODO(gri) we also need to do substitution for parameterized interface methods
// (this breaks code in testdata/linalg.go2 at the moment)
}
if x.mode == typexpr {

View File

@ -111,6 +111,7 @@ var tests = [][]string{
{"testdata/chans.go2"},
{"testdata/map.go2"},
{"testdata/map2.go2"},
{"testdata/linalg.go2"},
// Go 2 prototype examples
{"examples/contracts.go2"},

View File

@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// TODO(gri) add this file to the test suite once it type-checks.
package linalg
import "math"
@ -66,9 +64,10 @@ contract Complex(T) {
type OrderedAbs(type T OrderedNumeric) T
func (a OrderedAbs(T)) Abs() T {
if a < 0 {
return -a
}
// TODO(gri) implement conversions against parameterized types
// if a < 0 {
// return -a
// }
return a
}
@ -84,9 +83,13 @@ func (a ComplexAbs(T)) Abs() T {
}
func OrderedAbsDifference(type T OrderedNumeric)(a, b T) T {
return T(AbsDifference(OrderedAbs(T)(a), OrderedAbs(T)(b)))
// TODO(gri) fix this
// return T(AbsDifference(OrderedAbs(T)(a), OrderedAbs(T)(b)))
return a
}
func ComplexAbsDifference(type T Complex)(a, b T) T {
return T(AbsDifference(ComplexAbs(T)(a), ComplexAbs(T)(b)))
// TODO(gri) fix this
// return T(AbsDifference(ComplexAbs(T)(a), ComplexAbs(T)(b)))
return a
}

View File

@ -3,3 +3,19 @@
// license that can be found in the LICENSE file.
package p
contract C(T) {
T int
T Abs() T
}
type B(type T) interface {
type int
Abs() T
}
func AbsDifference(type T C)(a, b T) T {
d := a - b
// TODO(gri) make this work
return d /* ERROR cannot use */ .Abs()
}

View File

@ -276,7 +276,7 @@ func writeType(buf *bytes.Buffer, typ Type, qf Qualifier, visited []Type) {
if i > 0 {
buf.WriteString(", ")
}
buf.WriteString(tpar.name)
writeType(buf, tpar.typ, qf, visited)
}
buf.WriteString("){")
i := 0
@ -284,7 +284,7 @@ func writeType(buf *bytes.Buffer, typ Type, qf Qualifier, visited []Type) {
if i > 0 {
buf.WriteString("; ")
}
buf.WriteString(tpar.name)
writeType(buf, tpar.typ, qf, visited)
buf.WriteByte(' ')
writeType(buf, iface, qf, visited)
i++