Fix panic in completion of non-existing namespace variable.

This fixes #1209.
This commit is contained in:
Qi Xiao 2021-01-09 15:54:12 +00:00
parent 363b712f66
commit 9991b08fe0
2 changed files with 14 additions and 3 deletions

View File

@ -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

View File

@ -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) {