Skip to content

Commit 4d95005

Browse files
marvinhagemeisterdsherret
authored andcommitted
chore(lint): remove manual AST field counter (#27449)
Addresses the review feedback in #27416 . - Hoist the buffer max size variable to make it less confusing - Remove manual AST field counter in favour of an explicit "commit schema" step which writes the actual field count.
1 parent 68287ab commit 4d95005

File tree

3 files changed

+354
-217
lines changed

3 files changed

+354
-217
lines changed

cli/tools/lint/ast_buffer/buffer.rs

+31-14
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ impl StringTable {
121121
#[derive(Debug, Clone, Copy, PartialEq)]
122122
pub struct NodeRef(pub usize);
123123

124+
/// Represents an offset to a node whose schema hasn't been committed yet
125+
#[derive(Debug, Clone, Copy, PartialEq)]
126+
pub struct PendingNodeRef(pub NodeRef);
127+
124128
#[derive(Debug)]
125129
pub struct BoolPos(pub usize);
126130
#[derive(Debug)]
@@ -152,20 +156,16 @@ where
152156
K: Into<u8> + Display,
153157
P: Into<u8> + Display,
154158
{
155-
fn header(
156-
&mut self,
157-
kind: K,
158-
parent: NodeRef,
159-
span: &Span,
160-
prop_count: usize,
161-
) -> NodeRef;
159+
fn header(&mut self, kind: K, parent: NodeRef, span: &Span)
160+
-> PendingNodeRef;
162161
fn ref_field(&mut self, prop: P) -> FieldPos;
163162
fn ref_vec_field(&mut self, prop: P, len: usize) -> FieldArrPos;
164163
fn str_field(&mut self, prop: P) -> StrPos;
165164
fn bool_field(&mut self, prop: P) -> BoolPos;
166165
fn undefined_field(&mut self, prop: P) -> UndefPos;
167166
#[allow(dead_code)]
168167
fn null_field(&mut self, prop: P) -> NullPos;
168+
fn commit_schema(&mut self, offset: PendingNodeRef) -> NodeRef;
169169

170170
fn write_ref(&mut self, pos: FieldPos, value: NodeRef);
171171
fn write_maybe_ref(&mut self, pos: FieldPos, value: Option<NodeRef>);
@@ -183,6 +183,7 @@ pub struct SerializeCtx {
183183
str_table: StringTable,
184184
kind_map: Vec<usize>,
185185
prop_map: Vec<usize>,
186+
field_count: u8,
186187
}
187188

188189
/// This is the internal context used to allocate and fill the buffer. The point
@@ -200,8 +201,9 @@ impl SerializeCtx {
200201
start_buf: NodeRef(0),
201202
buf: vec![],
202203
str_table: StringTable::new(),
203-
kind_map: vec![0; kind_size + 1],
204-
prop_map: vec![0; prop_size + 1],
204+
kind_map: vec![0; kind_size],
205+
prop_map: vec![0; prop_size],
206+
field_count: 0,
205207
};
206208

207209
let empty_str = ctx.str_table.insert("");
@@ -232,6 +234,8 @@ impl SerializeCtx {
232234
where
233235
P: Into<u8> + Display + Clone,
234236
{
237+
self.field_count += 1;
238+
235239
let offset = self.buf.len();
236240

237241
let n: u8 = prop.clone().into();
@@ -268,7 +272,7 @@ impl SerializeCtx {
268272
parent: NodeRef,
269273
span: &Span,
270274
prop_count: usize,
271-
) -> NodeRef {
275+
) -> PendingNodeRef {
272276
let offset = self.buf.len();
273277

274278
// Node type fits in a u8
@@ -285,7 +289,19 @@ impl SerializeCtx {
285289
debug_assert!(prop_count < 10);
286290
self.buf.push(prop_count as u8);
287291

288-
NodeRef(offset)
292+
PendingNodeRef(NodeRef(offset))
293+
}
294+
295+
pub fn commit_schema(&mut self, node_ref: PendingNodeRef) -> NodeRef {
296+
let mut offset = node_ref.0 .0;
297+
298+
// type + parentId + span lo + span hi
299+
offset += 1 + 4 + 4 + 4;
300+
301+
self.buf[offset] = self.field_count;
302+
self.field_count = 0;
303+
304+
node_ref.0
289305
}
290306

291307
/// Allocate the node header. It's always the same for every node.
@@ -299,8 +315,7 @@ impl SerializeCtx {
299315
kind: N,
300316
parent: NodeRef,
301317
span: &Span,
302-
prop_count: usize,
303-
) -> NodeRef
318+
) -> PendingNodeRef
304319
where
305320
N: Into<u8> + Display + Clone,
306321
{
@@ -313,7 +328,9 @@ impl SerializeCtx {
313328
}
314329
}
315330

316-
self.append_node(n, parent, span, prop_count)
331+
// Prop count will be filled with the actual value when the
332+
// schema is committed.
333+
self.append_node(n, parent, span, 0)
317334
}
318335

319336
/// Allocate a reference property that will hold the offset of

0 commit comments

Comments
 (0)