Skip to content

Commit 7ba7aa5

Browse files
committed
fix: fix panic on unset vars
1 parent 5a6be23 commit 7ba7aa5

File tree

4 files changed

+34
-26
lines changed

4 files changed

+34
-26
lines changed

internal/analysis/check.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -135,32 +135,32 @@ type CheckResult struct {
135135
Program parser.Program
136136

137137
stmtType Type
138-
ExprTypes map[parser.ValueExpr]Type
139-
VarTypes map[parser.VarDeclaration]Type
138+
exprTypes map[parser.ValueExpr]Type
139+
varTypes map[parser.VarDeclaration]Type
140140
}
141141

142-
func (r *CheckResult) getExprType(expr parser.ValueExpr) Type {
143-
exprType, ok := r.ExprTypes[expr]
142+
func (r *CheckResult) GetExprType(expr parser.ValueExpr) Type {
143+
exprType, ok := r.exprTypes[expr]
144144
if !ok {
145145
t := TVar{}
146-
r.ExprTypes[expr] = &t
146+
r.exprTypes[expr] = &t
147147
return &t
148148
}
149-
return exprType
149+
return exprType.Resolve()
150150
}
151151

152-
func (r *CheckResult) getVarDeclType(decl parser.VarDeclaration) Type {
153-
exprType, ok := r.VarTypes[decl]
152+
func (r *CheckResult) GetVarDeclType(decl parser.VarDeclaration) Type {
153+
exprType, ok := r.varTypes[decl]
154154
if !ok {
155155
t := TVar{}
156-
r.VarTypes[decl] = &t
156+
r.varTypes[decl] = &t
157157
return &t
158158
}
159-
return exprType
159+
return exprType.Resolve()
160160
}
161161

162162
func (r *CheckResult) unifyNodeWith(expr parser.ValueExpr, t Type) {
163-
exprT := r.getExprType(expr)
163+
exprT := r.GetExprType(expr)
164164
r.unify(expr.GetRange(), exprT, t)
165165
}
166166

@@ -224,8 +224,8 @@ func newCheckResult(program parser.Program) CheckResult {
224224
unusedVars: make(map[string]parser.Range),
225225
varResolution: make(map[*parser.Variable]parser.VarDeclaration),
226226
fnCallResolution: make(map[*parser.FnCallIdentifier]FnCallResolution),
227-
ExprTypes: make(map[parser.ValueExpr]Type),
228-
VarTypes: make(map[parser.VarDeclaration]Type),
227+
exprTypes: make(map[parser.ValueExpr]Type),
228+
varTypes: make(map[parser.VarDeclaration]Type),
229229
}
230230
}
231231

@@ -242,7 +242,7 @@ func (res *CheckResult) check() {
242242

243243
if varDecl.Origin != nil {
244244
res.checkExpression(*varDecl.Origin, varDecl.Type.Name)
245-
res.unifyNodeWith(*varDecl.Origin, res.getVarDeclType(varDecl))
245+
res.unifyNodeWith(*varDecl.Origin, res.GetVarDeclType(varDecl))
246246
}
247247
}
248248
}
@@ -357,10 +357,10 @@ func (res *CheckResult) checkFnCallArity(fnCall *parser.FnCall) {
357357
case FnVarOriginBalance, FnVarOriginOverdraft:
358358
// we run unify(<expr>, <asset>) in:
359359
// <expr> := balance(@acc, <asset>)
360-
res.unifyNodeWith(fnCall, res.getExprType(validArgs[1]))
360+
res.unifyNodeWith(fnCall, res.GetExprType(validArgs[1]))
361361

362362
case FnVarOriginGetAsset:
363-
res.unifyNodeWith(fnCall, res.getExprType(validArgs[0]))
363+
res.unifyNodeWith(fnCall, res.GetExprType(validArgs[0]))
364364
}
365365
} else {
366366
for _, arg := range validArgs {
@@ -421,7 +421,7 @@ func (res *CheckResult) checkTypeOf(lit parser.ValueExpr, typeHint string) strin
421421
case *parser.Variable:
422422
if varDeclaration, ok := res.DeclaredVars[lit.Name]; ok {
423423
res.varResolution[lit] = varDeclaration
424-
res.unifyNodeWith(lit, res.getVarDeclType(varDeclaration))
424+
res.unifyNodeWith(lit, res.GetVarDeclType(varDeclaration))
425425
} else {
426426
res.pushDiagnostic(lit.Range, UnboundVariable{Name: lit.Name, Type: typeHint})
427427
}
@@ -440,11 +440,11 @@ func (res *CheckResult) checkTypeOf(lit parser.ValueExpr, typeHint string) strin
440440
we unify $mon and $asset in:
441441
`let $mon := [$asset 42]`
442442
*/
443-
res.unifyNodeWith(lit, res.getExprType(lit.Asset))
443+
res.unifyNodeWith(lit, res.GetExprType(lit.Asset))
444444
return TypeMonetary
445445

446446
case *parser.BinaryInfix:
447-
res.unifyNodeWith(lit.Left, res.getExprType(lit.Right))
447+
res.unifyNodeWith(lit.Left, res.GetExprType(lit.Right))
448448

449449
switch lit.Operator {
450450
case parser.InfixOperatorPlus:

internal/analysis/check_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,9 +1066,8 @@ send $mon1 (
10661066

10671067
res := analysis.CheckSource(input)
10681068

1069-
t1 := res.VarTypes[res.DeclaredVars["mon1"]]
1070-
1071-
t2 := res.VarTypes[res.DeclaredVars["mon2"]]
1069+
t1 := res.GetVarDeclType(res.DeclaredVars["mon1"])
1070+
t2 := res.GetVarDeclType(res.DeclaredVars["mon2"])
10721071

10731072
require.Same(t, t1.Resolve(), t2.Resolve())
10741073
}
@@ -1085,7 +1084,7 @@ vars {
10851084
res := analysis.CheckSource(input)
10861085

10871086
v := res.DeclaredVars["ass"]
1088-
t1 := res.VarTypes[v]
1087+
t1 := res.GetVarDeclType(v)
10891088

10901089
expected := analysis.TAsset("USD/2")
10911090
require.Equal(t, &expected, t1.Resolve())

internal/cmd/test_init.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ func getTestInitCmd() *cobra.Command {
4444
return cmd
4545
}
4646

47-
func mkDefaultVar(decl parser.VarDeclaration, varsTypes map[parser.VarDeclaration]analysis.Type) string {
47+
func mkDefaultVar(decl parser.VarDeclaration, checkResult analysis.CheckResult) string {
4848
defaultAmt := 100
4949
defaultCurr := "USD/2"
5050

51-
asset := varsTypes[decl].Resolve()
51+
asset := checkResult.GetVarDeclType(decl)
5252
switch asset := asset.(type) {
5353
case *analysis.TAsset:
5454
defaultCurr = string(*asset)
@@ -94,7 +94,7 @@ func MakeSpecsFile(source string) (specs_format.Specs, error) {
9494
continue
9595
}
9696

97-
value := mkDefaultVar(decl, checkResult.VarTypes)
97+
value := mkDefaultVar(decl, checkResult)
9898
vars[decl.Name.Name] = value
9999
}
100100
}

internal/cmd/test_init_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ func TestMakeSpecsFileRetryForMissingFunds(t *testing.T) {
2525
}, out.Balances)
2626
}
2727

28+
func TestUnusedVars(t *testing.T) {
29+
out, err := cmd.MakeSpecsFile(`
30+
vars { monetary $m }
31+
`)
32+
33+
require.Nil(t, err)
34+
require.Equal(t, interpreter.VariablesMap{"m": "USD/2 100"}, out.Vars)
35+
}
36+
2837
func TestMakeSpecsFileRetryForMissingFeatureFlags(t *testing.T) {
2938
out, err := cmd.MakeSpecsFile(`
3039
send [USD/2 10000] (

0 commit comments

Comments
 (0)