diff options
author | Aleksey Kladov <[email protected]> | 2020-08-18 16:20:10 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-08-18 16:20:57 +0100 |
commit | aad911fb0cd772e809270be3e0a526d4738b9dd5 (patch) | |
tree | 071693ea923db713b5e590a5e7cecfa2fff04185 /crates | |
parent | e81c310b6224946318b8e6af56a55021716ea9b5 (diff) |
Speedup ty tests
Closes #5792
Diffstat (limited to 'crates')
-rw-r--r-- | crates/hir_ty/src/tests.rs | 17 | ||||
-rw-r--r-- | crates/stdx/src/lib.rs | 30 |
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; | |||
8 | mod macros; | 8 | mod macros; |
9 | mod display_source_code; | 9 | mod display_source_code; |
10 | 10 | ||
11 | use std::sync::Arc; | 11 | use std::{env, sync::Arc}; |
12 | 12 | ||
13 | use base_db::{fixture::WithFixture, FileRange, SourceDatabase, SourceDatabaseExt}; | 13 | use base_db::{fixture::WithFixture, FileRange, SourceDatabase, SourceDatabaseExt}; |
14 | use expect::Expect; | 14 | use expect::Expect; |
@@ -22,12 +22,14 @@ use hir_def::{ | |||
22 | AssocItemId, DefWithBodyId, LocalModuleId, Lookup, ModuleDefId, | 22 | AssocItemId, DefWithBodyId, LocalModuleId, Lookup, ModuleDefId, |
23 | }; | 23 | }; |
24 | use hir_expand::{db::AstDatabase, InFile}; | 24 | use hir_expand::{db::AstDatabase, InFile}; |
25 | use stdx::format_to; | 25 | use stdx::{format_to, RacyFlag}; |
26 | use syntax::{ | 26 | use syntax::{ |
27 | algo, | 27 | algo, |
28 | ast::{self, AstNode}, | 28 | ast::{self, AstNode}, |
29 | SyntaxNode, | 29 | SyntaxNode, |
30 | }; | 30 | }; |
31 | use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry}; | ||
32 | use tracing_tree::HierarchicalLayer; | ||
31 | 33 | ||
32 | use crate::{ | 34 | use 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 | ||
40 | fn setup_tracing() -> tracing::subscriber::DefaultGuard { | 42 | fn 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 | ||
53 | fn check_types(ra_fixture: &str) { | 58 | fn 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. |
2 | use std::time::Instant; | 2 | use std::{ |
3 | sync::atomic::{AtomicUsize, Ordering}, | ||
4 | time::Instant, | ||
5 | }; | ||
3 | 6 | ||
4 | mod macros; | 7 | mod macros; |
5 | 8 | ||
@@ -134,6 +137,31 @@ where | |||
134 | left | 137 | left |
135 | } | 138 | } |
136 | 139 | ||
140 | pub struct RacyFlag(AtomicUsize); | ||
141 | |||
142 | impl 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)] |
138 | mod tests { | 166 | mod tests { |
139 | use super::*; | 167 | use super::*; |