Skip to content

Commit

Permalink
Fix type inference, inheritance etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
darkdrag00nv2 committed Nov 25, 2023
1 parent a0bc766 commit 71d2a1d
Show file tree
Hide file tree
Showing 17 changed files with 58 additions and 44 deletions.
3 changes: 3 additions & 0 deletions encoding/ccf/decode_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,9 @@ func (d *Decoder) decodeSimpleTypeID() (cadence.Type, error) {
case TypeSignedInteger:
return cadence.TheSignedIntegerType, nil

case TypeFixedSizeUnsignedInteger:
return cadence.TheFixedSizeUnsignedIntegerType, nil

case TypeFixedPoint:
return cadence.TheFixedPointType, nil

Expand Down
4 changes: 2 additions & 2 deletions runtime/interpreter/interpreter_expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ func (interpreter *Interpreter) NewIntegerValueFromBigInt(value *big.Int, intege
// NOTE: cases meter manually and call the unmetered constructors to avoid allocating closures

switch integerSubType {
case sema.IntType, sema.IntegerType, sema.SignedIntegerType, sema.FixedSizeUnsignedIntegerType:
case sema.IntType, sema.IntegerType, sema.SignedIntegerType:
// BigInt value is already metered at parser.
return NewUnmeteredIntValueFromBigInt(value)
case sema.UIntType:
Expand Down Expand Up @@ -692,7 +692,7 @@ func (interpreter *Interpreter) NewIntegerValueFromBigInt(value *big.Int, intege
case sema.UInt128Type:
// BigInt value is already metered at parser.
return NewUnmeteredUInt128ValueFromBigInt(value)
case sema.UInt256Type:
case sema.UInt256Type, sema.FixedSizeUnsignedIntegerType:
// BigInt value is already metered at parser.
return NewUnmeteredUInt256ValueFromBigInt(value)

Expand Down
2 changes: 1 addition & 1 deletion runtime/literal.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func convertIntValue(
return interpreter.ConvertUInt64(memoryGauge, intValue, interpreter.EmptyLocationRange), nil
case sema.UInt128Type:
return interpreter.ConvertUInt128(memoryGauge, intValue, interpreter.EmptyLocationRange), nil
case sema.UInt256Type:
case sema.UInt256Type, sema.FixedSizeUnsignedIntegerType:
return interpreter.ConvertUInt256(memoryGauge, intValue, interpreter.EmptyLocationRange), nil

case sema.Word8Type:
Expand Down
19 changes: 14 additions & 5 deletions runtime/sema/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -5768,15 +5768,13 @@ func checkSubTypeWithoutEquality(subType Type, superType Type) bool {

case IntegerType:
switch subType {
case IntegerType, SignedIntegerType,
UIntType,
UInt8Type, UInt16Type, UInt32Type, UInt64Type, UInt128Type, UInt256Type,
Word8Type, Word16Type, Word32Type, Word64Type, Word128Type, Word256Type:
case IntegerType, SignedIntegerType, FixedSizeUnsignedIntegerType,
UIntType:

return true

default:
return IsSubType(subType, SignedIntegerType)
return IsSubType(subType, SignedIntegerType) || IsSubType(subType, FixedSizeUnsignedIntegerType)
}

case SignedIntegerType:
Expand All @@ -5791,6 +5789,17 @@ func checkSubTypeWithoutEquality(subType Type, superType Type) bool {
return false
}

case FixedSizeUnsignedIntegerType:
switch subType {
case UInt8Type, UInt16Type, UInt32Type, UInt64Type, UInt128Type, UInt256Type,
Word8Type, Word16Type, Word32Type, Word64Type, Word128Type, Word256Type:

return true

default:
return false
}

case FixedPointType:
switch subType {
case FixedPointType, SignedFixedPointType,
Expand Down
24 changes: 9 additions & 15 deletions runtime/sema/type_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,21 +244,6 @@ var (
Or(Int128TypeTag).
Or(Int256TypeTag)

UnsignedIntegerTypeTag = newTypeTagFromLowerMask(unsignedIntegerTypeMask).
Or(UIntTypeTag).
Or(UInt8TypeTag).
Or(UInt16TypeTag).
Or(UInt32TypeTag).
Or(UInt64TypeTag).
Or(UInt128TypeTag).
Or(UInt256TypeTag).
Or(Word8TypeTag).
Or(Word16TypeTag).
Or(Word32TypeTag).
Or(Word64TypeTag).
Or(Word128TypeTag).
Or(Word256TypeTag)

FixedSizeUnsignedIntegerTypeTag = newTypeTagFromUpperMask(fixedSizeUnsignedIntegerTypeMask).
Or(UInt8TypeTag).
Or(UInt16TypeTag).
Expand All @@ -273,6 +258,10 @@ var (
Or(Word128TypeTag).
Or(Word256TypeTag)

UnsignedIntegerTypeTag = newTypeTagFromLowerMask(unsignedIntegerTypeMask).
Or(UIntTypeTag).
Or(FixedSizeUnsignedIntegerTypeTag)

IntegerTypeTag = newTypeTagFromLowerMask(integerTypeMask).
Or(SignedIntegerTypeTag).
Or(UnsignedIntegerTypeTag)
Expand Down Expand Up @@ -518,6 +507,8 @@ func findCommonSuperType(joinedTypeTag TypeTag, types ...Type) Type {
return InvalidType
case joinedTypeTag.BelongsTo(SignedIntegerTypeTag):
return SignedIntegerType
case joinedTypeTag.BelongsTo(FixedSizeUnsignedIntegerTypeTag):
return FixedSizeUnsignedIntegerType
case joinedTypeTag.BelongsTo(IntegerTypeTag):
return IntegerType
case joinedTypeTag.BelongsTo(SignedFixedPointTypeTag):
Expand Down Expand Up @@ -717,6 +708,9 @@ func findSuperTypeFromUpperMask(joinedTypeTag TypeTag, types []Type) Type {
case accountCapabilityControllerTypeMask:
return AccountCapabilityControllerType

case fixedSizeUnsignedIntegerTypeMask:
return FixedSizeUnsignedIntegerType

default:
return nil
}
Expand Down
9 changes: 9 additions & 0 deletions runtime/sema/type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -892,9 +892,18 @@ func TestCommonSuperType(t *testing.T) {
types: []Type{
UInt8Type,
UInt128Type,
UIntType,
},
expectedSuperType: IntegerType,
},
{
name: "fixed size unsigned integers",
types: []Type{
UInt8Type,
UInt128Type,
},
expectedSuperType: FixedSizeUnsignedIntegerType,
},
{
name: "heterogeneous simple types",
types: []Type{
Expand Down
9 changes: 3 additions & 6 deletions runtime/stdlib/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,7 @@ func NewRevertibleRandomFunction(generator RandomGenerator) StandardLibraryValue
func(invocation interpreter.Invocation) interpreter.Value {
inter := invocation.Interpreter

// modulo is optional.
if len(invocation.Arguments) > 0 {
// TODO: Get modulo from the arguments.
}
// TODO: Check if invocation has an argument and implement modulo operation.

returnIntegerType := invocation.TypeParameterTypes.Oldest().Value
returnIntegerStaticType := interpreter.ConvertSemaToStaticType(inter, returnIntegerType)
Expand All @@ -107,7 +104,7 @@ func NewRevertibleRandomFunction(generator RandomGenerator) StandardLibraryValue
return interpreter.NewUInt8Value(
inter,
func() uint8 {
return uint8(getRandomBytes(generator, 1)[0])
return getRandomBytes(generator, 1)[0]
},
)
case interpreter.PrimitiveStaticTypeUInt16:
Expand Down Expand Up @@ -153,7 +150,7 @@ func NewRevertibleRandomFunction(generator RandomGenerator) StandardLibraryValue
return interpreter.NewWord8Value(
inter,
func() uint8 {
return uint8(getRandomBytes(generator, 1)[0])
return getRandomBytes(generator, 1)[0]
},
)
case interpreter.PrimitiveStaticTypeWord16:
Expand Down
2 changes: 1 addition & 1 deletion runtime/tests/checker/builtinfunctions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func TestCheckFromBigEndianBytes(t *testing.T) {
for _, ty := range sema.AllNumberTypes {
switch ty {
case sema.NumberType, sema.SignedNumberType,
sema.IntegerType, sema.SignedIntegerType,
sema.IntegerType, sema.SignedIntegerType, sema.FixedSizeUnsignedIntegerType,
sema.FixedPointType, sema.SignedFixedPointType:
continue

Expand Down
9 changes: 5 additions & 4 deletions runtime/tests/checker/integer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ func TestCheckInvalidIntegerConversionFunctionWithoutArgs(t *testing.T) {
for _, ty := range allIntegerTypesAndAddressType {
// Only test leaf types
switch ty {
case sema.IntegerType, sema.SignedIntegerType:
case sema.IntegerType, sema.SignedIntegerType, sema.FixedSizeUnsignedIntegerType:
continue
}

Expand Down Expand Up @@ -488,7 +488,7 @@ func TestCheckFixedPointToIntegerConversion(t *testing.T) {
for _, ty := range sema.AllIntegerTypes {
// Only test leaf types
switch ty {
case sema.IntegerType, sema.SignedIntegerType:
case sema.IntegerType, sema.SignedIntegerType, sema.FixedSizeUnsignedIntegerType:
continue
}

Expand Down Expand Up @@ -531,7 +531,8 @@ func TestCheckIntegerLiteralArguments(t *testing.T) {

switch ty {
case sema.IntegerType,
sema.SignedIntegerType:
sema.SignedIntegerType,
sema.FixedSizeUnsignedIntegerType:
errs := RequireCheckerErrors(t, err, 1)
assert.IsType(t, &sema.InvalidBinaryOperandsError{}, errs[0])
default:
Expand Down Expand Up @@ -567,7 +568,7 @@ func TestCheckIntegerMinMax(t *testing.T) {
for _, ty := range sema.AllIntegerTypes {
// Only test leaf types
switch ty {
case sema.IntegerType, sema.SignedIntegerType:
case sema.IntegerType, sema.SignedIntegerType, sema.FixedSizeUnsignedIntegerType:
continue
}

Expand Down
1 change: 1 addition & 0 deletions runtime/tests/checker/operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ func TestCheckNumericSuperTypeBinaryOperations(t *testing.T) {
sema.SignedNumberType,
sema.IntegerType,
sema.SignedIntegerType,
sema.FixedSizeUnsignedIntegerType,
sema.FixedPointType,
sema.SignedFixedPointType,
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/tests/interpreter/arithmetic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func init() {

for _, integerType := range sema.AllIntegerTypes {
switch integerType {
case sema.IntegerType, sema.SignedIntegerType:
case sema.IntegerType, sema.SignedIntegerType, sema.FixedSizeUnsignedIntegerType:
continue
}

Expand Down
2 changes: 1 addition & 1 deletion runtime/tests/interpreter/bitwise_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func init() {

for _, integerType := range sema.AllIntegerTypes {
switch integerType {
case sema.IntegerType, sema.SignedIntegerType:
case sema.IntegerType, sema.SignedIntegerType, sema.FixedSizeUnsignedIntegerType:
continue
}

Expand Down
4 changes: 2 additions & 2 deletions runtime/tests/interpreter/builtinfunctions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ func TestInterpretToBigEndianBytes(t *testing.T) {
for _, integerType := range sema.AllNumberTypes {
switch integerType {
case sema.NumberType, sema.SignedNumberType,
sema.IntegerType, sema.SignedIntegerType,
sema.IntegerType, sema.SignedIntegerType, sema.FixedSizeUnsignedIntegerType,
sema.FixedPointType, sema.SignedFixedPointType:
continue
}
Expand Down Expand Up @@ -839,7 +839,7 @@ func TestInterpretFromBigEndianBytes(t *testing.T) {
for _, integerType := range sema.AllNumberTypes {
switch integerType {
case sema.NumberType, sema.SignedNumberType,
sema.IntegerType, sema.SignedIntegerType,
sema.IntegerType, sema.SignedIntegerType, sema.FixedSizeUnsignedIntegerType,
sema.FixedPointType, sema.SignedFixedPointType:
continue
}
Expand Down
6 changes: 3 additions & 3 deletions runtime/tests/interpreter/integers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func init() {
for _, integerType := range sema.AllIntegerTypes {
// Only test leaf types
switch integerType {
case sema.IntegerType, sema.SignedIntegerType:
case sema.IntegerType, sema.SignedIntegerType, sema.FixedSizeUnsignedIntegerType:
continue
}

Expand Down Expand Up @@ -705,7 +705,7 @@ func TestInterpretIntegerConversion(t *testing.T) {
for _, ty := range sema.AllIntegerTypes {
// Only test leaf types
switch ty {
case sema.IntegerType, sema.SignedIntegerType:
case sema.IntegerType, sema.SignedIntegerType, sema.FixedSizeUnsignedIntegerType:
continue
}

Expand Down Expand Up @@ -891,7 +891,7 @@ func TestInterpretIntegerMinMax(t *testing.T) {
for _, ty := range sema.AllIntegerTypes {
// Only test leaf types
switch ty {
case sema.IntegerType, sema.SignedIntegerType:
case sema.IntegerType, sema.SignedIntegerType, sema.FixedSizeUnsignedIntegerType:
continue
}

Expand Down
2 changes: 1 addition & 1 deletion runtime/tests/interpreter/interpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7469,7 +7469,7 @@ func TestInterpretEmitEventParameterTypes(t *testing.T) {
for _, integerType := range sema.AllIntegerTypes {

switch integerType {
case sema.IntegerType, sema.SignedIntegerType:
case sema.IntegerType, sema.SignedIntegerType, sema.FixedSizeUnsignedIntegerType:
continue
}

Expand Down
2 changes: 1 addition & 1 deletion types.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ func NewUnsignedIntegerType() FixedSizeUnsignedIntegerType {
func (FixedSizeUnsignedIntegerType) isType() {}

func (FixedSizeUnsignedIntegerType) ID() string {
return "FixedSizedUnsignedInteger"
return "FixedSizeUnsignedInteger"
}

func (t FixedSizeUnsignedIntegerType) Equal(other Type) bool {
Expand Down
2 changes: 1 addition & 1 deletion values_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ func TestNumberValue_ToBigEndianBytes(t *testing.T) {
for _, integerType := range sema.AllNumberTypes {
switch integerType {
case sema.NumberType, sema.SignedNumberType,
sema.IntegerType, sema.SignedIntegerType,
sema.IntegerType, sema.SignedIntegerType, sema.FixedSizeUnsignedIntegerType,
sema.FixedPointType, sema.SignedFixedPointType:
continue
}
Expand Down

0 comments on commit 71d2a1d

Please sign in to comment.