Skip to content

Commit d21d5e4

Browse files
bors[bot]matklad
andauthored
Merge #5800
5800: Speedup ty tests r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 0df9ece + aad911f commit d21d5e4

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

crates/hir_ty/src/tests.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mod method_resolution;
88
mod macros;
99
mod display_source_code;
1010

11-
use std::sync::Arc;
11+
use std::{env, sync::Arc};
1212

1313
use base_db::{fixture::WithFixture, FileRange, SourceDatabase, SourceDatabaseExt};
1414
use expect::Expect;
@@ -22,12 +22,14 @@ use hir_def::{
2222
AssocItemId, DefWithBodyId, LocalModuleId, Lookup, ModuleDefId,
2323
};
2424
use hir_expand::{db::AstDatabase, InFile};
25-
use stdx::format_to;
25+
use stdx::{format_to, RacyFlag};
2626
use syntax::{
2727
algo,
2828
ast::{self, AstNode},
2929
SyntaxNode,
3030
};
31+
use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry};
32+
use tracing_tree::HierarchicalLayer;
3133

3234
use crate::{
3335
db::HirDatabase, display::HirDisplay, infer::TypeMismatch, test_db::TestDB, InferenceResult, Ty,
@@ -37,17 +39,20 @@ use crate::{
3739
// against snapshots of the expected results using expect. Use
3840
// `env UPDATE_EXPECT=1 cargo test -p hir_ty` to update the snapshots.
3941

40-
fn setup_tracing() -> tracing::subscriber::DefaultGuard {
41-
use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry};
42-
use tracing_tree::HierarchicalLayer;
42+
fn setup_tracing() -> Option<tracing::subscriber::DefaultGuard> {
43+
static ENABLE: RacyFlag = RacyFlag::new();
44+
if !ENABLE.get(|| env::var("CHALK_DEBUG").is_ok()) {
45+
return None;
46+
}
47+
4348
let filter = EnvFilter::from_env("CHALK_DEBUG");
4449
let layer = HierarchicalLayer::default()
4550
.with_indent_lines(true)
4651
.with_ansi(false)
4752
.with_indent_amount(2)
4853
.with_writer(std::io::stderr);
4954
let subscriber = Registry::default().with(filter).with(layer);
50-
tracing::subscriber::set_default(subscriber)
55+
Some(tracing::subscriber::set_default(subscriber))
5156
}
5257

5358
fn check_types(ra_fixture: &str) {

crates/stdx/src/lib.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
//! Missing batteries for standard libraries.
2-
use std::time::Instant;
2+
use std::{
3+
sync::atomic::{AtomicUsize, Ordering},
4+
time::Instant,
5+
};
36

47
mod macros;
58

@@ -134,6 +137,31 @@ where
134137
left
135138
}
136139

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+
137165
#[cfg(test)]
138166
mod tests {
139167
use super::*;

0 commit comments

Comments
 (0)