From 8a75e4581c8f05ef74dbaccf272e8391cf0ec579 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 | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/punctuated.rs b/src/punctuated.rs index 4d3496976c..b86cf8d160 100644 --- a/src/punctuated.rs +++ b/src/punctuated.rs @@ -162,7 +162,12 @@ 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()); + assert!( + self.empty_or_trailing(), + "Punctuated::push_value: Punctuated is not empty or \ + does not have a trailing punctuation" + ); + self.last = Some(Box::new(value)); } @@ -174,7 +179,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()); + assert!( + self.last.is_some(), + "Punctuated::push_punct: Punctuated doesn't have any items" + ); let last = self.last.take().unwrap(); self.inner.push((*last, punctuation)); } @@ -228,7 +236,10 @@ impl Punctuated { where P: Default, { - assert!(index <= self.len()); + assert!( + index <= self.len(), + "Punctuated::insert: index out of range" + ); if index == self.len() { self.push(value); @@ -454,7 +465,12 @@ impl FromIterator> for Punctuated { impl Extend> for Punctuated { fn extend>>(&mut self, i: I) { - assert!(self.empty_or_trailing()); + assert!( + self.empty_or_trailing(), + "Punctuated::extend: Punctuated is not empty or \ + does not have a trailing punctuation" + ); + let mut nomore = false; for pair in i { if nomore {