@@ -726,31 +726,58 @@ fn check_if_attr_is_complete(source: &str, edition: Edition) -> bool {
726726 // Empty content so nothing to check in here...
727727 return true ;
728728 }
729- rustc_span:: create_session_if_not_set_then ( edition, |_| {
730- let filename = FileName :: anon_source_code ( source) ;
731- let sess = ParseSess :: with_silent_emitter ( None ) ;
732- let mut parser = match maybe_new_parser_from_source_str ( & sess, filename, source. to_owned ( ) )
733- {
734- Ok ( p) => p,
735- Err ( _) => {
736- debug ! ( "Cannot build a parser to check mod attr so skipping..." ) ;
737- return true ;
729+ rustc_driver:: catch_fatal_errors ( || {
730+ rustc_span:: create_session_if_not_set_then ( edition, |_| {
731+ use rustc_errors:: emitter:: EmitterWriter ;
732+ use rustc_errors:: Handler ;
733+ use rustc_span:: source_map:: FilePathMapping ;
734+
735+ let filename = FileName :: anon_source_code ( source) ;
736+ // Any errors in parsing should also appear when the doctest is compiled for real, so just
737+ // send all the errors that librustc_ast emits directly into a `Sink` instead of stderr.
738+ let sm = Lrc :: new ( SourceMap :: new ( FilePathMapping :: empty ( ) ) ) ;
739+ let fallback_bundle =
740+ rustc_errors:: fallback_fluent_bundle ( rustc_errors:: DEFAULT_LOCALE_RESOURCES , false ) ;
741+
742+ let emitter = EmitterWriter :: new (
743+ box io:: sink ( ) ,
744+ None ,
745+ None ,
746+ fallback_bundle,
747+ false ,
748+ false ,
749+ false ,
750+ None ,
751+ false ,
752+ ) ;
753+
754+ let handler = Handler :: with_emitter ( false , None , box emitter) ;
755+ let sess = ParseSess :: with_span_handler ( handler, sm) ;
756+ let mut parser =
757+ match maybe_new_parser_from_source_str ( & sess, filename, source. to_owned ( ) ) {
758+ Ok ( p) => p,
759+ Err ( _) => {
760+ debug ! ( "Cannot build a parser to check mod attr so skipping..." ) ;
761+ return true ;
762+ }
763+ } ;
764+ // If a parsing error happened, it's very likely that the attribute is incomplete.
765+ if let Err ( e) = parser. parse_attribute ( InnerAttrPolicy :: Permitted ) {
766+ e. cancel ( ) ;
767+ return false ;
738768 }
739- } ;
740- // If a parsing error happened, it's very likely that the attribute is incomplete.
741- if parser. parse_attribute ( InnerAttrPolicy :: Permitted ) . is_err ( ) {
742- return false ;
743- }
744- // We now check if there is an unclosed delimiter for the attribute. To do so, we look at
745- // the `unclosed_delims` and see if the opening square bracket was closed.
746- parser
747- . unclosed_delims ( )
748- . get ( 0 )
749- . map ( |unclosed| {
750- unclosed. unclosed_span . map ( |s| s. lo ( ) ) . unwrap_or ( BytePos ( 0 ) ) != BytePos ( 2 )
751- } )
752- . unwrap_or ( true )
769+ // We now check if there is an unclosed delimiter for the attribute. To do so, we look at
770+ // the `unclosed_delims` and see if the opening square bracket was closed.
771+ parser
772+ . unclosed_delims ( )
773+ . get ( 0 )
774+ . map ( |unclosed| {
775+ unclosed. unclosed_span . map ( |s| s. lo ( ) ) . unwrap_or ( BytePos ( 0 ) ) != BytePos ( 2 )
776+ } )
777+ . unwrap_or ( true )
778+ } )
753779 } )
780+ . unwrap_or ( false )
754781}
755782
756783fn partition_source ( s : & str , edition : Edition ) -> ( String , String , String ) {
0 commit comments