Rollup merge of #130116 - veera-sivarajan:freeze-suggestions, r=chenyukang
Implement a Method to Seal `DiagInner`'s Suggestions This PR adds a method on `DiagInner` called `.seal_suggestions()` to prevent new suggestions from being added while preserving existing suggestions. This is useful because currently there is no way to prevent new suggestions from being added to a diagnostic. `.disable_suggestions()` is the closest but it gets rid of all suggestions before and after the call. Therefore, `.seal_suggestions()` can be used when, for example, misspelled keyword is detected and reported. In such cases, we may want to prevent other suggestions from being added to the diagnostic, as they would likely be meaningless once the misspelled keyword is identified. For context: https://github.com/rust-lang/rust/pull/129899#discussion_r1741307132 To store an additional state, the type of the `suggestions` field in `DiagInner` was changed into a three variant enum. While this change affects files across different crates, care was taken to preserve the existing code's semantics. This is validated by the fact that all UI tests pass without any modifications. r? chenyukang
This commit is contained in:
commit
09b255d3d4
13 changed files with 102 additions and 53 deletions
|
@ -17,7 +17,7 @@ use rustc_ast::visit::{visit_opt, walk_list, AssocCtxt, BoundKind, FnCtxt, FnKin
|
|||
use rustc_ast::*;
|
||||
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
|
||||
use rustc_errors::codes::*;
|
||||
use rustc_errors::{Applicability, DiagArgValue, IntoDiagArg, StashKey};
|
||||
use rustc_errors::{Applicability, DiagArgValue, IntoDiagArg, StashKey, Suggestions};
|
||||
use rustc_hir::def::Namespace::{self, *};
|
||||
use rustc_hir::def::{self, CtorKind, DefKind, LifetimeRes, NonMacroAttrKind, PartialRes, PerNS};
|
||||
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID, LOCAL_CRATE};
|
||||
|
@ -4085,17 +4085,23 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
|
|||
err.sort_span = parent_err.sort_span;
|
||||
err.is_lint = parent_err.is_lint.clone();
|
||||
|
||||
// merge the parent's suggestions with the typo suggestions
|
||||
fn append_result<T, E>(res1: &mut Result<Vec<T>, E>, res2: Result<Vec<T>, E>) {
|
||||
match res1 {
|
||||
Ok(vec1) => match res2 {
|
||||
Ok(mut vec2) => vec1.append(&mut vec2),
|
||||
Err(e) => *res1 = Err(e),
|
||||
},
|
||||
Err(_) => (),
|
||||
};
|
||||
// merge the parent_err's suggestions with the typo (err's) suggestions
|
||||
match &mut err.suggestions {
|
||||
Suggestions::Enabled(typo_suggestions) => match &mut parent_err.suggestions {
|
||||
Suggestions::Enabled(parent_suggestions) => {
|
||||
// If both suggestions are enabled, append parent_err's suggestions to err's suggestions.
|
||||
typo_suggestions.append(parent_suggestions)
|
||||
}
|
||||
Suggestions::Sealed(_) | Suggestions::Disabled => {
|
||||
// If the parent's suggestions are either sealed or disabled, it signifies that
|
||||
// new suggestions cannot be added or removed from the diagnostic. Therefore,
|
||||
// we assign both types of suggestions to err's suggestions and discard the
|
||||
// existing suggestions in err.
|
||||
err.suggestions = std::mem::take(&mut parent_err.suggestions);
|
||||
}
|
||||
},
|
||||
Suggestions::Sealed(_) | Suggestions::Disabled => (),
|
||||
}
|
||||
append_result(&mut err.suggestions, parent_err.suggestions.clone());
|
||||
|
||||
parent_err.cancel();
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue