From ac788f2082711161ec8208eede04d9a2bae02241 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Fri, 22 Jan 2021 17:16:42 +0100 Subject: =?UTF-8?q?Many=20more=20expressions=20=F0=9F=A5=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Boolean, equality, comparison and assignment expression parsing and evaluation. --- src/syntax/span.rs | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) (limited to 'src/syntax/span.rs') diff --git a/src/syntax/span.rs b/src/syntax/span.rs index fbde35af..21fa9ab8 100644 --- a/src/syntax/span.rs +++ b/src/syntax/span.rs @@ -1,10 +1,7 @@ +use std::cell::Cell; use std::fmt::{self, Debug, Display, Formatter}; use std::ops::Range; -#[cfg(test)] -use std::cell::Cell; - -#[cfg(test)] thread_local! { static CMP_SPANS: Cell = Cell::new(true); } @@ -148,10 +145,27 @@ impl Span { self.start.to_usize() .. self.end.to_usize() } + /// Run some code with span comparisons disabled. + pub fn without_cmp(f: F) -> T + where + F: FnOnce() -> T, + { + let prev = Self::cmp(); + Self::set_cmp(false); + let val = f(); + Self::set_cmp(prev); + val + } + + /// Whether spans will currently be compared. + fn cmp() -> bool { + CMP_SPANS.with(Cell::get) + } + + /// Whether spans should be compared. + /// /// When set to `false` comparisons with `PartialEq` ignore spans. - #[cfg(test)] - #[allow(unused)] - pub(crate) fn set_cmp(cmp: bool) { + fn set_cmp(cmp: bool) { CMP_SPANS.with(|cell| cell.set(cmp)); } } @@ -169,12 +183,7 @@ 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 + !Self::cmp() || (self.start == other.start && self.end == other.end) } } -- cgit v1.2.3