From 84f30fb73518ca23cbc728b1bf414e80b344412a Mon Sep 17 00:00:00 2001 From: Laurenz Date: Sat, 15 Aug 2020 15:01:55 +0200 Subject: =?UTF-8?q?Remove=20SpanlessEq=20=F0=9F=8E=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The new solution is slightly hacky, but way more flexible. All types that implement PartialEq can now be compared spanlessly in tests by manipulating a thread-local boolean that is read in Span's PartialEq implementation. --- src/syntax/span.rs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'src/syntax/span.rs') 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 = 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 Debug for Spanned { } /// 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) -- cgit v1.2.3