aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/hir_ty/src/tests.rs17
-rw-r--r--crates/stdx/src/lib.rs30
2 files changed, 40 insertions, 7 deletions
diff --git a/crates/hir_ty/src/tests.rs b/crates/hir_ty/src/tests.rs
index c953925ec..91c9d38c5 100644
--- a/crates/hir_ty/src/tests.rs
+++ b/crates/hir_ty/src/tests.rs
@@ -8,7 +8,7 @@ mod method_resolution;
8mod macros; 8mod macros;
9mod display_source_code; 9mod display_source_code;
10 10
11use std::sync::Arc; 11use std::{env, sync::Arc};
12 12
13use base_db::{fixture::WithFixture, FileRange, SourceDatabase, SourceDatabaseExt}; 13use base_db::{fixture::WithFixture, FileRange, SourceDatabase, SourceDatabaseExt};
14use expect::Expect; 14use expect::Expect;
@@ -22,12 +22,14 @@ use hir_def::{
22 AssocItemId, DefWithBodyId, LocalModuleId, Lookup, ModuleDefId, 22 AssocItemId, DefWithBodyId, LocalModuleId, Lookup, ModuleDefId,
23}; 23};
24use hir_expand::{db::AstDatabase, InFile}; 24use hir_expand::{db::AstDatabase, InFile};
25use stdx::format_to; 25use stdx::{format_to, RacyFlag};
26use syntax::{ 26use syntax::{
27 algo, 27 algo,
28 ast::{self, AstNode}, 28 ast::{self, AstNode},
29 SyntaxNode, 29 SyntaxNode,
30}; 30};
31use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry};
32use tracing_tree::HierarchicalLayer;
31 33
32use crate::{ 34use crate::{
33 db::HirDatabase, display::HirDisplay, infer::TypeMismatch, test_db::TestDB, InferenceResult, Ty, 35 db::HirDatabase, display::HirDisplay, infer::TypeMismatch, test_db::TestDB, InferenceResult, Ty,
@@ -37,9 +39,12 @@ use crate::{
37// against snapshots of the expected results using expect. Use 39// against snapshots of the expected results using expect. Use
38// `env UPDATE_EXPECT=1 cargo test -p hir_ty` to update the snapshots. 40// `env UPDATE_EXPECT=1 cargo test -p hir_ty` to update the snapshots.
39 41
40fn setup_tracing() -> tracing::subscriber::DefaultGuard { 42fn setup_tracing() -> Option<tracing::subscriber::DefaultGuard> {
41 use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry}; 43 static ENABLE: RacyFlag = RacyFlag::new();
42 use tracing_tree::HierarchicalLayer; 44 if !ENABLE.get(|| env::var("CHALK_DEBUG").is_ok()) {
45 return None;
46 }
47
43 let filter = EnvFilter::from_env("CHALK_DEBUG"); 48 let filter = EnvFilter::from_env("CHALK_DEBUG");
44 let layer = HierarchicalLayer::default() 49 let layer = HierarchicalLayer::default()
45 .with_indent_lines(true) 50 .with_indent_lines(true)
@@ -47,7 +52,7 @@ fn setup_tracing() -> tracing::subscriber::DefaultGuard {
47 .with_indent_amount(2) 52 .with_indent_amount(2)
48 .with_writer(std::io::stderr); 53 .with_writer(std::io::stderr);
49 let subscriber = Registry::default().with(filter).with(layer); 54 let subscriber = Registry::default().with(filter).with(layer);
50 tracing::subscriber::set_default(subscriber) 55 Some(tracing::subscriber::set_default(subscriber))
51} 56}
52 57
53fn check_types(ra_fixture: &str) { 58fn check_types(ra_fixture: &str) {
diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs
index 265d19288..5d60f0219 100644
--- a/crates/stdx/src/lib.rs
+++ b/crates/stdx/src/lib.rs
@@ -1,5 +1,8 @@
1//! Missing batteries for standard libraries. 1//! Missing batteries for standard libraries.
2use std::time::Instant; 2use std::{
3 sync::atomic::{AtomicUsize, Ordering},
4 time::Instant,
5};
3 6
4mod macros; 7mod macros;
5 8
@@ -134,6 +137,31 @@ where
134 left 137 left
135} 138}
136 139
140pub struct RacyFlag(AtomicUsize);
141
142impl RacyFlag {
143 pub const fn new() -> RacyFlag {
144 RacyFlag(AtomicUsize::new(0))
145 }
146
147 pub fn get(&self, init: impl FnMut() -> bool) -> bool {
148 let mut init = Some(init);
149 self.get_impl(&mut || init.take().map_or(false, |mut f| f()))
150 }
151
152 fn get_impl(&self, init: &mut dyn FnMut() -> bool) -> bool {
153 match self.0.load(Ordering::Relaxed) {
154 0 => false,
155 1 => true,
156 _ => {
157 let res = init();
158 self.0.store(if res { 1 } else { 0 }, Ordering::Relaxed);
159 res
160 }
161 }
162 }
163}
164
137#[cfg(test)] 165#[cfg(test)]
138mod tests { 166mod tests {
139 use super::*; 167 use super::*;