@@ -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 ,
@@ -429,7 +425,7 @@ impl DiagInner {
429425 & Option < ErrCode > ,
430426 & MultiSpan ,
431427 & [ Subdiag ] ,
432- & Result < Vec < CodeSuggestion > , SuggestionsDisabled > ,
428+ & Suggestions ,
433429 Vec < ( & DiagArgName , & DiagArgValue ) > ,
434430 & Option < IsLint > ,
435431 ) {
@@ -843,16 +839,32 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
843839 self
844840 }
845841
846- /// Disallow attaching suggestions this diagnostic.
842+ /// Disallow attaching suggestions to this diagnostic.
847843 /// Any suggestions attached e.g. with the `span_suggestion_*` methods
848844 /// (before and after the call to `disable_suggestions`) will be ignored.
849845 #[ rustc_lint_diagnostics]
850846 pub fn disable_suggestions ( & mut self ) -> & mut Self {
851- self . suggestions = Err ( SuggestionsDisabled ) ;
847+ self . suggestions = Suggestions :: Disabled ;
852848 self
853849 }
854850
855- /// Helper for pushing to `self.suggestions`, if available (not disable).
851+ /// Prevent new suggestions from being added to this diagnostic.
852+ ///
853+ /// Suggestions added before the call to `.seal_suggestions()` will be preserved
854+ /// and new suggestions will be ignored.
855+ #[ rustc_lint_diagnostics]
856+ pub fn seal_suggestions ( & mut self ) -> & mut Self {
857+ if let Suggestions :: Enabled ( suggestions) = & mut self . suggestions {
858+ let suggestions_slice = std:: mem:: take ( suggestions) . into_boxed_slice ( ) ;
859+ self . suggestions = Suggestions :: Sealed ( suggestions_slice) ;
860+ }
861+ self
862+ }
863+
864+ /// Helper for pushing to `self.suggestions`.
865+ ///
866+ /// A new suggestion is added if suggestions are enabled for this diagnostic.
867+ /// Otherwise, they are ignored.
856868 #[ rustc_lint_diagnostics]
857869 fn push_suggestion ( & mut self , suggestion : CodeSuggestion ) {
858870 for subst in & suggestion. substitutions {
@@ -866,7 +878,7 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
866878 }
867879 }
868880
869- if let Ok ( suggestions) = & mut self . suggestions {
881+ if let Suggestions :: Enabled ( suggestions) = & mut self . suggestions {
870882 suggestions. push ( suggestion) ;
871883 }
872884 }
0 commit comments