Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip inferring variable type from default values #338

Merged
merged 2 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions decoder/expression_candidates_legacy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2476,8 +2476,7 @@ variable "ccc" {}
FriendlyName: "variable",
ScopeId: lang.ScopeId("variable"),
AsTypeOf: &schema.BlockAsTypeOf{
AttributeExpr: "type",
AttributeValue: "default",
AttributeExpr: "type",
},
},
},
Expand Down
6 changes: 2 additions & 4 deletions decoder/hover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1651,8 +1651,7 @@ func TestDecoder_HoverAtPos_extensions_references(t *testing.T) {
ScopeId: lang.ScopeId("variable"),
AsReference: true,
AsTypeOf: &schema.BlockAsTypeOf{
AttributeExpr: "type",
AttributeValue: "default",
AttributeExpr: "type",
},
},
Labels: []*schema.LabelSchema{
Expand Down Expand Up @@ -1719,8 +1718,7 @@ variable "name" {
ScopeId: lang.ScopeId("variable"),
AsReference: true,
AsTypeOf: &schema.BlockAsTypeOf{
AttributeExpr: "type",
AttributeValue: "default",
AttributeExpr: "type",
},
},
Labels: []*schema.LabelSchema{
Expand Down
20 changes: 1 addition & 19 deletions decoder/reference_targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/hashicorp/hcl/v2/ext/typeexpr"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/convert"
)

type ReferenceTargets []*ReferenceTarget
Expand Down Expand Up @@ -466,30 +465,13 @@ func referenceAsTypeOf(block *hcl.Block, rngPtr *hcl.Range, bSchema *schema.Bloc

if bSchema.Address.AsTypeOf.AttributeExpr != "" {
typeDecl, ok := asTypeOfAttrExpr(attrs, bSchema)
if !ok && bSchema.Address.AsTypeOf.AttributeValue == "" {
if !ok {
// nothing to fall back to, exit early
return reference.Targets{ref}
}
ref.Type = typeDecl
}

if bSchema.Address.AsTypeOf.AttributeValue != "" {
attr, ok := attrs[bSchema.Address.AsTypeOf.AttributeValue]
if !ok {
return reference.Targets{ref}
}
value, diags := attr.Expr.Value(nil)
if diags.HasErrors() {
return reference.Targets{ref}
}
val, err := convert.Convert(value, ref.Type)
if err != nil {
// type does not comply with type constraint
return reference.Targets{ref}
}
ref.Type = val.Type()
}

return reference.Targets{ref}
}

Expand Down
153 changes: 4 additions & 149 deletions decoder/reference_targets_collect_hcl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3796,77 +3796,6 @@ provider "test" {
},
},
},
{
"block as data type per attribute - default string",
&schema.BodySchema{
Blocks: map[string]*schema.BlockSchema{
"variable": {
Labels: []*schema.LabelSchema{
{Name: "name"},
},
Address: &schema.BlockAddrSchema{
Steps: []schema.AddrStep{
schema.LabelStep{Index: 0},
},
AsTypeOf: &schema.BlockAsTypeOf{
AttributeValue: "default",
},
},
Type: schema.BlockTypeObject,
Body: &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"type": {
IsOptional: true,
Constraint: schema.TypeDeclaration{},
},
"default": {
IsOptional: true,
Constraint: schema.LiteralType{Type: cty.DynamicPseudoType},
},
},
},
},
},
},
`variable "test" {
default = "something"
}
`,
reference.Targets{
{
Addr: lang.Address{
lang.RootStep{Name: "test"},
},
Type: cty.String,
RangePtr: &hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{
Line: 1,
Column: 1,
Byte: 0,
},
End: hcl.Pos{
Line: 3,
Column: 2,
Byte: 43,
},
},
DefRangePtr: &hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{
Line: 1,
Column: 1,
Byte: 0,
},
End: hcl.Pos{
Line: 1,
Column: 16,
Byte: 15,
},
},
},
},
},
{
"block as data type per attribute - default tuple constant",
&schema.BodySchema{
Expand All @@ -3880,8 +3809,7 @@ provider "test" {
schema.LabelStep{Index: 0},
},
AsTypeOf: &schema.BlockAsTypeOf{
AttributeExpr: "type",
AttributeValue: "default",
AttributeExpr: "type",
},
},
Type: schema.BlockTypeObject,
Expand Down Expand Up @@ -3909,7 +3837,7 @@ provider "test" {
Addr: lang.Address{
lang.RootStep{Name: "test"},
},
Type: cty.Tuple([]cty.Type{cty.String}),
Type: cty.DynamicPseudoType,
RangePtr: &hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{
Expand Down Expand Up @@ -3952,8 +3880,7 @@ provider "test" {
schema.LabelStep{Index: 0},
},
AsTypeOf: &schema.BlockAsTypeOf{
AttributeExpr: "type",
AttributeValue: "default",
AttributeExpr: "type",
},
},
Type: schema.BlockTypeObject,
Expand Down Expand Up @@ -3984,7 +3911,7 @@ provider "test" {
Addr: lang.Address{
lang.RootStep{Name: "test"},
},
Type: cty.List(cty.String),
Type: cty.List(cty.DynamicPseudoType),
RangePtr: &hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{
Expand Down Expand Up @@ -4014,78 +3941,6 @@ provider "test" {
},
},
},
{
"block as data type per attribute - both type and default",
&schema.BodySchema{
Blocks: map[string]*schema.BlockSchema{
"variable": {
Labels: []*schema.LabelSchema{
{Name: "name"},
},
Address: &schema.BlockAddrSchema{
Steps: []schema.AddrStep{
schema.LabelStep{Index: 0},
},
AsTypeOf: &schema.BlockAsTypeOf{
AttributeValue: "default",
},
},
Type: schema.BlockTypeObject,
Body: &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"type": {
IsOptional: true,
Constraint: schema.TypeDeclaration{},
},
"default": {
IsOptional: true,
Constraint: schema.LiteralType{Type: cty.DynamicPseudoType},
},
},
},
},
},
},
`variable "test" {
type = any
default = "something"
}
`,
reference.Targets{
{
Addr: lang.Address{
lang.RootStep{Name: "test"},
},
Type: cty.String,
RangePtr: &hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{
Line: 1,
Column: 1,
Byte: 0,
},
End: hcl.Pos{
Line: 4,
Column: 2,
Byte: 56,
},
},
DefRangePtr: &hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{
Line: 1,
Column: 1,
Byte: 0,
},
End: hcl.Pos{
Line: 1,
Column: 16,
Byte: 15,
},
},
},
},
},
{
"additional targetables",
&schema.BodySchema{
Expand Down
Loading