Skip to content

Commit 8d909c4

Browse files
committed
unsafe type data field
1 parent affd77d commit 8d909c4

File tree

4 files changed

+70
-30
lines changed

4 files changed

+70
-30
lines changed

internal/checker/checker.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -22948,14 +22948,14 @@ func isUnaryTupleTypeNode(node *ast.Node) bool {
2294822948
return ast.IsTupleTypeNode(node) && len(node.AsTupleTypeNode().Elements.Nodes) == 1
2294922949
}
2295022950

22951-
func (c *Checker) newType(flags TypeFlags, objectFlags ObjectFlags, data TypeData) *Type {
22951+
func (c *Checker) newType(flags TypeFlags, objectFlags ObjectFlags, data typeData) *Type {
2295222952
c.TypeCount++
2295322953
t := data.AsType()
2295422954
t.flags = flags
2295522955
t.objectFlags = objectFlags &^ (ObjectFlagsCouldContainTypeVariablesComputed | ObjectFlagsCouldContainTypeVariables | ObjectFlagsMembersResolved)
2295622956
t.id = TypeId(c.TypeCount)
2295722957
t.checker = c
22958-
t.data = data
22958+
t.setData(data)
2295922959
return t
2296022960
}
2296122961

@@ -23006,7 +23006,7 @@ func (c *Checker) newUniqueESSymbolType(symbol *ast.Symbol, name string) *Type {
2300623006
}
2300723007

2300823008
func (c *Checker) newObjectType(objectFlags ObjectFlags, symbol *ast.Symbol) *Type {
23009-
var data TypeData
23009+
var data typeData
2301023010
switch {
2301123011
case objectFlags&ObjectFlagsClassOrInterface != 0:
2301223012
data = &InterfaceType{}

internal/checker/type_safe.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//go:build !tsunsafe
2+
3+
package checker
4+
5+
type typeDataField struct {
6+
_data typeData
7+
}
8+
9+
func (t *typeDataField) data() typeData {
10+
return t._data
11+
}
12+
13+
func (t *typeDataField) setData(data typeData) {
14+
t._data = data
15+
}

internal/checker/type_unsafe.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//go:build tsunsafe
2+
3+
package checker
4+
5+
import "unsafe"
6+
7+
type typeDataField struct {
8+
_dataType unsafe.Pointer
9+
}
10+
11+
type interfaceValue struct {
12+
typ unsafe.Pointer
13+
value unsafe.Pointer
14+
}
15+
16+
func (t *typeDataField) data() typeData {
17+
var data typeData
18+
(*interfaceValue)(unsafe.Pointer(&data)).typ = t._dataType
19+
(*interfaceValue)(unsafe.Pointer(&data)).value = unsafe.Pointer(t)
20+
return data
21+
}
22+
23+
func (t *typeDataField) setData(data typeData) {
24+
t._dataType = (*interfaceValue)(unsafe.Pointer(&data)).typ
25+
}

internal/checker/types.go

+27-27
Original file line numberDiff line numberDiff line change
@@ -528,41 +528,41 @@ type Type struct {
528528
symbol *ast.Symbol
529529
alias *TypeAlias
530530
checker *Checker
531-
data TypeData // Type specific data
531+
typeDataField
532532
}
533533

534534
// Casts for concrete struct types
535535

536-
func (t *Type) AsIntrinsicType() *IntrinsicType { return t.data.(*IntrinsicType) }
537-
func (t *Type) AsLiteralType() *LiteralType { return t.data.(*LiteralType) }
538-
func (t *Type) AsUniqueESSymbolType() *UniqueESSymbolType { return t.data.(*UniqueESSymbolType) }
539-
func (t *Type) AsTupleType() *TupleType { return t.data.(*TupleType) }
540-
func (t *Type) AsSingleSignatureType() *SingleSignatureType { return t.data.(*SingleSignatureType) }
536+
func (t *Type) AsIntrinsicType() *IntrinsicType { return t.data().(*IntrinsicType) }
537+
func (t *Type) AsLiteralType() *LiteralType { return t.data().(*LiteralType) }
538+
func (t *Type) AsUniqueESSymbolType() *UniqueESSymbolType { return t.data().(*UniqueESSymbolType) }
539+
func (t *Type) AsTupleType() *TupleType { return t.data().(*TupleType) }
540+
func (t *Type) AsSingleSignatureType() *SingleSignatureType { return t.data().(*SingleSignatureType) }
541541
func (t *Type) AsInstantiationExpressionType() *InstantiationExpressionType {
542-
return t.data.(*InstantiationExpressionType)
543-
}
544-
func (t *Type) AsMappedType() *MappedType { return t.data.(*MappedType) }
545-
func (t *Type) AsReverseMappedType() *ReverseMappedType { return t.data.(*ReverseMappedType) }
546-
func (t *Type) AsEvolvingArrayType() *EvolvingArrayType { return t.data.(*EvolvingArrayType) }
547-
func (t *Type) AsTypeParameter() *TypeParameter { return t.data.(*TypeParameter) }
548-
func (t *Type) AsUnionType() *UnionType { return t.data.(*UnionType) }
549-
func (t *Type) AsIntersectionType() *IntersectionType { return t.data.(*IntersectionType) }
550-
func (t *Type) AsIndexType() *IndexType { return t.data.(*IndexType) }
551-
func (t *Type) AsIndexedAccessType() *IndexedAccessType { return t.data.(*IndexedAccessType) }
552-
func (t *Type) AsTemplateLiteralType() *TemplateLiteralType { return t.data.(*TemplateLiteralType) }
553-
func (t *Type) AsStringMappingType() *StringMappingType { return t.data.(*StringMappingType) }
554-
func (t *Type) AsSubstitutionType() *SubstitutionType { return t.data.(*SubstitutionType) }
555-
func (t *Type) AsConditionalType() *ConditionalType { return t.data.(*ConditionalType) }
542+
return t.data().(*InstantiationExpressionType)
543+
}
544+
func (t *Type) AsMappedType() *MappedType { return t.data().(*MappedType) }
545+
func (t *Type) AsReverseMappedType() *ReverseMappedType { return t.data().(*ReverseMappedType) }
546+
func (t *Type) AsEvolvingArrayType() *EvolvingArrayType { return t.data().(*EvolvingArrayType) }
547+
func (t *Type) AsTypeParameter() *TypeParameter { return t.data().(*TypeParameter) }
548+
func (t *Type) AsUnionType() *UnionType { return t.data().(*UnionType) }
549+
func (t *Type) AsIntersectionType() *IntersectionType { return t.data().(*IntersectionType) }
550+
func (t *Type) AsIndexType() *IndexType { return t.data().(*IndexType) }
551+
func (t *Type) AsIndexedAccessType() *IndexedAccessType { return t.data().(*IndexedAccessType) }
552+
func (t *Type) AsTemplateLiteralType() *TemplateLiteralType { return t.data().(*TemplateLiteralType) }
553+
func (t *Type) AsStringMappingType() *StringMappingType { return t.data().(*StringMappingType) }
554+
func (t *Type) AsSubstitutionType() *SubstitutionType { return t.data().(*SubstitutionType) }
555+
func (t *Type) AsConditionalType() *ConditionalType { return t.data().(*ConditionalType) }
556556

557557
// Casts for embedded struct types
558558

559-
func (t *Type) AsConstrainedType() *ConstrainedType { return t.data.AsConstrainedType() }
560-
func (t *Type) AsStructuredType() *StructuredType { return t.data.AsStructuredType() }
561-
func (t *Type) AsObjectType() *ObjectType { return t.data.AsObjectType() }
562-
func (t *Type) AsTypeReference() *TypeReference { return t.data.AsTypeReference() }
563-
func (t *Type) AsInterfaceType() *InterfaceType { return t.data.AsInterfaceType() }
559+
func (t *Type) AsConstrainedType() *ConstrainedType { return t.data().AsConstrainedType() }
560+
func (t *Type) AsStructuredType() *StructuredType { return t.data().AsStructuredType() }
561+
func (t *Type) AsObjectType() *ObjectType { return t.data().AsObjectType() }
562+
func (t *Type) AsTypeReference() *TypeReference { return t.data().AsTypeReference() }
563+
func (t *Type) AsInterfaceType() *InterfaceType { return t.data().AsInterfaceType() }
564564
func (t *Type) AsUnionOrIntersectionType() *UnionOrIntersectionType {
565-
return t.data.AsUnionOrIntersectionType()
565+
return t.data().AsUnionOrIntersectionType()
566566
}
567567

568568
func (t *Type) Distributed() []*Type {
@@ -623,7 +623,7 @@ func (t *Type) TargetTupleType() *TupleType {
623623

624624
// TypeData
625625

626-
type TypeData interface {
626+
type typeData interface {
627627
AsType() *Type
628628
AsConstrainedType() *ConstrainedType
629629
AsStructuredType() *StructuredType

0 commit comments

Comments
 (0)