From 6a156f67cef989ee115cec59b49ea0d46a22dbcc Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Tue, 16 Apr 2019 09:34:03 -0700 Subject: [PATCH] vendor: go get github.com/hashicorp/hcl2@master This includes a new specialized error message for when a fractional number is used to index a sequence. --- go.mod | 2 +- go.sum | 4 ++-- vendor/github.com/hashicorp/hcl2/hcl/ops.go | 22 +++++++++++++++++++ vendor/github.com/hashicorp/hcl2/hcl/pos.go | 3 +++ .../hashicorp/hcl2/hcl/pos_scanner.go | 18 +++++++++------ vendor/modules.txt | 2 +- 6 files changed, 40 insertions(+), 11 deletions(-) diff --git a/go.mod b/go.mod index 4970ed1c47..389d995972 100644 --- a/go.mod +++ b/go.mod @@ -60,7 +60,7 @@ require ( github.com/hashicorp/go-version v1.1.0 github.com/hashicorp/golang-lru v0.5.0 // indirect github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f - github.com/hashicorp/hcl2 v0.0.0-20190402200843-8b450a7d58f9 + github.com/hashicorp/hcl2 v0.0.0-20190416162332-2c5a4b7d729a github.com/hashicorp/hil v0.0.0-20190212112733-ab17b08d6590 github.com/hashicorp/logutils v1.0.0 github.com/hashicorp/memberlist v0.1.0 // indirect diff --git a/go.sum b/go.sum index 1aa5c96671..b6c99471db 100644 --- a/go.sum +++ b/go.sum @@ -202,8 +202,8 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f h1:UdxlrJz4JOnY8W+DbLISwf2B8WXEolNRA8BGCwI9jws= github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f/go.mod h1:oZtUIOe8dh44I2q6ScRibXws4Ajl+d+nod3AaR9vL5w= github.com/hashicorp/hcl2 v0.0.0-20181208003705-670926858200/go.mod h1:ShfpTh661oAaxo7VcNxg0zcZW6jvMa7Moy2oFx7e5dE= -github.com/hashicorp/hcl2 v0.0.0-20190402200843-8b450a7d58f9 h1:zCITwiA0cog6aYr/a/McDHKtgsEpYxXvTIgugv5iu8o= -github.com/hashicorp/hcl2 v0.0.0-20190402200843-8b450a7d58f9/go.mod h1:HtEzazM5AZ9fviNEof8QZB4T1Vz9UhHrGhnMPzl//Ek= +github.com/hashicorp/hcl2 v0.0.0-20190416162332-2c5a4b7d729a h1:doKt9ZBCYgYQrGK6CqJsEB+8xqm3WoFyKu4TPZlyymg= +github.com/hashicorp/hcl2 v0.0.0-20190416162332-2c5a4b7d729a/go.mod h1:HtEzazM5AZ9fviNEof8QZB4T1Vz9UhHrGhnMPzl//Ek= github.com/hashicorp/hil v0.0.0-20190212112733-ab17b08d6590 h1:2yzhWGdgQUWZUCNK+AoO35V+HTsgEmcM4J9IkArh7PI= github.com/hashicorp/hil v0.0.0-20190212112733-ab17b08d6590/go.mod h1:n2TSygSNwsLJ76m8qFXTSc7beTb+auJxYdqrnoqwZWE= github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y= diff --git a/vendor/github.com/hashicorp/hcl2/hcl/ops.go b/vendor/github.com/hashicorp/hcl2/hcl/ops.go index 3aa0bdf04e..5d2910c130 100644 --- a/vendor/github.com/hashicorp/hcl2/hcl/ops.go +++ b/vendor/github.com/hashicorp/hcl2/hcl/ops.go @@ -2,6 +2,7 @@ package hcl import ( "fmt" + "math/big" "github.com/zclconf/go-cty/cty" "github.com/zclconf/go-cty/cty/convert" @@ -84,6 +85,27 @@ func Index(collection, key cty.Value, srcRange *Range) (cty.Value, Diagnostics) } } if has.False() { + // We have a more specialized error message for the situation of + // using a fractional number to index into a sequence, because + // that will tend to happen if the user is trying to use division + // to calculate an index and not realizing that HCL does float + // division rather than integer division. + if (ty.IsListType() || ty.IsTupleType()) && key.Type().Equals(cty.Number) { + if key.IsKnown() && !key.IsNull() { + bf := key.AsBigFloat() + if _, acc := bf.Int(nil); acc != big.Exact { + return cty.DynamicVal, Diagnostics{ + { + Severity: DiagError, + Summary: "Invalid index", + Detail: fmt.Sprintf("The given key does not identify an element in this collection value: indexing a sequence requires a whole number, but the given index (%g) has a fractional part.", bf), + Subject: srcRange, + }, + } + } + } + } + return cty.DynamicVal, Diagnostics{ { Severity: DiagError, diff --git a/vendor/github.com/hashicorp/hcl2/hcl/pos.go b/vendor/github.com/hashicorp/hcl2/hcl/pos.go index 6b7ec1d347..06db8bfbd4 100644 --- a/vendor/github.com/hashicorp/hcl2/hcl/pos.go +++ b/vendor/github.com/hashicorp/hcl2/hcl/pos.go @@ -31,6 +31,9 @@ type Pos struct { Byte int } +// InitialPos is a suitable position to use to mark the start of a file. +var InitialPos = Pos{Byte: 0, Line: 1, Column: 1} + // Range represents a span of characters between two positions in a source // file. // diff --git a/vendor/github.com/hashicorp/hcl2/hcl/pos_scanner.go b/vendor/github.com/hashicorp/hcl2/hcl/pos_scanner.go index 7c8f2dfa58..17c0d7c6b1 100644 --- a/vendor/github.com/hashicorp/hcl2/hcl/pos_scanner.go +++ b/vendor/github.com/hashicorp/hcl2/hcl/pos_scanner.go @@ -29,8 +29,8 @@ type RangeScanner struct { err error // error from last scan, if any } -// Create a new RangeScanner for the given buffer, producing ranges for the -// given filename. +// NewRangeScanner creates a new RangeScanner for the given buffer, producing +// ranges for the given filename. // // Since ranges have grapheme-cluster granularity rather than byte granularity, // the scanner will produce incorrect results if the given SplitFunc creates @@ -39,15 +39,19 @@ type RangeScanner struct { // around individual UTF-8 sequences, which will split any multi-sequence // grapheme clusters. func NewRangeScanner(b []byte, filename string, cb bufio.SplitFunc) *RangeScanner { + return NewRangeScannerFragment(b, filename, InitialPos, cb) +} + +// NewRangeScannerFragment is like NewRangeScanner but the ranges it produces +// will be offset by the given starting position, which is appropriate for +// sub-slices of a file, whereas NewRangeScanner assumes it is scanning an +// entire file. +func NewRangeScannerFragment(b []byte, filename string, start Pos, cb bufio.SplitFunc) *RangeScanner { return &RangeScanner{ filename: filename, b: b, cb: cb, - pos: Pos{ - Byte: 0, - Line: 1, - Column: 1, - }, + pos: start, } } diff --git a/vendor/modules.txt b/vendor/modules.txt index 3724332808..e27ec1637b 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -307,7 +307,7 @@ github.com/hashicorp/hcl/hcl/scanner github.com/hashicorp/hcl/hcl/strconv github.com/hashicorp/hcl/json/scanner github.com/hashicorp/hcl/json/token -# github.com/hashicorp/hcl2 v0.0.0-20190402200843-8b450a7d58f9 +# github.com/hashicorp/hcl2 v0.0.0-20190416162332-2c5a4b7d729a github.com/hashicorp/hcl2/hcl github.com/hashicorp/hcl2/hcl/hclsyntax github.com/hashicorp/hcl2/hcldec