From ac4102bcb2bfb378b7543eea67d4e593d1c6946d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Sinan=20A=C4=9Facan?= Date: Tue, 23 Feb 2021 16:06:29 +0300 Subject: [PATCH] Replace assertions in Puctuated with more helpful panic messages Debugging proc macros is very difficult as we can't easily get backtraces into the proc macro code. Improved error messages will hopefully give some hint to the user on what the problem is. --- src/punctuated.rs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/punctuated.rs b/src/punctuated.rs index 4d3496976c..7460cda62f 100644 --- a/src/punctuated.rs +++ b/src/punctuated.rs @@ -162,7 +162,13 @@ impl Punctuated { /// Panics if the sequence does not already have a trailing punctuation when /// this method is called. pub fn push_value(&mut self, value: T) { - assert!(self.empty_or_trailing()); + if !self.empty_or_trailing() { + panic!( + "Punctuated::push_value: Punctuated is not empty or \ + does not have a trailing punctuation" + ); + } + self.last = Some(Box::new(value)); } @@ -174,7 +180,10 @@ impl Punctuated { /// /// Panics if the sequence is empty or already has a trailing punctuation. pub fn push_punct(&mut self, punctuation: P) { - assert!(self.last.is_some()); + if !self.last.is_some() { + panic!("Punctuated::push_punct: Punctuated doesn't have any items"); + } + let last = self.last.take().unwrap(); self.inner.push((*last, punctuation)); } @@ -228,7 +237,9 @@ impl Punctuated { where P: Default, { - assert!(index <= self.len()); + if index > self.len() { + panic!("Punctuated::insert: index out of range"); + } if index == self.len() { self.push(value); @@ -454,7 +465,13 @@ impl FromIterator> for Punctuated { impl Extend> for Punctuated { fn extend>>(&mut self, i: I) { - assert!(self.empty_or_trailing()); + if !self.empty_or_trailing() { + panic!( + "Punctuated::extend: Punctuated is not empty or \ + does not have a trailing punctuation" + ); + } + let mut nomore = false; for pair in i { if nomore {