typeexpr: Improve diagnostic for any() constraints

Using the any keyword with arguments (e.g. any(string, bool)) is
invalid, but any is not technically a "primitive type keyword". This
commit corrects the language in the diagnostic and updates the tests.
This commit is contained in:
Alisdair McDiarmid 2022-04-27 14:52:03 -04:00
parent 356cee7b89
commit dde4979c49
2 changed files with 10 additions and 3 deletions

View File

@ -77,13 +77,20 @@ func getType(expr hcl.Expression, constraint bool) (cty.Type, hcl.Diagnostics) {
}
switch call.Name {
case "bool", "string", "number", "any":
case "bool", "string", "number":
return cty.DynamicPseudoType, hcl.Diagnostics{{
Severity: hcl.DiagError,
Summary: invalidTypeSummary,
Detail: fmt.Sprintf("Primitive type keyword %q does not expect arguments.", call.Name),
Subject: &call.ArgsRange,
}}
case "any":
return cty.DynamicPseudoType, hcl.Diagnostics{{
Severity: hcl.DiagError,
Summary: invalidTypeSummary,
Detail: fmt.Sprintf("Type constraint keyword %q does not expect arguments.", call.Name),
Subject: &call.ArgsRange,
}}
}
if len(call.Arguments) != 1 {

View File

@ -104,13 +104,13 @@ func TestGetType(t *testing.T) {
`any()`,
false,
cty.DynamicPseudoType,
`Primitive type keyword "any" does not expect arguments.`,
`Type constraint keyword "any" does not expect arguments.`,
},
{
`any()`,
true,
cty.DynamicPseudoType,
`Primitive type keyword "any" does not expect arguments.`,
`Type constraint keyword "any" does not expect arguments.`,
},
{
`list(string)`,