summaryrefslogtreecommitdiff
path: root/src/syntax/span.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/syntax/span.rs')
-rw-r--r--src/syntax/span.rs29
1 files changed, 28 insertions, 1 deletions
diff --git a/src/syntax/span.rs b/src/syntax/span.rs
index c93f5c1e..923d13c2 100644
--- a/src/syntax/span.rs
+++ b/src/syntax/span.rs
@@ -3,9 +3,23 @@
use std::fmt::{self, Debug, Formatter};
use std::ops::{Add, Sub};
+#[cfg(test)]
+use std::cell::Cell;
+
#[cfg(feature = "serialize")]
use serde::Serialize;
+#[cfg(test)]
+thread_local! {
+ static CMP_SPANS: Cell<bool> = Cell::new(true);
+}
+
+/// When set to `false` comparisons with `PartialEq` ignore spans.
+#[cfg(test)]
+pub(crate) fn set_cmp(cmp: bool) {
+ CMP_SPANS.with(|cell| cell.set(cmp));
+}
+
/// Span offsetting.
pub trait Offset {
/// Offset all spans contained in `Self` by the given position.
@@ -85,7 +99,7 @@ impl<T: Debug> Debug for Spanned<T> {
}
/// Locates a slice of source code.
-#[derive(Copy, Clone, Eq, PartialEq, Hash)]
+#[derive(Copy, Clone, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize))]
pub struct Span {
/// The inclusive start position.
@@ -131,6 +145,19 @@ impl Offset for Span {
}
}
+impl Eq for Span {}
+
+impl PartialEq for Span {
+ fn eq(&self, other: &Self) -> bool {
+ #[cfg(test)]
+ if !CMP_SPANS.with(Cell::get) {
+ return true;
+ }
+
+ self.start == other.start && self.end == other.end
+ }
+}
+
impl Debug for Span {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "<{:?}-{:?}>", self.start, self.end)