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

Check native function declarations #2821

Merged
merged 3 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 16 additions & 1 deletion runtime/sema/check_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,21 @@ func (checker *Checker) visitFunctionDeclaration(
declaration.Identifier,
)

functionBlock := declaration.FunctionBlock

if declaration.IsNative() {
if !functionBlock.IsEmpty() {
checker.report(&NativeFunctionWithImplementationError{
Range: ast.NewRangeFromPositioned(
checker.memoryGauge,
functionBlock,
),
})
}

functionBlock = nil
}

// global functions were previously declared, see `declareFunctionDeclaration`

functionType := checker.Elaboration.FunctionDeclarationFunctionType(declaration)
Expand All @@ -93,7 +108,7 @@ func (checker *Checker) visitFunctionDeclaration(
declaration.ParameterList,
declaration.ReturnTypeAnnotation,
functionType,
declaration.FunctionBlock,
functionBlock,
options.mustExit,
nil,
options.checkResourceLoss,
Expand Down
17 changes: 17 additions & 0 deletions runtime/sema/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,23 @@ func (e *InvalidNativeModifierError) Error() string {
return "invalid native modifier for declaration"
}

// NativeFunctionWithImplementationError

type NativeFunctionWithImplementationError struct {
ast.Range
}

var _ SemanticError = &NativeFunctionWithImplementationError{}
var _ errors.UserError = &NativeFunctionWithImplementationError{}

func (*NativeFunctionWithImplementationError) isSemanticError() {}

func (*NativeFunctionWithImplementationError) IsUserError() {}

func (e *NativeFunctionWithImplementationError) Error() string {
return "native function may not have an implementation"
turbolent marked this conversation as resolved.
Show resolved Hide resolved
}

// InvalidNameError

type InvalidNameError struct {
Expand Down
76 changes: 65 additions & 11 deletions runtime/tests/checker/function_test.go
SupunS marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -440,20 +440,74 @@ func TestCheckNativeFunctionDeclaration(t *testing.T) {

t.Parallel()

_, err := ParseAndCheckWithOptions(t,
`
native fun test() {}
`,
ParseAndCheckOptions{
ParseOptions: parser.Config{
NativeModifierEnabled: true,
t.Run("disabled", func(t *testing.T) {

t.Parallel()

_, err := ParseAndCheckWithOptions(t,
`
native fun test(): Int {}
`,
ParseAndCheckOptions{
ParseOptions: parser.Config{
NativeModifierEnabled: true,
},
Config: &sema.Config{
AllowNativeDeclarations: false,
},
},
},
)
)

errs := RequireCheckerErrors(t, err, 1)
errs := RequireCheckerErrors(t, err, 1)

assert.IsType(t, &sema.InvalidNativeModifierError{}, errs[0])
})

t.Run("enabled, valid", func(t *testing.T) {

t.Parallel()

_, err := ParseAndCheckWithOptions(t,
`
native fun test(): Int {}
`,
ParseAndCheckOptions{
ParseOptions: parser.Config{
NativeModifierEnabled: true,
},
Config: &sema.Config{
AllowNativeDeclarations: true,
},
},
)

require.NoError(t, err)
})

t.Run("enabled, invalid", func(t *testing.T) {

assert.IsType(t, &sema.InvalidNativeModifierError{}, errs[0])
t.Parallel()

_, err := ParseAndCheckWithOptions(t,
`
native fun test(): Int {
return 1
}
`,
ParseAndCheckOptions{
ParseOptions: parser.Config{
NativeModifierEnabled: true,
},
Config: &sema.Config{
AllowNativeDeclarations: true,
},
},
)

errs := RequireCheckerErrors(t, err, 1)

assert.IsType(t, &sema.NativeFunctionWithImplementationError{}, errs[0])
})
}

func TestCheckResultVariable(t *testing.T) {
Expand Down
Loading