@@ -131,6 +131,7 @@ pub struct Translator<'db> {
131131 file_id : Option < EditionedFileId > ,
132132 pub semantics : Option < & ' db Semantics < ' db , RootDatabase > > ,
133133 source_kind : SourceKind ,
134+ library_diagnostics : bool ,
134135 pub ( crate ) macro_context_depth : usize ,
135136 diagnostic_count : usize ,
136137 /// When emitting a reconstructed built-in derive expansion, holds the span map of the
@@ -152,6 +153,7 @@ impl<'db> Translator<'db> {
152153 line_index : LineIndex ,
153154 semantic_info : Option < & FileSemanticInformation < ' db > > ,
154155 source_kind : SourceKind ,
156+ library_diagnostics : bool ,
155157 ) -> Translator < ' db > {
156158 Translator {
157159 trap,
@@ -161,11 +163,17 @@ impl<'db> Translator<'db> {
161163 file_id : semantic_info. map ( |i| i. file_id ) ,
162164 semantics : semantic_info. map ( |i| i. semantics ) ,
163165 source_kind,
166+ library_diagnostics,
164167 macro_context_depth : 0 ,
165168 diagnostic_count : 0 ,
166169 builtin_derive_span_map : None ,
167170 }
168171 }
172+
173+ pub fn detailed_diagnostics_enabled ( & self ) -> bool {
174+ self . source_kind == SourceKind :: Source || self . library_diagnostics
175+ }
176+
169177 fn location ( & self , range : TextRange ) -> Option < ( LineCol , LineCol ) > {
170178 let start = self . line_index . try_line_col ( range. start ( ) ) ?;
171179 let range_end = range. end ( ) ;
@@ -368,6 +376,9 @@ impl<'db> Translator<'db> {
368376 node : & impl ast:: AstNode ,
369377 expanded : & SyntaxNode ,
370378 ) {
379+ if !self . detailed_diagnostics_enabled ( ) {
380+ return ;
381+ }
371382 let semantics = self . semantics . as_ref ( ) . unwrap ( ) ;
372383 if let Some ( value) = semantics
373384 . hir_file_for ( expanded)
@@ -448,7 +459,7 @@ impl<'db> Translator<'db> {
448459 value,
449460 & mut self . trap . writer ,
450461 ) ;
451- } else {
462+ } else if self . detailed_diagnostics_enabled ( ) {
452463 let range = self . text_range_for_node ( mcall) ;
453464 self . emit_parse_error ( mcall, & SyntaxError :: new (
454465 format ! (
@@ -463,18 +474,20 @@ impl<'db> Translator<'db> {
463474 if self . reconstruct_format_args_expansion ( mcall, label) {
464475 return ;
465476 }
466- // let's not spam warnings if we don't have semantics, we already emitted one
467- let range = self . text_range_for_node ( mcall) ;
468- self . emit_parse_error (
469- mcall,
470- & SyntaxError :: new (
471- format ! (
472- "macro expansion failed for '{}'" ,
473- mcall. path( ) . map( |p| p. to_string( ) ) . unwrap_or_default( )
477+ if self . detailed_diagnostics_enabled ( ) {
478+ // let's not spam warnings if we don't have semantics, we already emitted one
479+ let range = self . text_range_for_node ( mcall) ;
480+ self . emit_parse_error (
481+ mcall,
482+ & SyntaxError :: new (
483+ format ! (
484+ "macro expansion failed for '{}'" ,
485+ mcall. path( ) . map( |p| p. to_string( ) ) . unwrap_or_default( )
486+ ) ,
487+ range. unwrap_or_else ( || TextRange :: empty ( TextSize :: from ( 0 ) ) ) ,
474488 ) ,
475- range. unwrap_or_else ( || TextRange :: empty ( TextSize :: from ( 0 ) ) ) ,
476- ) ,
477- ) ;
489+ ) ;
490+ }
478491 }
479492 }
480493
@@ -633,7 +646,9 @@ impl<'db> Translator<'db> {
633646 ) -> Option < Label < generated:: MacroItems > > {
634647 let semantics = self . semantics . unwrap ( ) ; // if we are here, we have semantics
635648 self . emit_macro_expansion_parse_errors ( node, & value) ;
636- if let Some ( err) = err {
649+ if let Some ( err) = err
650+ && self . detailed_diagnostics_enabled ( )
651+ {
637652 let rendered = err. render_to_string ( semantics. db ) ;
638653 self . emit_diagnostic_for_node (
639654 node,
@@ -645,7 +660,7 @@ impl<'db> Translator<'db> {
645660 }
646661 if let Some ( items) = ast:: MacroItems :: cast ( value) {
647662 self . emit_macro_items ( & items)
648- } else {
663+ } else if self . detailed_diagnostics_enabled ( ) {
649664 let message =
650665 "attribute or derive macro expansion cannot be cast to MacroItems" . to_owned ( ) ;
651666 self . emit_diagnostic_for_node (
@@ -656,6 +671,8 @@ impl<'db> Translator<'db> {
656671 message,
657672 ) ;
658673 None
674+ } else {
675+ None
659676 }
660677 }
661678
@@ -768,7 +785,9 @@ impl<'db> Translator<'db> {
768785 let ( parsed, output_span_map) =
769786 token_tree_to_syntax_node ( & output, TopEntryPoint :: MacroItems , & mut |_| edition) ;
770787 let items = ast:: MacroItems :: cast ( parsed. syntax_node ( ) ) ?;
771- if let Some ( err) = err {
788+ if let Some ( err) = err
789+ && self . detailed_diagnostics_enabled ( )
790+ {
772791 let rendered = err. render_to_string ( db) ;
773792 self . emit_diagnostic_for_node (
774793 adt,
0 commit comments