Skip to content

Commit

Permalink
Make cast-hints less restrictive
Browse files Browse the repository at this point in the history
  • Loading branch information
SupunS committed Nov 9, 2021
1 parent bbf77e9 commit a8c7784
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 27 deletions.
19 changes: 3 additions & 16 deletions runtime/sema/check_casting_expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,17 +431,14 @@ func IsCastRedundant(expr ast.Expression, exprInferredType, targetType, expected
return true
}

checkCastVisitor := &CheckCastVisitor{
expectedType: expectedType,
}
checkCastVisitor := &CheckCastVisitor{}

return checkCastVisitor.isCastRedundant(expr, exprInferredType, targetType)
}

type CheckCastVisitor struct {
exprInferredType Type
targetType Type
expectedType Type
}

var _ ast.ExpressionVisitor = &CheckCastVisitor{}
Expand Down Expand Up @@ -622,16 +619,6 @@ func (d *CheckCastVisitor) isTypeRedundant(exprType, targetType Type) bool {
// var x: Int8 = 5
// var y = x as Int8 // <-- not ok: `y` will be of type `Int8` with/without cast
// var y = x as Integer // <-- ok : `y` will be of type `Integer`
if d.expectedType == nil {
return exprType != nil &&
exprType.Equal(targetType)
}

// Otherwise, if there is already an expected type from the context,
// then target type being same/supertype is redundant.
// e.g:
// var x: Int8 = 5
// var y: AnyStruct = x as Int8 // <-- not ok: `y` will anyway have an `Int8` value
// var y: AnyStruct = x as Integer // <-- not ok: `y` will anyway have an `Int8` value
return IsSubType(exprType, targetType)
return exprType != nil &&
exprType.Equal(targetType)
}
33 changes: 22 additions & 11 deletions runtime/tests/checker/casting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6172,39 +6172,50 @@ func TestCheckUnnecessaryCasts(t *testing.T) {
checker, err := ParseAndCheckWithAny(t, `
let x: Int8 = 5
let y: AnyStruct = x as Int8 // Not OK
let z: AnyStruct = x as Integer // Not OK
let z: AnyStruct = x as Integer // OK
`)

require.NoError(t, err)

hints := checker.Hints()
require.Len(t, hints, 2)
require.Len(t, hints, 1)

require.IsType(t, &sema.UnnecessaryCastHint{}, hints[0])
castHint := hints[0].(*sema.UnnecessaryCastHint)
assert.Equal(t, sema.Int8Type, castHint.TargetType)

require.IsType(t, &sema.UnnecessaryCastHint{}, hints[1])
castHint = hints[1].(*sema.UnnecessaryCastHint)
assert.Equal(t, sema.IntegerType, castHint.TargetType)
})

t.Run("Int literal with expected type", func(t *testing.T) {
t.Run("With invalid expected type", func(t *testing.T) {
t.Parallel()

checker, err := ParseAndCheckWithAny(t, `
let x: AnyStruct = 4 as Int8 // OK
let y: AnyStruct = 4 as Integer // Not OK
let x: Int8 = 5
let y: String = x as Int8
let z: String = x as Integer
`)

require.NoError(t, err)
require.Error(t, err)

hints := checker.Hints()
require.Len(t, hints, 1)

require.IsType(t, &sema.UnnecessaryCastHint{}, hints[0])
castHint := hints[0].(*sema.UnnecessaryCastHint)
assert.Equal(t, sema.IntegerType, castHint.TargetType)
assert.Equal(t, sema.Int8Type, castHint.TargetType)
})

t.Run("Int literal with expected type", func(t *testing.T) {
t.Parallel()

checker, err := ParseAndCheckWithAny(t, `
let x: AnyStruct = 4 as Int8 // OK
let y: AnyStruct = 4 as Integer // OK
`)

require.NoError(t, err)

hints := checker.Hints()
require.Len(t, hints, 0)
})

t.Run("Fixed point literal", func(t *testing.T) {
Expand Down

0 comments on commit a8c7784

Please sign in to comment.