// A FuncType node represents a function type.
FuncType struct {
- Func token.Pos // position of "func" keyword (token.NoPos if there is no "func")
- TParams *FieldList // type parameters; or nil
- Params *FieldList // (incoming) parameters; non-nil
- Results *FieldList // (outgoing) results; or nil
+ Func token.Pos // position of "func" keyword (token.NoPos if there is no "func")
+ TypeParams *FieldList // type parameters; or nil
+ Params *FieldList // (incoming) parameters; non-nil
+ Results *FieldList // (outgoing) results; or nil
}
// An InterfaceType node represents an interface type.
// A TypeSpec node represents a type declaration (TypeSpec production).
TypeSpec struct {
- Doc *CommentGroup // associated documentation; or nil
- Name *Ident // type name
- TParams *FieldList // type parameters; or nil
- Assign token.Pos // position of '=', if any
- Type Expr // *Ident, *ParenExpr, *SelectorExpr, *StarExpr, or any of the *XxxTypes
- Comment *CommentGroup // line comments; or nil
+ Doc *CommentGroup // associated documentation; or nil
+ Name *Ident // type name
+ TypeParams *FieldList // type parameters; or nil
+ Assign token.Pos // position of '=', if any
+ Type Expr // *Ident, *ParenExpr, *SelectorExpr, *StarExpr, or any of the *XxxTypes
+ Comment *CommentGroup // line comments; or nil
}
)
Walk(v, n.Fields)
case *FuncType:
- if n.TParams != nil {
- Walk(v, n.TParams)
+ if n.TypeParams != nil {
+ Walk(v, n.TypeParams)
}
if n.Params != nil {
Walk(v, n.Params)
Walk(v, n.Doc)
}
Walk(v, n.Name)
- if n.TParams != nil {
- Walk(v, n.TParams)
+ if n.TypeParams != nil {
+ Walk(v, n.TypeParams)
}
Walk(v, n.Type)
if n.Comment != nil {
results := p.parseResult()
idents = []*ast.Ident{ident}
typ = &ast.FuncType{
- Func: token.NoPos,
- TParams: tparams,
- Params: params,
- Results: results,
+ Func: token.NoPos,
+ TypeParams: tparams,
+ Params: params,
+ Results: results,
}
} else {
// embedded instantiated type
func (p *parser) parseGenericType(spec *ast.TypeSpec, openPos token.Pos, name0 *ast.Ident, closeTok token.Token) {
list := p.parseParameterList(name0, closeTok, p.parseParamDecl, true)
closePos := p.expect(closeTok)
- spec.TParams = &ast.FieldList{Opening: openPos, List: list, Closing: closePos}
+ spec.TypeParams = &ast.FieldList{Opening: openPos, List: list, Closing: closePos}
// Type alias cannot have type parameters. Accept them for robustness but complain.
if p.tok == token.ASSIGN {
p.error(p.pos, "generic type cannot be alias")
Recv: recv,
Name: ident,
Type: &ast.FuncType{
- Func: pos,
- TParams: tparams,
- Params: params,
- Results: results,
+ Func: pos,
+ TypeParams: tparams,
+ Params: params,
+ Results: results,
},
Body: body,
}
// at the identifier in the TypeSpec and ends at the end of the innermost
// containing block.
r.declare(spec, nil, r.topScope, ast.Typ, spec.Name)
- if spec.TParams != nil {
+ if spec.TypeParams != nil {
r.openScope(spec.Pos())
defer r.closeScope()
- r.walkTParams(spec.TParams)
+ r.walkTParams(spec.TypeParams)
}
ast.Walk(r, spec.Type)
}
// Type parameters are walked normally: they can reference each other, and
// can be referenced by normal parameters.
- if n.Type.TParams != nil {
- r.walkTParams(n.Type.TParams)
+ if n.Type.TypeParams != nil {
+ r.walkTParams(n.Type.TypeParams)
// TODO(rFindley): need to address receiver type parameters.
}
}
func (r *resolver) walkFuncType(typ *ast.FuncType) {
- // typ.TParams must be walked separately for FuncDecls.
+ // typ.TypeParams must be walked separately for FuncDecls.
r.resolveList(typ.Params)
r.resolveList(typ.Results)
r.declareList(typ.Params, ast.Var)
}
func (p *printer) signature(sig *ast.FuncType) {
- if sig.TParams != nil {
- p.parameters(sig.TParams, true)
+ if sig.TypeParams != nil {
+ p.parameters(sig.TypeParams, true)
}
if sig.Params != nil {
p.parameters(sig.Params, false)
case *ast.TypeSpec:
p.setComment(s.Doc)
p.expr(s.Name)
- if s.TParams != nil {
- p.parameters(s.TParams, true)
+ if s.TypeParams != nil {
+ p.parameters(s.TypeParams, true)
}
if n == 1 {
p.print(blank)
})
alias := tdecl.Assign.IsValid()
- if alias && tdecl.TParams.NumFields() != 0 {
+ if alias && tdecl.TypeParams.NumFields() != 0 {
// The parser will ensure this but we may still get an invalid AST.
// Complain and continue as regular type definition.
check.error(atPos(tdecl.Assign), 0, "generic type cannot be alias")
named := check.newNamed(obj, nil, nil, nil, nil)
def.setUnderlying(named)
- if tdecl.TParams != nil {
+ if tdecl.TypeParams != nil {
check.openScope(tdecl, "type parameters")
defer check.closeScope()
- named.tparams = check.collectTypeParams(tdecl.TParams)
+ named.tparams = check.collectTypeParams(tdecl.TypeParams)
}
// determine underlying type of named
check.funcType(sig, fdecl.Recv, fdecl.Type)
obj.color_ = saved
- if fdecl.Type.TParams.NumFields() > 0 && fdecl.Body == nil {
+ if fdecl.Type.TypeParams.NumFields() > 0 && fdecl.Body == nil {
check.softErrorf(fdecl.Name, _Todo, "parameterized function is missing function body")
}
// a receiver specification.)
if sig.tparams != nil {
var at positioner = f.Type
- if ftyp, _ := f.Type.(*ast.FuncType); ftyp != nil && ftyp.TParams != nil {
- at = ftyp.TParams
+ if ftyp, _ := f.Type.(*ast.FuncType); ftyp != nil && ftyp.TypeParams != nil {
+ at = ftyp.TypeParams
}
check.errorf(at, _Todo, "methods cannot have type parameters")
}
check.declarePkgObj(name, obj, di)
}
case typeDecl:
- if d.spec.TParams.NumFields() != 0 && !check.allowVersion(pkg, 1, 18) {
- check.softErrorf(d.spec.TParams.List[0], _Todo, "type parameters require go1.18 or later")
+ if d.spec.TypeParams.NumFields() != 0 && !check.allowVersion(pkg, 1, 18) {
+ check.softErrorf(d.spec.TypeParams.List[0], _Todo, "type parameters require go1.18 or later")
}
obj := NewTypeName(d.spec.Name.Pos(), pkg, d.spec.Name.Name, nil)
check.declarePkgObj(d.spec.Name, obj, &declInfo{file: fileScope, tdecl: d.spec})
if name == "main" {
code = _InvalidMainDecl
}
- if d.decl.Type.TParams.NumFields() != 0 {
- check.softErrorf(d.decl.Type.TParams.List[0], code, "func %s must have no type parameters", name)
+ if d.decl.Type.TypeParams.NumFields() != 0 {
+ check.softErrorf(d.decl.Type.TypeParams.List[0], code, "func %s must have no type parameters", name)
hasTParamError = true
}
if t := d.decl.Type; t.Params.NumFields() != 0 || t.Results != nil {
}
check.recordDef(d.decl.Name, obj)
}
- if d.decl.Type.TParams.NumFields() != 0 && !check.allowVersion(pkg, 1, 18) && !hasTParamError {
- check.softErrorf(d.decl.Type.TParams.List[0], _Todo, "type parameters require go1.18 or later")
+ if d.decl.Type.TypeParams.NumFields() != 0 && !check.allowVersion(pkg, 1, 18) && !hasTParamError {
+ check.softErrorf(d.decl.Type.TypeParams.List[0], _Todo, "type parameters require go1.18 or later")
}
info := &declInfo{file: fileScope, fdecl: d.decl}
// Methods are not package-level objects but we still track them in the
}
}
- if ftyp.TParams != nil {
- sig.tparams = check.collectTypeParams(ftyp.TParams)
+ if ftyp.TypeParams != nil {
+ sig.tparams = check.collectTypeParams(ftyp.TypeParams)
// Always type-check method type parameters but complain that they are not allowed.
// (A separate check is needed when type-checking interface method signatures because
// they don't have a receiver specification.)
if recvPar != nil {
- check.errorf(ftyp.TParams, _Todo, "methods cannot have type parameters")
+ check.errorf(ftyp.TypeParams, _Todo, "methods cannot have type parameters")
}
}