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

Fix parsing of reference expressions #1622

Merged
merged 1 commit into from
May 11, 2022
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
7 changes: 4 additions & 3 deletions runtime/parser2/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ import (
"github.com/onflow/cadence/runtime/parser2/lexer"
)

const exprBindingPowerGap = 10

const (
exprLeftBindingPowerTernary = 10 * (iota + 2)
exprLeftBindingPowerTernary = exprBindingPowerGap * (iota + 2)
exprLeftBindingPowerLogicalOr
exprLeftBindingPowerLogicalAnd
exprLeftBindingPowerComparison
Expand Down Expand Up @@ -1117,8 +1119,7 @@ func defineReferenceExpression() {
lexer.TokenAmpersand,
func(p *parser, token lexer.Token) ast.Expression {
p.skipSpaceAndComments(true)
// TODO: maybe require above unary
expression := parseExpression(p, lowestBindingPower)
expression := parseExpression(p, exprLeftBindingPowerCasting-exprBindingPowerGap)

p.skipSpaceAndComments(true)

Expand Down
105 changes: 105 additions & 0 deletions runtime/parser2/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1878,6 +1878,111 @@ func TestParseReference(t *testing.T) {
)
}

func TestParseNilCoelesceReference(t *testing.T) {

t.Parallel()

result, errs := ParseExpression(`
&xs["a"] as &Int? ?? 1
`)
require.Empty(t, errs)

utils.AssertEqualWithDiff(t,
&ast.BinaryExpression{
Operation: ast.OperationNilCoalesce,
Left: &ast.ReferenceExpression{
Expression: &ast.IndexExpression{
TargetExpression: &ast.IdentifierExpression{
Identifier: ast.Identifier{
Identifier: "xs",
Pos: ast.Position{
Offset: 12,
Line: 2,
Column: 11,
},
},
},
IndexingExpression: &ast.StringExpression{
Value: "a",
Range: ast.Range{
StartPos: ast.Position{
Offset: 15,
Line: 2,
Column: 14,
},
EndPos: ast.Position{
Offset: 17,
Line: 2,
Column: 16,
},
},
},
Range: ast.Range{
StartPos: ast.Position{
Offset: 14,
Line: 2,
Column: 13,
},
EndPos: ast.Position{
Offset: 18,
Line: 2,
Column: 17,
},
},
},
Type: &ast.OptionalType{
Type: &ast.ReferenceType{
Authorized: false,
Type: &ast.NominalType{
Identifier: ast.Identifier{
Identifier: "Int",
Pos: ast.Position{
Offset: 24,
Line: 2,
Column: 23,
},
},
},
StartPos: ast.Position{
Offset: 23,
Line: 2,
Column: 22,
},
},
EndPos: ast.Position{
Offset: 27,
Line: 2,
Column: 26,
},
},
StartPos: ast.Position{
Offset: 11,
Line: 2,
Column: 10,
},
},
Right: &ast.IntegerExpression{
PositiveLiteral: "1",
Value: big.NewInt(1),
Base: 10,
Range: ast.Range{
StartPos: ast.Position{
Offset: 32,
Line: 2,
Column: 31,
},
EndPos: ast.Position{
Offset: 32,
Line: 2,
Column: 31,
},
},
},
},
result,
)
}

func TestParseCasts(t *testing.T) {

t.Parallel()
Expand Down
20 changes: 20 additions & 0 deletions runtime/tests/checker/reference_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,26 @@ func TestCheckReferenceExpressionOfOptional(t *testing.T) {
})
}

func TestCheckNilCoalesceReference(t *testing.T) {

t.Parallel()

checker, err := ParseAndCheckWithPanic(t, `
let xs = {"a": 1}
let ref = &xs["a"] as &Int? ?? panic("no a")
`)
require.NoError(t, err)

refValueType := RequireGlobalValue(t, checker.Elaboration, "ref")

assert.Equal(t,
&sema.ReferenceType{
Type: sema.IntType,
},
refValueType,
)
}

func TestCheckInvalidReferenceExpressionNonReferenceAmbiguous(t *testing.T) {

t.Parallel()
Expand Down
41 changes: 41 additions & 0 deletions runtime/tests/interpreter/interpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9785,3 +9785,44 @@ func TestInterpretCastingBoxing(t *testing.T) {
)
})
}

func TestInterpretNilCoalesceReference(t *testing.T) {

t.Parallel()

standardLibraryFunctions :=
stdlib.StandardLibraryFunctions{
stdlib.PanicFunction,
}

valueDeclarations := standardLibraryFunctions.ToSemaValueDeclarations()
values := standardLibraryFunctions.ToInterpreterValueDeclarations()

inter, err := parseCheckAndInterpretWithOptions(t,
`
let xs = {"a": 2}
let ref = &xs["a"] as &Int? ?? panic("no a")
`,
ParseCheckAndInterpretOptions{
CheckerOptions: []sema.Option{
sema.WithPredeclaredValues(valueDeclarations),
},
Options: []interpreter.Option{
interpreter.WithPredeclaredValues(values),
},
},
)
require.NoError(t, err)

variable, ok := inter.Globals.Get("ref")
require.True(t, ok)

require.Equal(
t,
&interpreter.EphemeralReferenceValue{
Value: interpreter.NewIntValueFromInt64(2),
BorrowedType: sema.IntType,
},
variable.GetValue(),
)
}