Skip to content

Commit

Permalink
pass AST type argument range in Instantiate, also fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
darkdrag00nv2 committed Dec 2, 2023
1 parent 7b65121 commit eefb4a1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 36 deletions.
8 changes: 2 additions & 6 deletions runtime/sema/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2244,15 +2244,11 @@ func (checker *Checker) convertInstantiationType(t *ast.InstantiationType) Type
return ty
}

astRange := ast.NewRange(
checker.memoryGauge,
t.TypeArgumentsStartPos,
t.EndPos,
)
return parameterizedType.Instantiate(
checker.memoryGauge,
typeArguments,
t.TypeArguments,
checker.report,
&astRange,
)
}

Expand Down
20 changes: 14 additions & 6 deletions runtime/sema/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,18 +277,19 @@ type LocatedType interface {
type ParameterizedType interface {
Type
TypeParameters() []*TypeParameter
Instantiate(typeArguments []Type, report func(err error), astRange *ast.Range) Type
Instantiate(memoryGauge common.MemoryGauge, typeArguments []Type, astTypeArguments []*ast.TypeAnnotation, report func(err error)) Type
BaseType() Type
TypeArguments() []Type
}

func MustInstantiate(t ParameterizedType, typeArguments ...Type) Type {
return t.Instantiate(
nil, /* memoryGauge */
typeArguments,
nil, /* astTypeArguments */
func(err error) {
panic(errors.NewUnexpectedErrorFromCause(err))
},
nil,
)
}

Expand Down Expand Up @@ -5410,22 +5411,24 @@ func (t *InclusiveRangeType) BaseType() Type {
}

func (t *InclusiveRangeType) Instantiate(
memoryGauge common.MemoryGauge,
typeArguments []Type,
astTypeArguments []*ast.TypeAnnotation,
report func(err error),
astRange *ast.Range,
) Type {
memberType := typeArguments[0]

if astRange == nil {
if astTypeArguments == nil || astTypeArguments[0] == nil {
panic(errors.NewUnreachableError())

Check warning on line 5422 in runtime/sema/type.go

View check run for this annotation

Codecov / codecov/patch

runtime/sema/type.go#L5422

Added line #L5422 was not covered by tests
}
paramAstRange := ast.NewRangeFromPositioned(memoryGauge, astTypeArguments[0])

// memberType must only be a leaf integer type.
for _, ty := range AllNonLeafIntegerTypes {
if memberType == ty {
report(&InvalidTypeArgumentError{
TypeArgumentName: inclusiveRangeTypeParameter.Name,
Range: *astRange,
Range: paramAstRange,
Details: fmt.Sprintf("Creation of InclusiveRange<%s> is disallowed", memberType),
})
}
Expand Down Expand Up @@ -7288,7 +7291,12 @@ func (t *CapabilityType) TypeParameters() []*TypeParameter {
}
}

func (t *CapabilityType) Instantiate(typeArguments []Type, _ func(err error), _ *ast.Range) Type {
func (t *CapabilityType) Instantiate(
memoryGauge common.MemoryGauge,
typeArguments []Type,
_ []*ast.TypeAnnotation,
_ func(err error),
) Type {
borrowType := typeArguments[0]
return &CapabilityType{
BorrowType: borrowType,
Expand Down
22 changes: 22 additions & 0 deletions runtime/tests/checker/range_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,28 @@ func TestCheckInclusiveRangeConstructionInvalid(t *testing.T) {
"let r: InclusiveRange = InclusiveRange(1, 10)",
[]error{&sema.MissingTypeArgumentError{}},
)

runInvalidCase(
t,
"same_supertype_different_subtype_start_end",
`
let a: Integer = UInt8(0)
let b: Integer = Int16(10)
let r = InclusiveRange(a, b)
`,
[]error{&sema.InvalidTypeArgumentError{}},
)
runInvalidCase(
t,
"same_supertype_different_subtype_start_step",
`
let a: Integer = UInt8(0)
let b: Integer = UInt8(10)
let s: Integer = UInt16(2)
let r = InclusiveRange(a, b, step: s)
`,
[]error{&sema.InvalidTypeArgumentError{}},
)
}

func TestInclusiveRangeNonLeafIntegerTypes(t *testing.T) {
Expand Down
24 changes: 0 additions & 24 deletions runtime/tests/interpreter/range_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,28 +586,4 @@ func TestInclusiveRangeConstructionInvalid(t *testing.T) {
"step value cannot be negative for unsigned integer type",
)
}

runInvalidCase(
t,
"same_supertype_different_subtype_start_end",
`
let a: Integer = UInt8(0)
let b: Integer = Int16(10)
let r = InclusiveRange(a, b)
`,
&interpreter.InclusiveRangeConstructionError{},
"start and end are of different types",
)
runInvalidCase(
t,
"same_supertype_different_subtype_start_step",
`
let a: Integer = UInt8(0)
let b: Integer = UInt8(10)
let s: Integer = UInt16(2)
let r = InclusiveRange(a, b, step: s)
`,
&interpreter.InclusiveRangeConstructionError{},
"step must be of the same type as start and end",
)
}

0 comments on commit eefb4a1

Please sign in to comment.