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

Suggest replacing number conversion function calls #346

Merged
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
30 changes: 29 additions & 1 deletion languageserver/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ var containerCompletionItems = []*protocol.CompletionItem{
// Completion is called to compute completion items at a given cursor position.
//
func (s *Server) Completion(
conn protocol.Conn,
_ protocol.Conn,
params *protocol.CompletionParams,
) (
items []*protocol.CompletionItem,
Expand Down Expand Up @@ -1159,6 +1159,11 @@ func (s *Server) getDiagnostics(
diagnostics = append(diagnostics, extraDiagnostics...)
}

for _, hint := range checker.Hints() {
diagnostic := convertHint(hint)
diagnostics = append(diagnostics, diagnostic)
}

return
}

Expand Down Expand Up @@ -1303,3 +1308,26 @@ func convertError(err convertibleError) protocol.Diagnostic {
},
}
}

// convertHint converts a checker error to a diagnostic.
func convertHint(hint sema.Hint) protocol.Diagnostic {
startPosition := hint.StartPosition()
endPosition := hint.EndPosition()

return protocol.Diagnostic{
Message: hint.Hint(),
// protocol.SeverityHint doesn't look prominent enough in VS Code,
// only the first character of the range is highlighted.
Severity: protocol.SeverityInformation,
Range: protocol.Range{
Start: protocol.Position{
Line: float64(startPosition.Line - 1),
Character: float64(startPosition.Column),
},
End: protocol.Position{
Line: float64(endPosition.Line - 1),
Character: float64(endPosition.Column + 1),
},
},
}
}
1 change: 1 addition & 0 deletions runtime/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ func (r *REPL) Accept(code string) (inputIsComplete bool) {
}

r.checker.ResetErrors()
r.checker.ResetHints()

for _, element := range result {

Expand Down
6 changes: 5 additions & 1 deletion runtime/sema/check_invocation_expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,11 @@ func (checker *Checker) checkInvocation(
argumentExpressions[i] = argument.Expression
}

invokableType.CheckArgumentExpressions(checker, argumentExpressions)
invokableType.CheckArgumentExpressions(
checker,
argumentExpressions,
ast.NewRangeFromPositioned(invocationExpression),
)

returnType = functionType.ReturnTypeAnnotation.Type.Resolve(typeArguments)
if returnType == nil {
Expand Down
47 changes: 35 additions & 12 deletions runtime/sema/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ type Checker struct {
allCheckers map[ast.LocationID]*Checker
accessCheckMode AccessCheckMode
errors []error
hints []Hint
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

valueActivations *VariableActivations
resources *Resources
typeActivations *VariableActivations
Expand Down Expand Up @@ -349,6 +350,10 @@ func (checker *Checker) report(err error) {
checker.errors = append(checker.errors, err)
}

func (checker *Checker) hint(hint Hint) {
checker.hints = append(checker.hints, hint)
}

func (checker *Checker) UserDefinedValues() map[string]*Variable {
variables := map[string]*Variable{}

Expand Down Expand Up @@ -618,23 +623,25 @@ func (checker *Checker) checkTypeCompatibility(expression ast.Expression, valueT
// checkIntegerLiteral checks that the value of the integer literal
// fits into range of the target integer type
//
func (checker *Checker) checkIntegerLiteral(expression *ast.IntegerExpression, targetType Type) {
func (checker *Checker) checkIntegerLiteral(expression *ast.IntegerExpression, targetType Type) bool {
ranged := targetType.(IntegerRangedType)
minInt := ranged.MinInt()
maxInt := ranged.MaxInt()

if checker.checkIntegerRange(expression.Value, minInt, maxInt) {
return
if !checker.checkIntegerRange(expression.Value, minInt, maxInt) {
checker.report(
&InvalidIntegerLiteralRangeError{
ExpectedType: targetType,
ExpectedMinInt: minInt,
ExpectedMaxInt: maxInt,
Range: ast.NewRangeFromPositioned(expression),
},
)

return false
}

checker.report(
&InvalidIntegerLiteralRangeError{
ExpectedType: targetType,
ExpectedMinInt: minInt,
ExpectedMaxInt: maxInt,
Range: ast.NewRangeFromPositioned(expression),
},
)
return true
}

// checkFixedPointLiteral checks that the value of the fixed-point literal
Expand Down Expand Up @@ -719,17 +726,21 @@ func (checker *Checker) checkFixedPointLiteral(expression *ast.FixedPointExpress
// checkAddressLiteral checks that the value of the integer literal
// fits into the range of an address (64 bits), and is hexadecimal
//
func (checker *Checker) checkAddressLiteral(expression *ast.IntegerExpression) {
func (checker *Checker) checkAddressLiteral(expression *ast.IntegerExpression) bool {
ranged := &AddressType{}
rangeMin := ranged.MinInt()
rangeMax := ranged.MaxInt()

valid := true

if expression.Base != 16 {
checker.report(
&InvalidAddressLiteralError{
Range: ast.NewRangeFromPositioned(expression),
},
)

valid = false
}

if !checker.checkIntegerRange(expression.Value, rangeMin, rangeMax) {
Expand All @@ -738,7 +749,11 @@ func (checker *Checker) checkAddressLiteral(expression *ast.IntegerExpression) {
Range: ast.NewRangeFromPositioned(expression),
},
)

valid = false
}

return valid
}

func (checker *Checker) checkIntegerRange(value, min, max *big.Int) bool {
Expand Down Expand Up @@ -1684,6 +1699,10 @@ func (checker *Checker) ResetErrors() {
checker.errors = nil
}

func (checker *Checker) ResetHints() {
checker.hints = nil
}

const invalidTypeDeclarationAccessModifierExplanation = "type declarations must be public"

func (checker *Checker) checkDeclarationAccessModifier(
Expand Down Expand Up @@ -2226,3 +2245,7 @@ func (checker *Checker) convertInstantiationType(t *ast.InstantiationType) Type

return parameterizedType.Instantiate(typeArguments, checker.report)
}

func (checker *Checker) Hints() []Hint {
return checker.hints
}
47 changes: 47 additions & 0 deletions runtime/sema/hints.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Cadence - The resource-oriented smart contract programming language
*
* Copyright 2019-2020 Dapper Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package sema

import (
"fmt"

"github.com/onflow/cadence/runtime/ast"
)

type Hint interface {
Hint() string
ast.HasPosition
isHint()
}

// ReplacementHint

type ReplacementHint struct {
Expression ast.Expression
ast.Range
}

func (h *ReplacementHint) Hint() string {
return fmt.Sprintf(
"consider replacing with: `%s`",
h.Expression,
)
}

func (*ReplacementHint) isHint() {}
Loading