-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdestructure.go
More file actions
207 lines (198 loc) · 6.38 KB
/
Copy pathdestructure.go
File metadata and controls
207 lines (198 loc) · 6.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package analysis
import (
"fmt"
"iter"
"github.com/ProCode-Software/klar/internal/ast"
"github.com/ProCode-Software/klar/internal/klarerrs"
"github.com/ProCode-Software/klar/internal/ranges"
)
// followDestructure follows a destructure expression, yielding each
// [ast.Assignable] on the left (from dest) and its corresponding [Type]
// from the right (from e). Errors are reported for type mismatches,
// and for [ast.BadExpression] nodes if nameOnly == false.
//
// All [ast.BadExpression] nodes are skipped (if nameOnly == true,
// errors would have already been reported by [Checker.declareVars]). If
// nameOnly == true, all yielded nodes are [*ast.Symbol] or [*ast.Discard].
func (c *Checker) followDestructure(
lhs ast.Assignable, rhs Type, fid FileID, r ranges.Range, nameOnly bool,
) iter.Seq2[ast.Assignable, Type] {
dc := &destructureContext{fid: fid, rhsRange: r, nameOnly: nameOnly}
return func(yield func(ast.Assignable, Type) bool) {
dc.walk = func(dest ast.Expression, typ Type) bool {
// Ensure the expression can actually be in a destructure pattern.
// For a top-level destructure `[a, b] = ...`, this will always be the
// case, but in lists and tuples, it isn't checked at parse-time.
assg, ok := dest.(ast.Assignable)
if !ok {
if !nameOnly {
err := klarerrs.Node(klarerrs.ErrInvalidAssignment, dest)
err.Label = "Can't assign to this expression"
c.fileError(err, dc.fid)
}
return true
}
// We don't want walk{List,Tuple}Destructure to report an error if the
// type isn't a list/tuple.
if typ.Kind() == InvalidType {
return yield(assg, typ)
}
switch dest := assg.(type) {
case *ast.Symbol:
return yield(dest, typ)
case *ast.ListLiteral:
return c.walkListDestructure(dest, typ, dc)
case *ast.TupleLiteral:
return c.walkTupleDestructure(dest, typ, dc)
case *ast.Discard:
return true
case *ast.BadExpression:
return true // Syntax error already reported
case ast.Destructurable:
panic(fmt.Sprintf(
"followDestructure: unhandled ast.Destructurable pattern type: %T",
dest,
))
default:
if nameOnly {
return true // Error already reported by [Checker.declareVars]
}
return yield(dest, typ)
}
}
dc.walk(lhs, rhs)
}
}
type destructureContext struct {
fid FileID
walk func(ast.Expression, Type) bool
rhsRange ranges.Range
nameOnly bool // Reports syntax errors if false
}
// TODO: In List and Tuple destructure, ensure there is only 1 rest. Also properly
// check for the next destructure patterns after the rest.
func (c *Checker) walkListDestructure(dest *ast.ListLiteral,
t Type, dc *destructureContext,
) bool {
if t.Kind() != KindList {
err := klarerrs.TypeError(
klarerrs.ErrTypeMismatch, dc.rhsRange, KindList.String(), t.String(),
)
err.AddHighlight("This pattern destructures a list", dest.Range)
c.fileError(err, dc.fid)
return true
}
list := Underlying(t).(*List)
elem := list.Elem
for _, item := range dest.Items {
// Rest destructure
// [a, b, rest...] := [1, 2, 3, 4, 5]
// [a, b, obj.items...] = [1, 2, 3, 4, 5]
if rest, ok := item.(*ast.RestExpression); ok {
// The rest item has type [T]
if !dc.walk(rest.Expression, &List{Elem: elem}) {
return false
}
} else
// Normal destructure
// [a, b, c] := [1, 2, 3]
// [first, (key, value)] := [('a', 1), ('b', 2)]
if !dc.walk(item, elem) {
return false
}
}
return true
}
func (c *Checker) walkTupleDestructure(dest *ast.TupleLiteral,
t Type, dc *destructureContext,
) bool {
if t.Kind() != KindTuple {
err := klarerrs.TypeError(
klarerrs.ErrTypeMismatch, dc.rhsRange, KindTuple.String(), t.String(),
)
err.AddHighlight("This pattern destructures a tuple", dest.Range)
c.fileError(err, dc.fid)
return true
}
rhs := Underlying(t).(*Tuple)
for i, item := range dest.Values {
if i >= rhs.Len() {
// More variables on the left than items on the right.
// (a, b, c) := (1, 2)
err := makeMismatchTupleDestructError(
dest.Values[i:], rhs.Len(), dc.rhsRange,
)
c.fileError(err, dc.fid)
return true
}
if rest, ok := item.(*ast.RestExpression); ok {
// Rest item in destructure
// (a, b...) := (1, 2, 3, 4)
// 'b' gets (2, 3, 4)
restTuple := &Tuple{rhs.Items[i:]}
if restTuple.Len() < 2 {
// The rest item must have at least 2 items, so the rest in the
// example above is invalid if the RHS is (1, 2) or (1, 2, 3).
err := makeTupleSpreadCountError(rest, restTuple.Len(), true)
err.AddHighlight(
"This tuple has "+klarerrs.FormatCount(rhs.Len(), "item"),
dc.rhsRange,
)
c.fileError(err, dc.fid)
continue
}
if !dc.walk(rest.Expression, restTuple) {
return false
}
} else if !dc.walk(item, rhs.Items[i]) { // Non-rest item on LHS
return false
}
}
// Currently, there can be LESS items on the left than the right
// (a, b) := (1, 2, 3)
return true
}
// The tuple destructure on the LHS has more variables than the RHS tuple's values.
func makeMismatchTupleDestructError(remaining []ast.Expression,
rhsLen int, rhsRange ranges.Range,
) *klarerrs.Error {
err := klarerrs.Slice(klarerrs.ErrMismatchTupleDestruct, remaining)
err.Label = "Not enough values on the right to assign to " +
klarerrs.FormatThis(len(remaining))
if len(remaining) == 0 {
err.AddHighlight("The tuple on the right has no values to destructure", rhsRange)
} else {
err.AddHighlight(
"The tuple on the right has only "+klarerrs.FormatCount(rhsLen, "value"),
rhsRange,
)
}
err.SetParam("remaining", len(remaining))
return err
}
// The tuple being spread has less than 2 items.
func makeTupleSpreadCountError(rest *ast.RestExpression, willGet int, destruct bool) *klarerrs.Error {
// TODO: Convert rest.Expression to a string and store in err.Name
err := klarerrs.Node(klarerrs.ErrTupleRestDestruct, rest)
err.SetParam("n", willGet)
targetRange := rest.Expression.GetRange()
switch willGet {
case 0:
err.AddHighlight("This variable will have no items", targetRange)
if !destruct {
err.Hint("Remove the spread")
}
case 1:
err.AddHighlight("This variable will have only 1 item", targetRange)
hintWithDiff(
err, "Take the individual item from the tuple",
klarerrs.DeletedRange{
ranges.Range{rest.Range.Start, rest.Expression.GetRange().Start},
},
klarerrs.AddedString{Pos: rest.Range.End, String: "[0]"},
)
default:
panic(fmt.Sprintf("unreachable: %d", willGet))
}
return err
}