@@ -19,13 +19,9 @@ use crate::snippet::Style;
1919use crate :: {
2020 CodeSuggestion , DiagCtxtHandle , DiagMessage , ErrCode , ErrorGuaranteed , ExplicitBug , Level ,
2121 MultiSpan , StashKey , SubdiagMessage , Substitution , SubstitutionPart , SuggestionStyle ,
22+ Suggestions ,
2223} ;
2324
24- /// Error type for `DiagInner`'s `suggestions` field, indicating that
25- /// `.disable_suggestions()` was called on the `DiagInner`.
26- #[ derive( Clone , Debug , PartialEq , Eq , Hash , Encodable , Decodable ) ]
27- pub struct SuggestionsDisabled ;
28-
2925/// Simplified version of `FluentArg` that can implement `Encodable` and `Decodable`. Collection of
3026/// `DiagArg` are converted to `FluentArgs` (consuming the collection) at the start of diagnostic
3127/// emission.
@@ -296,7 +292,7 @@ pub struct DiagInner {
296292 pub code : Option < ErrCode > ,
297293 pub span : MultiSpan ,
298294 pub children : Vec < Subdiag > ,
299- pub suggestions : Result < Vec < CodeSuggestion > , SuggestionsDisabled > ,
295+ pub suggestions : Suggestions ,
300296 pub args : DiagArgMap ,
301297
302298 /// This is not used for highlighting or rendering any error message. Rather, it can be used
@@ -325,7 +321,7 @@ impl DiagInner {
325321 code : None ,
326322 span : MultiSpan :: new ( ) ,
327323 children : vec ! [ ] ,
328- suggestions : Ok ( vec ! [ ] ) ,
324+ suggestions : Suggestions :: Enabled ( vec ! [ ] ) ,
329325 args : Default :: default ( ) ,
330326 sort_span : DUMMY_SP ,
331327 is_lint : None ,
@@ -409,7 +405,7 @@ impl DiagInner {
409405 & Option < ErrCode > ,
410406 & MultiSpan ,
411407 & [ Subdiag ] ,
412- & Result < Vec < CodeSuggestion > , SuggestionsDisabled > ,
408+ & Suggestions ,
413409 Vec < ( & DiagArgName , & DiagArgValue ) > ,
414410 & Option < IsLint > ,
415411 ) {
@@ -823,16 +819,32 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
823819 self
824820 }
825821
826- /// Disallow attaching suggestions this diagnostic.
822+ /// Disallow attaching suggestions to this diagnostic.
827823 /// Any suggestions attached e.g. with the `span_suggestion_*` methods
828824 /// (before and after the call to `disable_suggestions`) will be ignored.
829825 #[ rustc_lint_diagnostics]
830826 pub fn disable_suggestions ( & mut self ) -> & mut Self {
831- self . suggestions = Err ( SuggestionsDisabled ) ;
827+ self . suggestions = Suggestions :: Disabled ;
832828 self
833829 }
834830
835- /// Helper for pushing to `self.suggestions`, if available (not disable).
831+ /// Prevent new suggestions from being added to this diagnostic.
832+ ///
833+ /// Suggestions added before the call to `.seal_suggestions()` will be preserved
834+ /// and new suggestions will be ignored.
835+ #[ rustc_lint_diagnostics]
836+ pub fn seal_suggestions ( & mut self ) -> & mut Self {
837+ if let Suggestions :: Enabled ( suggestions) = & mut self . suggestions {
838+ let suggestions_slice = std:: mem:: take ( suggestions) . into_boxed_slice ( ) ;
839+ self . suggestions = Suggestions :: Sealed ( suggestions_slice) ;
840+ }
841+ self
842+ }
843+
844+ /// Helper for pushing to `self.suggestions`.
845+ ///
846+ /// A new suggestion is added if suggestions are enabled for this diagnostic.
847+ /// Otherwise, they are ignored.
836848 #[ rustc_lint_diagnostics]
837849 fn push_suggestion ( & mut self , suggestion : CodeSuggestion ) {
838850 for subst in & suggestion. substitutions {
@@ -846,7 +858,7 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
846858 }
847859 }
848860
849- if let Ok ( suggestions) = & mut self . suggestions {
861+ if let Suggestions :: Enabled ( suggestions) = & mut self . suggestions {
850862 suggestions. push ( suggestion) ;
851863 }
852864 }
0 commit comments