@@ -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 {
780779mod 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;
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]
0 commit comments