From 9991b08fe0c978348a72c9e74200dab179a19b48 Mon Sep 17 00:00:00 2001 From: Qi Xiao Date: Sat, 9 Jan 2021 15:54:12 +0000 Subject: [PATCH] Fix panic in completion of non-existing namespace variable. This fixes #1209. --- pkg/eval/purely_eval.go | 6 +++++- pkg/eval/purely_eval_test.go | 11 +++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/pkg/eval/purely_eval.go b/pkg/eval/purely_eval.go index 57899e1b..6c34821b 100644 --- a/pkg/eval/purely_eval.go +++ b/pkg/eval/purely_eval.go @@ -74,7 +74,11 @@ func (ev *Evaler) PurelyEvalPrimary(pn *parse.Primary) interface{} { fm := &Frame{Evaler: ev, local: ev.Global(), up: new(Ns)} ref := resolveVarRef(fm, qname, nil) if ref != nil { - return deref(fm, ref).Get() + variable := deref(fm, ref) + if variable == nil { + return nil + } + return variable.Get() } } return nil diff --git a/pkg/eval/purely_eval_test.go b/pkg/eval/purely_eval_test.go index 80db3e14..1efa898c 100644 --- a/pkg/eval/purely_eval_test.go +++ b/pkg/eval/purely_eval_test.go @@ -30,6 +30,10 @@ func TestPurelyEvalCompound(t *testing.T) { {code: "foo$x", upto: 3, wantValue: "foo"}, {code: "~", wantValue: home}, {code: "~/foo", wantValue: home + "/foo"}, + {code: "$ns:x", wantValue: "foo"}, + + {code: "$bad", wantErr: ErrImpure}, + {code: "$ns:bad", wantErr: ErrImpure}, {code: "[abc]", wantErr: ErrImpure}, {code: "$y", wantErr: ErrImpure}, @@ -38,10 +42,13 @@ func TestPurelyEvalCompound(t *testing.T) { } ev := NewEvaler() - ev.AddGlobal(NsBuilder{ + g := NsBuilder{ "x": vars.NewReadOnly("bar"), "y": vars.NewReadOnly(vals.MakeList()), - }.Ns()) + }. + AddNs("ns", NsBuilder{"x": vars.NewReadOnly("foo")}.Ns()). + Ns() + ev.AddGlobal(g) for _, test := range tests { t.Run(test.code, func(t *testing.T) {