Skip to content

Commit 25862ff

Browse files
committed
Auto merge of #93259 - eddyb:diagbld-scalar-pair, r=jackh726
rustc_errors: only box the `diagnostic` field in `DiagnosticBuilder`. I happened to need to do the first change (replacing `allow_suggestions` with equivalent functionality on `Diagnostic` itself) as part of a larger change, and noticed that there's only two fields left in `DiagnosticBuilderInner`. So with this PR, instead of a single pointer, `DiagnosticBuilder` is two pointers, which should work just as well for passing *it* by value (and may even work better wrt some operations, though probably not by much). But anything that was already taking advantage of `DiagnosticBuilder` being a single pointer, and wrapping it further (e.g. `Result<T, DiagnosticBuilder>` w/ non-ZST `T`), ~~will probably see a slowdown~~, so I want to do a perf run before even trying to propose this.
2 parents a6cd4aa + f5a3271 commit 25862ff

File tree

8 files changed

+88
-156
lines changed

8 files changed

+88
-156
lines changed

compiler/rustc_errors/src/diagnostic.rs

+29-9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ use rustc_span::{MultiSpan, Span, DUMMY_SP};
1111
use std::fmt;
1212
use std::hash::{Hash, Hasher};
1313

14+
/// Error type for `Diagnostic`'s `suggestions` field, indicating that
15+
/// `.disable_suggestions()` was called on the `Diagnostic`.
16+
#[derive(Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable)]
17+
pub struct SuggestionsDisabled;
18+
1419
#[must_use]
1520
#[derive(Clone, Debug, Encodable, Decodable)]
1621
pub struct Diagnostic {
@@ -19,7 +24,7 @@ pub struct Diagnostic {
1924
pub code: Option<DiagnosticId>,
2025
pub span: MultiSpan,
2126
pub children: Vec<SubDiagnostic>,
22-
pub suggestions: Vec<CodeSuggestion>,
27+
pub suggestions: Result<Vec<CodeSuggestion>, SuggestionsDisabled>,
2328

2429
/// This is not used for highlighting or rendering any error message. Rather, it can be used
2530
/// as a sort key to sort a buffer of diagnostics. By default, it is the primary span of
@@ -106,7 +111,7 @@ impl Diagnostic {
106111
code,
107112
span: MultiSpan::new(),
108113
children: vec![],
109-
suggestions: vec![],
114+
suggestions: Ok(vec![]),
110115
sort_span: DUMMY_SP,
111116
is_lint: false,
112117
}
@@ -300,6 +305,21 @@ impl Diagnostic {
300305
self
301306
}
302307

308+
/// Disallow attaching suggestions this diagnostic.
309+
/// Any suggestions attached e.g. with the `span_suggestion_*` methods
310+
/// (before and after the call to `disable_suggestions`) will be ignored.
311+
pub fn disable_suggestions(&mut self) -> &mut Self {
312+
self.suggestions = Err(SuggestionsDisabled);
313+
self
314+
}
315+
316+
/// Helper for pushing to `self.suggestions`, if available (not disable).
317+
fn push_suggestion(&mut self, suggestion: CodeSuggestion) {
318+
if let Ok(suggestions) = &mut self.suggestions {
319+
suggestions.push(suggestion);
320+
}
321+
}
322+
303323
/// Show a suggestion that has multiple parts to it.
304324
/// In other words, multiple changes need to be applied as part of this suggestion.
305325
pub fn multipart_suggestion(
@@ -340,7 +360,7 @@ impl Diagnostic {
340360
style: SuggestionStyle,
341361
) -> &mut Self {
342362
assert!(!suggestion.is_empty());
343-
self.suggestions.push(CodeSuggestion {
363+
self.push_suggestion(CodeSuggestion {
344364
substitutions: vec![Substitution {
345365
parts: suggestion
346366
.into_iter()
@@ -368,7 +388,7 @@ impl Diagnostic {
368388
applicability: Applicability,
369389
) -> &mut Self {
370390
assert!(!suggestion.is_empty());
371-
self.suggestions.push(CodeSuggestion {
391+
self.push_suggestion(CodeSuggestion {
372392
substitutions: vec![Substitution {
373393
parts: suggestion
374394
.into_iter()
@@ -426,7 +446,7 @@ impl Diagnostic {
426446
applicability: Applicability,
427447
style: SuggestionStyle,
428448
) -> &mut Self {
429-
self.suggestions.push(CodeSuggestion {
449+
self.push_suggestion(CodeSuggestion {
430450
substitutions: vec![Substitution {
431451
parts: vec![SubstitutionPart { snippet: suggestion, span: sp }],
432452
}],
@@ -471,7 +491,7 @@ impl Diagnostic {
471491
.into_iter()
472492
.map(|snippet| Substitution { parts: vec![SubstitutionPart { snippet, span: sp }] })
473493
.collect();
474-
self.suggestions.push(CodeSuggestion {
494+
self.push_suggestion(CodeSuggestion {
475495
substitutions,
476496
msg: msg.to_owned(),
477497
style: SuggestionStyle::ShowCode,
@@ -489,7 +509,7 @@ impl Diagnostic {
489509
suggestions: impl Iterator<Item = Vec<(Span, String)>>,
490510
applicability: Applicability,
491511
) -> &mut Self {
492-
self.suggestions.push(CodeSuggestion {
512+
self.push_suggestion(CodeSuggestion {
493513
substitutions: suggestions
494514
.map(|sugg| Substitution {
495515
parts: sugg
@@ -578,7 +598,7 @@ impl Diagnostic {
578598
applicability: Applicability,
579599
tool_metadata: Json,
580600
) {
581-
self.suggestions.push(CodeSuggestion {
601+
self.push_suggestion(CodeSuggestion {
582602
substitutions: vec![],
583603
msg: msg.to_owned(),
584604
style: SuggestionStyle::CompletelyHidden,
@@ -668,7 +688,7 @@ impl Diagnostic {
668688
&Vec<(String, Style)>,
669689
&Option<DiagnosticId>,
670690
&MultiSpan,
671-
&Vec<CodeSuggestion>,
691+
&Result<Vec<CodeSuggestion>, SuggestionsDisabled>,
672692
Option<&Vec<SubDiagnostic>>,
673693
) {
674694
(

0 commit comments

Comments
 (0)