Skip to content

Commit

Permalink
use type argument range in constructor when available
Browse files Browse the repository at this point in the history
  • Loading branch information
darkdrag00nv2 committed Dec 2, 2023
1 parent eefb4a1 commit 733cf47
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
19 changes: 8 additions & 11 deletions runtime/sema/check_invocation_expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,11 @@ func (checker *Checker) checkInvocation(
}
}

invocationRange := ast.NewRangeFromPositioned(
checker.memoryGauge,
invocationExpression,
)

// The invokable type might have special checks for the arguments

if functionType.ArgumentExpressionsCheck != nil && argumentCount > 0 {
Expand All @@ -467,11 +472,6 @@ func (checker *Checker) checkInvocation(
argumentExpressions[i] = argument.Expression
}

invocationRange := ast.NewRangeFromPositioned(
checker.memoryGauge,
invocationExpression,
)

functionType.ArgumentExpressionsCheck(
checker,
argumentExpressions,
Expand All @@ -496,15 +496,12 @@ func (checker *Checker) checkInvocation(
// The invokable type might have special checks for the type parameters.

if functionType.TypePrametersCheck != nil {
invocationRange := ast.NewRangeFromPositioned(
checker.memoryGauge,
invocationExpression,
)

functionType.TypePrametersCheck(
checker.memoryGauge,
typeArguments,
checker.report,
invocationExpression.TypeArguments,
invocationRange,
checker.report,
)
}

Expand Down
4 changes: 3 additions & 1 deletion runtime/sema/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -3379,9 +3379,11 @@ type ArgumentExpressionsCheck func(
)

type TypeParametersCheck func(
memoryGauge common.MemoryGauge,
typeArguments *TypeParameterTypeOrderedMap,
astTypeArguments []*ast.TypeAnnotation,
astInvocationRange ast.Range,
report func(err error),
invocationRange ast.Range,
)

// BaseTypeActivation is the base activation that contains
Expand Down
17 changes: 15 additions & 2 deletions runtime/stdlib/range.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"

"github.com/onflow/cadence/runtime/ast"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/cadence/runtime/errors"
"github.com/onflow/cadence/runtime/interpreter"
"github.com/onflow/cadence/runtime/sema"
Expand Down Expand Up @@ -75,19 +76,31 @@ var inclusiveRangeConstructorFunctionType = func() *sema.FunctionType {
),
// `step` parameter is optional
Arity: &sema.Arity{Min: 2, Max: 3},
TypePrametersCheck: func(typeArguments *sema.TypeParameterTypeOrderedMap, report func(error), invocationRange ast.Range) {
TypePrametersCheck: func(
memoryGauge common.MemoryGauge,
typeArguments *sema.TypeParameterTypeOrderedMap,
astTypeArguments []*ast.TypeAnnotation,
astInvocationRange ast.Range,
report func(error),
) {
memberType, ok := typeArguments.Get(typeParameter)
if !ok || memberType == nil {
// checker should prevent this
panic(errors.NewUnreachableError())

Check warning on line 89 in runtime/stdlib/range.go

View check run for this annotation

Codecov / codecov/patch

runtime/stdlib/range.go#L88-L89

Added lines #L88 - L89 were not covered by tests
}

paramAstRange := astInvocationRange
// If type argument was provided, use its range otherwise fallback to invocation range.
if len(astTypeArguments) > 0 {
paramAstRange = ast.NewRangeFromPositioned(memoryGauge, astTypeArguments[0])
}

// memberType must only be a leaf integer type.
for _, ty := range sema.AllNonLeafIntegerTypes {
if memberType == ty {
report(&sema.InvalidTypeArgumentError{
TypeArgumentName: typeParameter.Name,
Range: invocationRange,
Range: paramAstRange,
Details: fmt.Sprintf("Creation of InclusiveRange<%s> is disallowed", memberType),
})
}
Expand Down

0 comments on commit 733cf47

Please sign in to comment.