internal/core/adt: prevent nil interface bug for Source

Fixes #521

Change-Id: I59c789f72ba9c6597378279f26458d77721a4da1
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7122
Reviewed-by: Paul Jolly <paul@myitcv.org.uk>
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
diff --git a/internal/core/adt/expr.go b/internal/core/adt/expr.go
index e9b57fc..ea55ec3 100644
--- a/internal/core/adt/expr.go
+++ b/internal/core/adt/expr.go
@@ -273,8 +273,13 @@
 // Top represents all possible values. It can be used as a Value and Expr.
 type Top struct{ Src *ast.Ident }
 
-func (x *Top) Source() ast.Node { return x.Src }
-func (x *Top) Kind() Kind       { return TopKind }
+func (x *Top) Source() ast.Node {
+	if x.Src == nil {
+		return nil
+	}
+	return x.Src
+}
+func (x *Top) Kind() Kind { return TopKind }
 
 // BasicType represents all values of a certain Kind. It can be used as a Value
 // and Expr.
@@ -1246,6 +1251,9 @@
 }
 
 func (x *ValueClause) Source() ast.Node {
+	if x.StructLit == nil {
+		return nil
+	}
 	if x.Src == nil {
 		return nil
 	}
diff --git a/internal/core/adt/expr_test.go b/internal/core/adt/expr_test.go
new file mode 100644
index 0000000..3d7a936
--- /dev/null
+++ b/internal/core/adt/expr_test.go
@@ -0,0 +1,74 @@
+// Copyright 2020 CUE Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package adt
+
+import (
+	"fmt"
+	"testing"
+)
+
+func TestNilSource(t *testing.T) {
+	testCases := []Node{
+		&BasicType{},
+		&BinaryExpr{},
+		&Bool{},
+		&Bottom{},
+		&BoundExpr{},
+		&BoundValue{},
+		&Builtin{},
+		&BuiltinValidator{},
+		&BulkOptionalField{},
+		&Bytes{},
+		&CallExpr{},
+		&Conjunction{},
+		&Disjunction{},
+		&DisjunctionExpr{},
+		&DynamicField{},
+		&DynamicReference{},
+		&Ellipsis{},
+		&Field{},
+		&FieldReference{},
+		&ForClause{},
+		&IfClause{},
+		&ImportReference{},
+		&IndexExpr{},
+		&Interpolation{},
+		&LabelReference{},
+		&LetClause{},
+		&LetReference{},
+		&ListLit{},
+		&ListMarker{},
+		&NodeLink{},
+		&Null{},
+		&Num{},
+		&OptionalField{},
+		&SelectorExpr{},
+		&SliceExpr{},
+		&String{},
+		&StructLit{},
+		&StructMarker{},
+		&Top{},
+		&UnaryExpr{},
+		&ValueClause{},
+		&Vertex{},
+	}
+	for _, x := range testCases {
+		t.Run(fmt.Sprintf("%T", x), func(t *testing.T) {
+			if x.Source() != nil {
+				t.Error("nil source did not return nil")
+			}
+		})
+	}
+}