Skip to content

Commit 18629ff

Browse files
authored
Merge pull request #2539 from goplus/xgopilot/issue-2538-1768624042
feat(ast,parser,printer): add TupleType for Phase 1 of Tuple Type proposal
2 parents ef2ef13 + ca4a4c3 commit 18629ff

8 files changed

Lines changed: 1031 additions & 6 deletions

File tree

ast/ast.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,20 @@ type (
337337
Dir ChanDir // channel direction
338338
Value Expr // value type
339339
}
340+
341+
// A TupleType node represents a tuple type.
342+
// Tuple types are syntactic sugar for anonymous structs with ordinal field names.
343+
// Examples:
344+
// () ≡ struct{}
345+
// (T) ≡ T (degenerates to the type itself)
346+
// (T0, T1, ..., TN) ≡ struct{ _0 T0; _1 T1; ...; _N TN }
347+
// (name0 T0, name1 T1, ..., nameN TN) ≡ struct{ _0 T0; _1 T1; ...; _N TN }
348+
// Named fields are compile-time aliases only; runtime uses ordinal fields.
349+
TupleType struct {
350+
Lparen token.Pos // position of "("
351+
Fields *FieldList // tuple element types (and optional names)
352+
Rparen token.Pos // position of ")"
353+
}
340354
)
341355

342356
// Pos and End implementations for expression/type nodes.
@@ -414,6 +428,9 @@ func (x *MapType) Pos() token.Pos { return x.Map }
414428
// Pos returns position of first character belonging to the node.
415429
func (x *ChanType) Pos() token.Pos { return x.Begin }
416430

431+
// Pos returns position of first character belonging to the node.
432+
func (x *TupleType) Pos() token.Pos { return x.Lparen }
433+
417434
// End returns position of first character immediately after the node.
418435
func (x *BadExpr) End() token.Pos { return x.To }
419436

@@ -498,6 +515,9 @@ func (x *MapType) End() token.Pos { return x.Value.End() }
498515
// End returns position of first character immediately after the node.
499516
func (x *ChanType) End() token.Pos { return x.Value.End() }
500517

518+
// End returns position of first character immediately after the node.
519+
func (x *TupleType) End() token.Pos { return x.Rparen + 1 }
520+
501521
// exprNode() ensures that only expression/type nodes can be
502522
// assigned to an Expr.
503523
func (*BadExpr) exprNode() {}
@@ -522,6 +542,7 @@ func (*FuncType) exprNode() {}
522542
func (*InterfaceType) exprNode() {}
523543
func (*MapType) exprNode() {}
524544
func (*ChanType) exprNode() {}
545+
func (*TupleType) exprNode() {}
525546

526547
// ----------------------------------------------------------------------------
527548
// Convenience functions for Idents

ast/filter.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ func filterType(typ Expr, f Filter, export bool) bool {
189189
return b1 || b2
190190
case *ChanType:
191191
return filterType(t.Value, f, export)
192+
case *TupleType:
193+
if filterFieldList(t.Fields, f, export) {
194+
// Note: TupleType doesn't have an Incomplete field like StructType
195+
}
196+
return t.Fields != nil && len(t.Fields.List) > 0
192197
}
193198
return false
194199
}

ast/walk.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,11 @@ func Walk(v Visitor, node Node) {
203203
case *ChanType:
204204
Walk(v, n.Value)
205205

206+
case *TupleType:
207+
if n.Fields != nil {
208+
Walk(v, n.Fields)
209+
}
210+
206211
// Statements
207212
case *BadStmt:
208213
// nothing to do

0 commit comments

Comments
 (0)