Skip to content

Commit

Permalink
Merge pull request #3495 from onflow/supun/fix-supertype-inference
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent authored Jul 25, 2024
2 parents 85e1b4c + 936ec19 commit 282e996
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 3 deletions.
6 changes: 3 additions & 3 deletions runtime/sema/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2768,9 +2768,9 @@ func (checker *Checker) checkNativeModifier(isNative bool, position ast.HasPosit
}

func (checker *Checker) leastCommonSuperType(pos ast.HasPosition, types ...Type) Type {
elementType := LeastCommonSuperType(types...)
superType := LeastCommonSuperType(types...)

if elementType == InvalidType {
if superType == InvalidType {
checker.report(
&TypeAnnotationRequiredError{
Cause: "cannot infer type:",
Expand All @@ -2779,5 +2779,5 @@ func (checker *Checker) leastCommonSuperType(pos ast.HasPosition, types ...Type)
)
}

return elementType
return superType
}
113 changes: 113 additions & 0 deletions runtime/tests/checker/type_inference_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1375,3 +1375,116 @@ func TestCheckDeploymentResultInference(t *testing.T) {

assert.Equal(t, sema.DeploymentResultType, variableSizedType.Type)
}

func TestCheckNilCoalesceExpressionTypeInference(t *testing.T) {

t.Parallel()

t.Run("resource", func(t *testing.T) {
t.Parallel()

code := `
resource R {}
fun f(): @R? {
return <-create R()
}
let y <- f() ?? panic("no R")
`

checker, err := ParseAndCheckWithPanic(t, code)
require.NoError(t, err)

yType := RequireGlobalValue(t, checker.Elaboration, "y")
require.IsType(t, &sema.CompositeType{}, yType)
compositeType := yType.(*sema.CompositeType)

assert.Equal(t, "R", compositeType.Identifier)
})

t.Run("any resource", func(t *testing.T) {
t.Parallel()

code := `
resource R {}
fun f(): @AnyResource? {
return <-create R()
}
let y <- f() ?? panic("no R")
`

checker, err := ParseAndCheckWithPanic(t, code)
require.NoError(t, err)

yType := RequireGlobalValue(t, checker.Elaboration, "y")
assert.Equal(t, sema.AnyResourceType, yType)
})

t.Run("struct", func(t *testing.T) {
t.Parallel()

code := `
struct S {}
fun f(): S? {
return S()
}
let y = f() ?? panic("no S")
`

checker, err := ParseAndCheckWithPanic(t, code)
require.NoError(t, err)

yType := RequireGlobalValue(t, checker.Elaboration, "y")
require.IsType(t, &sema.CompositeType{}, yType)
compositeType := yType.(*sema.CompositeType)

assert.Equal(t, "S", compositeType.Identifier)
})

t.Run("any struct", func(t *testing.T) {
t.Parallel()

code := `
struct S {}
fun f(): AnyStruct? {
return S()
}
let y = f() ?? panic("no S")
`

checker, err := ParseAndCheckWithPanic(t, code)
require.NoError(t, err)

yType := RequireGlobalValue(t, checker.Elaboration, "y")
assert.Equal(t, sema.AnyStructType, yType)
})

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

code := `
struct S {}
resource R {}
fun f(): @R? {
return <-create R()
}
let y <- f() ?? S()
`

_, err := ParseAndCheckWithPanic(t, code)

errs := RequireCheckerErrors(t, err, 1)

typeAnnotRequiredError := &sema.TypeAnnotationRequiredError{}
require.ErrorAs(t, errs[0], &typeAnnotRequiredError)
})
}

0 comments on commit 282e996

Please sign in to comment.