Skip to content

Commit ca962a9

Browse files
committed
Explain next_pos wonkiness
1 parent 6c13445 commit ca962a9

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/reader/parser.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -399,20 +399,29 @@ impl PullParser {
399399

400400
#[inline]
401401
fn next_pos(&mut self) {
402-
if self.pos.len() > 1 {
403-
self.pos.remove(0);
404-
} else {
405-
self.pos[0] = self.lexer.position();
402+
// unfortunately calls to next_pos will never be perfectly balanced with push_pos,
403+
// at very least because parse errors and EOF can happen unexpectedly without a prior push.
404+
if self.pos.len() > 0 {
405+
if self.pos.len() > 1 {
406+
self.pos.remove(0);
407+
} else {
408+
self.pos[0] = self.lexer.position();
409+
}
406410
}
407411
}
408412

409413
#[inline]
414+
#[track_caller]
410415
fn push_pos(&mut self) {
411-
debug_assert!(self.pos.len() != self.pos.capacity(), "How did you get a document that weird? Please file a bug");
416+
debug_assert!(self.pos.len() != self.pos.capacity(), "You've found a bug in xml-rs, caused by calls to push_pos() in states that don't end up emitting events.
417+
This case is ignored in release mode, and merely causes document positions to be out of sync.
418+
Please file a bug and include the XML document that triggers this assert.");
412419

413420
// it has capacity preallocated for more than it ever needs, so this reduces code size
414421
if self.pos.len() != self.pos.capacity() {
415422
self.pos.push(self.lexer.position());
423+
} else if self.pos.len() > 1 {
424+
self.pos.remove(0); // this mitigates the excessive push_pos() call
416425
}
417426
}
418427

0 commit comments

Comments
 (0)