@@ -19,13 +19,9 @@ use crate::snippet::Style;
19
19
use crate :: {
20
20
CodeSuggestion , DiagCtxtHandle , DiagMessage , ErrCode , ErrorGuaranteed , ExplicitBug , Level ,
21
21
MultiSpan , StashKey , SubdiagMessage , Substitution , SubstitutionPart , SuggestionStyle ,
22
+ Suggestions ,
22
23
} ;
23
24
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
-
29
25
/// Simplified version of `FluentArg` that can implement `Encodable` and `Decodable`. Collection of
30
26
/// `DiagArg` are converted to `FluentArgs` (consuming the collection) at the start of diagnostic
31
27
/// emission.
@@ -296,7 +292,7 @@ pub struct DiagInner {
296
292
pub code : Option < ErrCode > ,
297
293
pub span : MultiSpan ,
298
294
pub children : Vec < Subdiag > ,
299
- pub suggestions : Result < Vec < CodeSuggestion > , SuggestionsDisabled > ,
295
+ pub suggestions : Suggestions ,
300
296
pub args : DiagArgMap ,
301
297
302
298
/// This is not used for highlighting or rendering any error message. Rather, it can be used
@@ -325,7 +321,7 @@ impl DiagInner {
325
321
code : None ,
326
322
span : MultiSpan :: new ( ) ,
327
323
children : vec ! [ ] ,
328
- suggestions : Ok ( vec ! [ ] ) ,
324
+ suggestions : Suggestions :: Enabled ( vec ! [ ] ) ,
329
325
args : Default :: default ( ) ,
330
326
sort_span : DUMMY_SP ,
331
327
is_lint : None ,
@@ -409,7 +405,7 @@ impl DiagInner {
409
405
& Option < ErrCode > ,
410
406
& MultiSpan ,
411
407
& [ Subdiag ] ,
412
- & Result < Vec < CodeSuggestion > , SuggestionsDisabled > ,
408
+ & Suggestions ,
413
409
Vec < ( & DiagArgName , & DiagArgValue ) > ,
414
410
& Option < IsLint > ,
415
411
) {
@@ -823,16 +819,32 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
823
819
self
824
820
}
825
821
826
- /// Disallow attaching suggestions this diagnostic.
822
+ /// Disallow attaching suggestions to this diagnostic.
827
823
/// Any suggestions attached e.g. with the `span_suggestion_*` methods
828
824
/// (before and after the call to `disable_suggestions`) will be ignored.
829
825
#[ rustc_lint_diagnostics]
830
826
pub fn disable_suggestions ( & mut self ) -> & mut Self {
831
- self . suggestions = Err ( SuggestionsDisabled ) ;
827
+ self . suggestions = Suggestions :: Disabled ;
832
828
self
833
829
}
834
830
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.
836
848
#[ rustc_lint_diagnostics]
837
849
fn push_suggestion ( & mut self , suggestion : CodeSuggestion ) {
838
850
for subst in & suggestion. substitutions {
@@ -846,7 +858,7 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
846
858
}
847
859
}
848
860
849
- if let Ok ( suggestions) = & mut self . suggestions {
861
+ if let Suggestions :: Enabled ( suggestions) = & mut self . suggestions {
850
862
suggestions. push ( suggestion) ;
851
863
}
852
864
}
0 commit comments