Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce 'wither' method for changing type traits #871

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion checker/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,11 @@ func internalIsAssignable(m *mapping, t1, t2 *types.Type) bool {
case types.BoolKind, types.BytesKind, types.DoubleKind, types.IntKind, types.StringKind, types.UintKind,
types.AnyKind, types.DurationKind, types.TimestampKind,
types.StructKind:
return t1.IsAssignableType(t2)
// Test whether t2 is assignable from t1. The order of this check won't usually matter;
// however, there may be cases where type capabilities are expanded beyond what is supported
// in the current common/types package. For example, an interface designation for a group of
// Struct types.
return t2.IsAssignableType(t1)
case types.TypeKind:
return kind2 == types.TypeKind
case types.OpaqueKind, types.ListKind, types.MapKind:
Expand Down
17 changes: 17 additions & 0 deletions common/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,23 @@ func (t *Type) TypeName() string {
return t.runtimeTypeName
}

// WithTraits creates a copy of the current Type and sets the trait mask to the traits parameter.
//
// This method should be used with Opaque types where the type acts like a container, e.g. vector.
func (t *Type) WithTraits(traits int) *Type {
if t == nil {
return nil
}
return &Type{
kind: t.kind,
parameters: t.parameters,
runtimeTypeName: t.runtimeTypeName,
isAssignableType: t.isAssignableType,
isAssignableRuntimeType: t.isAssignableRuntimeType,
traitMask: traits,
}
}

// String returns a human-readable definition of the type name.
func (t *Type) String() string {
if len(t.Parameters()) == 0 {
Expand Down
11 changes: 11 additions & 0 deletions common/types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,17 @@ func TestTypeHasTrait(t *testing.T) {
}
}

func TestTypeWithTraits(t *testing.T) {
vec := NewOpaqueType("vector", NewTypeParamType("T"))
if vec.HasTrait(traits.SizerType) {
t.Error("vec.HasTrait(SizerType) returned true")
}
vec = vec.WithTraits(traits.SizerType | traits.ContainerType | traits.IndexerType)
if !vec.HasTrait(traits.SizerType) {
t.Errorf("vec.HasTrait(SizerType) returned false after WithTraits() call")
}
}

func TestTypeConvertToType(t *testing.T) {
if BoolType.ConvertToType(TypeType) != TypeType {
t.Error("ConvertToType(TypeType) did not produce type value")
Expand Down