mirror of https://github.com/golang/go.git
go/types: incorporate last-minute changes added for GopherCon
Change-Id: I8e1ff18f073ca47ef1a5d8ea43c8b6a3bc4d27b8
This commit is contained in:
parent
d4272d0350
commit
c4a20af9f1
|
|
@ -1,34 +1,48 @@
|
|||
This version of go/types (and related go/*) libraries contains changes
|
||||
to type-check generic code as outlined in the latest contracts proposal,
|
||||
as presented by Ian Taylor at GopherCon 2019.
|
||||
This code contains changes to go/types and the go/* support libraries
|
||||
to type-check generic code as outlined in the latest contracts proposal
|
||||
and presented by Ian Lance Taylor at GopherCon 2019 in San Diego.
|
||||
|
||||
CAUTUION: EARLY PROTOTYPE. A LOT IS STILL MISSING. THERE ARE BUGS.
|
||||
CAUTION: EARLY PROTOTYPE. A LOT IS STILL MISSING. THERE ARE MANY BUGS.
|
||||
|
||||
The code is not tested enough, many parts need a rewrite/cleanup
|
||||
and no code review has happened. Read and use at your own risk.
|
||||
Read and use the code at your own risk.
|
||||
|
||||
That said, the go/parser and go/ast changes are working and pass tests
|
||||
including all the larger examples in the latest contracts design doc.
|
||||
Look for the *.go2 files in go/parser/testdata.
|
||||
|
||||
gofmt does not work with parameterized code yet.
|
||||
|
||||
The type-checker is starting to work but has still many problems.
|
||||
I will update this CL from time to time as progress happens.
|
||||
|
||||
Specifically, the following pieces (and more) are missing from type-
|
||||
checking or lead to unexpected behavior:
|
||||
|
||||
- Importing of packages with type parameters or contracts.
|
||||
- Type-checking of contracts and type parameter lists with contracts.
|
||||
- Alias type names with type parameters.
|
||||
- gofmt (and any other tools) don't work for parameterized code.
|
||||
For instance, because contracts are not yet type-checked, maps where
|
||||
the key type is a type parameter will lead to an error because the
|
||||
type checker doesn't know whether the key type is comparable or not.
|
||||
- Parameterized alias types.
|
||||
- Type instantiation is not always working as expected, leading to
|
||||
some odd error messages.
|
||||
- Methods with parameterized receiver types have only recently started
|
||||
to work; there are a few issues around them with proper type
|
||||
instantiation.
|
||||
|
||||
The following is "working" (as in "passes simple tests"):
|
||||
The following is "working" (as in: can be type-checked without errors):
|
||||
|
||||
- Parsing of parameterized types and functions.
|
||||
- Parsing of contract declarations.
|
||||
- Declaration and use of parameterized types without contracts.
|
||||
- Declaration and use (calls) of parameterized functions without
|
||||
- Declaration and use of simple parameterized types without contracts.
|
||||
- Declaration and use (calls) of simple parameterized functions without
|
||||
contracts, including type inference from function arguments.
|
||||
|
||||
Error messages, where present, are in usable condition but expect
|
||||
them to be significantly better in a real implementation.
|
||||
them to be significantly better in a more complete implementation.
|
||||
|
||||
To play with this prototype:
|
||||
|
||||
- Cherry-pick this CL on top of tip:
|
||||
- Cherry-pick this CL on top of tip (the cherry-pick was tested with
|
||||
tip at 919594830f17):
|
||||
|
||||
git fetch "https://go.googlesource.com/go" ... && git cherry-pick FETCH_HEAD
|
||||
|
||||
|
|
@ -47,4 +61,4 @@ See also `gotype -h` for more information.
|
|||
|
||||
Note: Currently, files containing parameterized code have extension ".go2".
|
||||
This is solely to distinguish them from regular Go code and to prevent gofmt
|
||||
from touching them. We expect a proper implementation to keep using ".go".
|
||||
from touching them. We expect a proper implementation to keep using ".go".
|
||||
|
|
@ -13,11 +13,13 @@ func Reverse (type T) (list []T) {
|
|||
j := len(list)-1
|
||||
for i < j {
|
||||
list[i], list[j] = list[j], list[i]
|
||||
i++
|
||||
j--
|
||||
}
|
||||
}
|
||||
|
||||
func _() {
|
||||
// Reverse can be called with an explicit type argument
|
||||
// Reverse can be called with an explicit type argument.
|
||||
Reverse(int)(nil)
|
||||
Reverse(string)([]string{"foo", "bar"})
|
||||
Reverse(struct{x, y int})([]struct{x, y int}{{1, 2}, {2, 3}, {3, 4}})
|
||||
|
|
|
|||
|
|
@ -1,11 +1,8 @@
|
|||
// Copyright 2019 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
|
||||
|
||||
type List(type E) []E
|
||||
func Ranger(type T)() (*Receiver(T)) {
|
||||
//return &Receiver(T){} // TODO(gri) make this work
|
||||
return nil
|
||||
}
|
||||
|
||||
var _ List(List(int)) = [](List(int)){}
|
||||
|
||||
//var _ List(List(List(int))) = [](List(List(int))){}
|
||||
type Receiver(type T) struct {}
|
||||
|
|
|
|||
Loading…
Reference in New Issue