Skip to content

Commit

Permalink
Fix error report of type mismatch on inline tables (#853)
Browse files Browse the repository at this point in the history
Parser did not track the location of the faulty inline table in the
document, and unmarshaler tried to the use the non-raw data field of the
AST node, both resulting into a panic when generating the parser error.

Fixes #850
  • Loading branch information
pelletier committed Feb 28, 2023
1 parent fcd9179 commit 8a416da
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion unmarshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ func (d *decoder) unmarshalInlineTable(itable *unstable.Node, v reflect.Value) e
}
return d.unmarshalInlineTable(itable, elem)
default:
return unstable.NewParserError(itable.Data, "cannot store inline table in Go type %s", v.Kind())
return unstable.NewParserError(d.p.Raw(itable.Raw), "cannot store inline table in Go type %s", v.Kind())
}

it := itable.Children()
Expand Down
6 changes: 6 additions & 0 deletions unmarshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2466,6 +2466,12 @@ func TestIssue807(t *testing.T) {
require.Equal(t, "foo", m.Name)
}

func TestIssue850(t *testing.T) {
data := make(map[string]string)
err := toml.Unmarshal([]byte("foo = {}"), &data)
require.Error(t, err)
}

func TestUnmarshalDecodeErrors(t *testing.T) {
examples := []struct {
desc string
Expand Down
1 change: 1 addition & 0 deletions unstable/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ func (p *Parser) parseInlineTable(b []byte) (reference, []byte, error) {
// inline-table-keyvals = keyval [ inline-table-sep inline-table-keyvals ]
parent := p.builder.Push(Node{
Kind: InlineTable,
Raw: p.Range(b[:1]),
})

first := true
Expand Down

0 comments on commit 8a416da

Please sign in to comment.