Skip to content

Commit 7bfa62e

Browse files
update based on comment
1 parent ff6c12b commit 7bfa62e

File tree

2 files changed

+30
-41
lines changed

2 files changed

+30
-41
lines changed

crates/ide/src/runnables.rs

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub enum RunnableKind {
5757
TestMod { path: String },
5858
Test { test_id: TestId, attr: TestAttr },
5959
Bench { test_id: TestId },
60-
DocTest { test_id: TestId, has_compile_fail: bool },
60+
DocTest { test_id: TestId },
6161
Bin,
6262
}
6363

@@ -404,7 +404,9 @@ pub(crate) fn runnable_impl(
404404
let display_target = def.module(sema.db).krate(sema.db).to_display_target(sema.db);
405405
let edition = display_target.edition;
406406
let attrs = def.attrs(sema.db);
407-
let doc_test_info = runnable_doc_test_info(sema.db, &attrs)?;
407+
if !has_runnable_doc_test(sema.db, &attrs) {
408+
return None;
409+
}
408410
let cfg = attrs.cfgs(sema.db).cloned();
409411
let nav = def.try_to_nav(sema)?.call_site();
410412
let ty = def.self_ty(sema.db);
@@ -427,7 +429,7 @@ pub(crate) fn runnable_impl(
427429
Some(Runnable {
428430
use_name_in_title: false,
429431
nav,
430-
kind: RunnableKind::DocTest { test_id, has_compile_fail: doc_test_info.has_compile_fail },
432+
kind: RunnableKind::DocTest { test_id },
431433
cfg,
432434
update_test,
433435
})
@@ -506,7 +508,9 @@ fn module_def_doctest(sema: &Semantics<'_, RootDatabase>, def: Definition) -> Op
506508
let display_target = krate
507509
.unwrap_or_else(|| (*db.all_crates().last().expect("no crate graph present")).into())
508510
.to_display_target(db);
509-
let doc_test_info = runnable_doc_test_info(db, &attrs)?;
511+
if !has_runnable_doc_test(db, &attrs) {
512+
return None;
513+
}
510514
let def_name = def.name(db)?;
511515
let path = (|| {
512516
let mut path = String::new();
@@ -547,7 +551,7 @@ fn module_def_doctest(sema: &Semantics<'_, RootDatabase>, def: Definition) -> Op
547551
let res = Runnable {
548552
use_name_in_title: false,
549553
nav,
550-
kind: RunnableKind::DocTest { test_id, has_compile_fail: doc_test_info.has_compile_fail },
554+
kind: RunnableKind::DocTest { test_id },
551555
cfg: attrs.cfgs(db).cloned(),
552556
update_test: UpdateTest::default(),
553557
};
@@ -565,23 +569,18 @@ impl TestAttr {
565569
}
566570
}
567571

568-
#[derive(Default, Clone, Copy)]
569-
struct RunnableDocTestInfo {
570-
has_compile_fail: bool,
571-
}
572-
573-
fn runnable_doc_test_info(
574-
db: &RootDatabase,
575-
attrs: &hir::AttrsWithOwner,
576-
) -> Option<RunnableDocTestInfo> {
572+
fn has_runnable_doc_test(db: &RootDatabase, attrs: &hir::AttrsWithOwner) -> bool {
577573
const RUSTDOC_FENCES: [&str; 2] = ["```", "~~~"];
578574
const RUSTDOC_CODE_BLOCK_ATTRIBUTES_RUNNABLE: &[&str] =
579575
&["", "rust", "should_panic", "edition2015", "edition2018", "edition2021", "edition2024"];
580576

581-
let doc = attrs.hir_docs(db)?;
582-
let mut info = RunnableDocTestInfo::default();
577+
let doc = match attrs.hir_docs(db) {
578+
Some(doc) => doc,
579+
None => return false,
580+
};
583581
let mut in_code_block = false;
584582
let mut runnable_found = false;
583+
let mut has_compile_fail = false;
585584

586585
for line in doc.docs().lines() {
587586
let trimmed_line = line.trim_start();
@@ -606,7 +605,7 @@ fn runnable_doc_test_info(
606605
if attr.eq_ignore_ascii_case("compile_fail") {
607606
block_has_compile_fail = true;
608607
block_runnable = false;
609-
continue;
608+
break;
610609
}
611610

612611
if !RUSTDOC_CODE_BLOCK_ATTRIBUTES_RUNNABLE
@@ -617,12 +616,12 @@ fn runnable_doc_test_info(
617616
}
618617
}
619618

620-
info.has_compile_fail |= block_has_compile_fail;
619+
has_compile_fail |= block_has_compile_fail;
621620
runnable_found |= block_runnable;
622621
}
623622
}
624623

625-
runnable_found.then_some(info)
624+
runnable_found && !has_compile_fail
626625
}
627626

628627
// We could create runnables for modules with number_of_test_submodules > 0,
@@ -780,7 +779,6 @@ impl UpdateTest {
780779
mod tests {
781780
use expect_test::{Expect, expect};
782781

783-
use super::RunnableKind;
784782
use crate::fixture;
785783

786784
fn check(#[rust_analyzer::rust_fixture] ra_fixture: &str, expect: Expect) {
@@ -970,11 +968,13 @@ impl Test for StructWithRunnable {}
970968
}
971969

972970
#[test]
973-
fn doc_test_runnable_tracks_compile_fail_blocks() {
974-
let (analysis, position) = fixture::position(
971+
fn doc_test_with_compile_fail_blocks_is_skipped() {
972+
check(
975973
r#"
976974
//- /lib.rs
977975
$0
976+
fn main() {}
977+
978978
/// ```compile_fail
979979
/// let x = 5;
980980
/// x += 1;
@@ -984,22 +984,14 @@ $0
984984
/// let x = 5;
985985
/// x + 1;
986986
/// ```
987-
fn add(left: u64, right: u64) -> u64 {
988-
left + right
989-
}
987+
fn add() {}
990988
"#,
989+
expect![[r#"
990+
[
991+
"(Bin, NavigationTarget { file_id: FileId(0), full_range: 1..13, focus_range: 4..8, name: \"main\", kind: Function })",
992+
]
993+
"#]],
991994
);
992-
993-
let runnables = analysis.runnables(position.file_id).unwrap();
994-
let doc_test = runnables
995-
.into_iter()
996-
.find(|runnable| matches!(runnable.kind, RunnableKind::DocTest { .. }))
997-
.expect("expected doctest runnable");
998-
999-
match doc_test.kind {
1000-
RunnableKind::DocTest { has_compile_fail, .. } => assert!(has_compile_fail),
1001-
_ => panic!("expected doctest runnable"),
1002-
}
1003995
}
1004996

1005997
#[test]

crates/rust-analyzer/src/target_spec.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl CargoTargetSpec {
116116
cfg: &Option<CfgExpr>,
117117
) -> (Vec<String>, Vec<String>) {
118118
let config = snap.config.runnables(None);
119-
let mut extra_test_binary_args = config.extra_test_binary_args;
119+
let extra_test_binary_args = config.extra_test_binary_args;
120120

121121
let mut cargo_args = Vec::new();
122122
let mut executable_args = Vec::new();
@@ -146,13 +146,10 @@ impl CargoTargetSpec {
146146
}
147147
executable_args.extend(extra_test_binary_args);
148148
}
149-
RunnableKind::DocTest { test_id, has_compile_fail } => {
149+
RunnableKind::DocTest { test_id } => {
150150
cargo_args.push("test".to_owned());
151151
cargo_args.push("--doc".to_owned());
152152
executable_args.push(test_id.to_string());
153-
if *has_compile_fail {
154-
extra_test_binary_args.retain(|arg| arg != "--nocapture");
155-
}
156153
executable_args.extend(extra_test_binary_args);
157154
}
158155
RunnableKind::Bin => {

0 commit comments

Comments
 (0)