@@ -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 ,
@@ -429,7 +425,7 @@ impl DiagInner {
429
425
& Option < ErrCode > ,
430
426
& MultiSpan ,
431
427
& [ Subdiag ] ,
432
- & Result < Vec < CodeSuggestion > , SuggestionsDisabled > ,
428
+ & Suggestions ,
433
429
Vec < ( & DiagArgName , & DiagArgValue ) > ,
434
430
& Option < IsLint > ,
435
431
) {
@@ -843,16 +839,32 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
843
839
self
844
840
}
845
841
846
- /// Disallow attaching suggestions this diagnostic.
842
+ /// Disallow attaching suggestions to this diagnostic.
847
843
/// Any suggestions attached e.g. with the `span_suggestion_*` methods
848
844
/// (before and after the call to `disable_suggestions`) will be ignored.
849
845
#[ rustc_lint_diagnostics]
850
846
pub fn disable_suggestions ( & mut self ) -> & mut Self {
851
- self . suggestions = Err ( SuggestionsDisabled ) ;
847
+ self . suggestions = Suggestions :: Disabled ;
852
848
self
853
849
}
854
850
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.
856
868
#[ rustc_lint_diagnostics]
857
869
fn push_suggestion ( & mut self , suggestion : CodeSuggestion ) {
858
870
for subst in & suggestion. substitutions {
@@ -866,7 +878,7 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
866
878
}
867
879
}
868
880
869
- if let Ok ( suggestions) = & mut self . suggestions {
881
+ if let Suggestions :: Enabled ( suggestions) = & mut self . suggestions {
870
882
suggestions. push ( suggestion) ;
871
883
}
872
884
}
0 commit comments