diff --git a/d2compiler/compile.go b/d2compiler/compile.go index e2c8b71bb..3c0946bfa 100644 --- a/d2compiler/compile.go +++ b/d2compiler/compile.go @@ -281,12 +281,6 @@ func (c *compiler) compileField(obj *d2graph.Object, f *d2ir.Field) { if f.Map() != nil { if len(f.Map().Edges) > 0 { c.errorf(f.Map().Edges[0].LastRef().AST(), "vars cannot contain an edge") - } else { - for _, f := range f.Map().Fields { - if f.Primary() == nil && f.Composite == nil { - c.errorf(f.LastRef().AST(), "invalid var with no value") - } - } } } return diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index d716228cf..b3f189be2 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -3797,17 +3797,6 @@ hi: ${z} `, `d2/testdata/d2compiler/TestCompile2/vars/errors/missing.d2:5:1: could not resolve variable "z"`) }, }, - { - name: "bad-var", - run: func(t *testing.T) { - assertCompile(t, ` -vars: { - x -} -hi: ${x} -`, `d2/testdata/d2compiler/TestCompile2/vars/errors/bad-var.d2:3:3: invalid var with no value`) - }, - }, { name: "multi-part-map", run: func(t *testing.T) { @@ -3818,7 +3807,7 @@ vars: { } } hi: 1 ${x} -`, `d2/testdata/d2compiler/TestCompile2/vars/errors/multi-part-map.d2:7:1: cannot substitute map variable "x" as part of a string`) +`, `d2/testdata/d2compiler/TestCompile2/vars/errors/multi-part-map.d2:7:1: cannot substitute composite variable "x" as part of a string`) }, }, { @@ -3938,7 +3927,7 @@ z: { d: ...${x} c } -`, `d2/testdata/d2compiler/TestCompile2/vars/errors/spread-non-solo.d2:8:2: cannot substitute map variable "x" as part of a string`) +`, `d2/testdata/d2compiler/TestCompile2/vars/errors/spread-non-solo.d2:8:2: cannot substitute composite variable "x" as part of a string`) }, }, } diff --git a/d2ir/compile.go b/d2ir/compile.go index ef88d95a9..ffa5a96ae 100644 --- a/d2ir/compile.go +++ b/d2ir/compile.go @@ -188,8 +188,12 @@ func (c *compiler) resolveSubstitutions(varsStack []*Map, node Node) { } } if resolvedField.Primary() == nil { + if resolvedField.Composite == nil { + c.errorf(node.LastRef().AST(), `cannot substitute variable without value: "%s"`, strings.Join(box.Substitution.IDA(), ".")) + return + } if len(s.Value) > 1 { - c.errorf(node.LastRef().AST(), `cannot substitute map variable "%s" as part of a string`, strings.Join(box.Substitution.IDA(), ".")) + c.errorf(node.LastRef().AST(), `cannot substitute composite variable "%s" as part of a string`, strings.Join(box.Substitution.IDA(), ".")) return } switch n := node.(type) { diff --git a/testdata/d2compiler/TestCompile2/vars/errors/bad-var.exp.json b/testdata/d2compiler/TestCompile2/vars/errors/bad-var.exp.json index c079e0f3c..c740711a7 100644 --- a/testdata/d2compiler/TestCompile2/vars/errors/bad-var.exp.json +++ b/testdata/d2compiler/TestCompile2/vars/errors/bad-var.exp.json @@ -5,6 +5,10 @@ { "range": "d2/testdata/d2compiler/TestCompile2/vars/errors/bad-var.d2,2:2:11-2:3:12", "errmsg": "d2/testdata/d2compiler/TestCompile2/vars/errors/bad-var.d2:3:3: invalid var with no value" + }, + { + "range": "d2/testdata/d2compiler/TestCompile2/vars/errors/bad-var.d2,4:4:23-4:5:24", + "errmsg": "d2/testdata/d2compiler/TestCompile2/vars/errors/bad-var.d2:5:5: invalid var with no value" } ] } diff --git a/testdata/d2compiler/TestCompile2/vars/errors/multi-part-map.exp.json b/testdata/d2compiler/TestCompile2/vars/errors/multi-part-map.exp.json index 70c263dfa..055c954df 100644 --- a/testdata/d2compiler/TestCompile2/vars/errors/multi-part-map.exp.json +++ b/testdata/d2compiler/TestCompile2/vars/errors/multi-part-map.exp.json @@ -4,7 +4,7 @@ "errs": [ { "range": "d2/testdata/d2compiler/TestCompile2/vars/errors/multi-part-map.d2,6:0:31-6:2:33", - "errmsg": "d2/testdata/d2compiler/TestCompile2/vars/errors/multi-part-map.d2:7:1: cannot substitute map variable \"x\" as part of a string" + "errmsg": "d2/testdata/d2compiler/TestCompile2/vars/errors/multi-part-map.d2:7:1: cannot substitute composite variable \"x\" as part of a string" } ] } diff --git a/testdata/d2compiler/TestCompile2/vars/errors/spread-non-solo.exp.json b/testdata/d2compiler/TestCompile2/vars/errors/spread-non-solo.exp.json index 296f35954..3d31fd966 100644 --- a/testdata/d2compiler/TestCompile2/vars/errors/spread-non-solo.exp.json +++ b/testdata/d2compiler/TestCompile2/vars/errors/spread-non-solo.exp.json @@ -4,7 +4,7 @@ "errs": [ { "range": "d2/testdata/d2compiler/TestCompile2/vars/errors/spread-non-solo.d2,7:1:36-7:2:37", - "errmsg": "d2/testdata/d2compiler/TestCompile2/vars/errors/spread-non-solo.d2:8:2: cannot substitute map variable \"x\" as part of a string" + "errmsg": "d2/testdata/d2compiler/TestCompile2/vars/errors/spread-non-solo.d2:8:2: cannot substitute composite variable \"x\" as part of a string" } ] }