Skip to content

Commit e1fc4a9

Browse files
committed
Don't store thir::Pat in error structs
In several cases this avoids the need to clone the underlying pattern, and then print the clone later.
1 parent 3148b35 commit e1fc4a9

File tree

4 files changed

+32
-40
lines changed

4 files changed

+32
-40
lines changed

Diff for: compiler/rustc_middle/src/thir.rs

-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
//! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/thir.html
1010
1111
use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
12-
use rustc_errors::{DiagArgValue, IntoDiagArg};
1312
use rustc_hir as hir;
1413
use rustc_hir::def_id::DefId;
1514
use rustc_hir::{BindingMode, ByRef, HirId, MatchSource, RangeEnd};
@@ -702,12 +701,6 @@ impl<'tcx> Pat<'tcx> {
702701
}
703702
}
704703

705-
impl<'tcx> IntoDiagArg for Pat<'tcx> {
706-
fn into_diag_arg(self) -> DiagArgValue {
707-
format!("{self}").into_diag_arg()
708-
}
709-
}
710-
711704
#[derive(Clone, Debug, HashStable, TypeVisitable)]
712705
pub struct Ascription<'tcx> {
713706
pub annotation: CanonicalUserTypeAnnotation<'tcx>,

Diff for: compiler/rustc_mir_build/src/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ pub(crate) struct PatternNotCovered<'s, 'tcx> {
856856
pub(crate) span: Span,
857857
pub(crate) origin: &'s str,
858858
#[subdiagnostic]
859-
pub(crate) uncovered: Uncovered<'tcx>,
859+
pub(crate) uncovered: Uncovered,
860860
#[subdiagnostic]
861861
pub(crate) inform: Option<Inform>,
862862
#[subdiagnostic]

Diff for: compiler/rustc_pattern_analysis/src/errors.rs

+26-27
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,44 @@
11
use rustc_errors::{Diag, EmissionGuarantee, SubdiagMessageOp, Subdiagnostic};
22
use rustc_macros::{LintDiagnostic, Subdiagnostic};
3-
use rustc_middle::thir::Pat;
43
use rustc_middle::ty::Ty;
54
use rustc_span::Span;
65

76
use crate::rustc::{RustcPatCtxt, WitnessPat};
87

98
#[derive(Subdiagnostic)]
109
#[label(pattern_analysis_uncovered)]
11-
pub struct Uncovered<'tcx> {
10+
pub struct Uncovered {
1211
#[primary_span]
1312
span: Span,
1413
count: usize,
15-
witness_1: Pat<'tcx>,
16-
witness_2: Pat<'tcx>,
17-
witness_3: Pat<'tcx>,
14+
witness_1: String, // a printed pattern
15+
witness_2: String, // a printed pattern
16+
witness_3: String, // a printed pattern
1817
remainder: usize,
1918
}
2019

21-
impl<'tcx> Uncovered<'tcx> {
22-
pub fn new<'p>(
20+
impl Uncovered {
21+
pub fn new<'p, 'tcx>(
2322
span: Span,
2423
cx: &RustcPatCtxt<'p, 'tcx>,
2524
witnesses: Vec<WitnessPat<'p, 'tcx>>,
2625
) -> Self
2726
where
2827
'tcx: 'p,
2928
{
30-
let witness_1 = cx.hoist_witness_pat(witnesses.get(0).unwrap());
29+
let witness_1 = cx.hoist_witness_pat(witnesses.get(0).unwrap()).to_string();
3130
Self {
3231
span,
3332
count: witnesses.len(),
3433
// Substitute dummy values if witnesses is smaller than 3. These will never be read.
3534
witness_2: witnesses
3635
.get(1)
37-
.map(|w| cx.hoist_witness_pat(w))
38-
.unwrap_or_else(|| witness_1.clone()),
36+
.map(|w| cx.hoist_witness_pat(w).to_string())
37+
.unwrap_or_default(),
3938
witness_3: witnesses
4039
.get(2)
41-
.map(|w| cx.hoist_witness_pat(w))
42-
.unwrap_or_else(|| witness_1.clone()),
40+
.map(|w| cx.hoist_witness_pat(w).to_string())
41+
.unwrap_or_default(),
4342
witness_1,
4443
remainder: witnesses.len().saturating_sub(3),
4544
}
@@ -49,19 +48,19 @@ impl<'tcx> Uncovered<'tcx> {
4948
#[derive(LintDiagnostic)]
5049
#[diag(pattern_analysis_overlapping_range_endpoints)]
5150
#[note]
52-
pub struct OverlappingRangeEndpoints<'tcx> {
51+
pub struct OverlappingRangeEndpoints {
5352
#[label]
5453
pub range: Span,
5554
#[subdiagnostic]
56-
pub overlap: Vec<Overlap<'tcx>>,
55+
pub overlap: Vec<Overlap>,
5756
}
5857

59-
pub struct Overlap<'tcx> {
58+
pub struct Overlap {
6059
pub span: Span,
61-
pub range: Pat<'tcx>,
60+
pub range: String, // a printed pattern
6261
}
6362

64-
impl<'tcx> Subdiagnostic for Overlap<'tcx> {
63+
impl Subdiagnostic for Overlap {
6564
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
6665
self,
6766
diag: &mut Diag<'_, G>,
@@ -78,38 +77,38 @@ impl<'tcx> Subdiagnostic for Overlap<'tcx> {
7877

7978
#[derive(LintDiagnostic)]
8079
#[diag(pattern_analysis_excluside_range_missing_max)]
81-
pub struct ExclusiveRangeMissingMax<'tcx> {
80+
pub struct ExclusiveRangeMissingMax {
8281
#[label]
8382
#[suggestion(code = "{suggestion}", applicability = "maybe-incorrect")]
8483
/// This is an exclusive range that looks like `lo..max` (i.e. doesn't match `max`).
8584
pub first_range: Span,
8685
/// Suggest `lo..=max` instead.
8786
pub suggestion: String,
88-
pub max: Pat<'tcx>,
87+
pub max: String, // a printed pattern
8988
}
9089

9190
#[derive(LintDiagnostic)]
9291
#[diag(pattern_analysis_excluside_range_missing_gap)]
93-
pub struct ExclusiveRangeMissingGap<'tcx> {
92+
pub struct ExclusiveRangeMissingGap {
9493
#[label]
9594
#[suggestion(code = "{suggestion}", applicability = "maybe-incorrect")]
9695
/// This is an exclusive range that looks like `lo..gap` (i.e. doesn't match `gap`).
9796
pub first_range: Span,
98-
pub gap: Pat<'tcx>,
97+
pub gap: String, // a printed pattern
9998
/// Suggest `lo..=gap` instead.
10099
pub suggestion: String,
101100
#[subdiagnostic]
102101
/// All these ranges skipped over `gap` which we think is probably a mistake.
103-
pub gap_with: Vec<GappedRange<'tcx>>,
102+
pub gap_with: Vec<GappedRange>,
104103
}
105104

106-
pub struct GappedRange<'tcx> {
105+
pub struct GappedRange {
107106
pub span: Span,
108-
pub gap: Pat<'tcx>,
109-
pub first_range: Pat<'tcx>,
107+
pub gap: String, // a printed pattern
108+
pub first_range: String, // a printed pattern
110109
}
111110

112-
impl<'tcx> Subdiagnostic for GappedRange<'tcx> {
111+
impl Subdiagnostic for GappedRange {
113112
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
114113
self,
115114
diag: &mut Diag<'_, G>,
@@ -134,7 +133,7 @@ impl<'tcx> Subdiagnostic for GappedRange<'tcx> {
134133
pub(crate) struct NonExhaustiveOmittedPattern<'tcx> {
135134
pub scrut_ty: Ty<'tcx>,
136135
#[subdiagnostic]
137-
pub uncovered: Uncovered<'tcx>,
136+
pub uncovered: Uncovered,
138137
}
139138

140139
#[derive(LintDiagnostic)]

Diff for: compiler/rustc_pattern_analysis/src/rustc.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,7 @@ impl<'p, 'tcx: 'p> PatCx for RustcPatCtxt<'p, 'tcx> {
966966
let overlaps: Vec<_> = overlaps_with
967967
.iter()
968968
.map(|pat| pat.data().span)
969-
.map(|span| errors::Overlap { range: overlap_as_pat.clone(), span })
969+
.map(|span| errors::Overlap { range: overlap_as_pat.to_string(), span })
970970
.collect();
971971
let pat_span = pat.data().span;
972972
self.tcx.emit_node_span_lint(
@@ -1014,7 +1014,7 @@ impl<'p, 'tcx: 'p> PatCx for RustcPatCtxt<'p, 'tcx> {
10141014
// Point at this range.
10151015
first_range: thir_pat.span,
10161016
// That's the gap that isn't covered.
1017-
max: gap_as_pat.clone(),
1017+
max: gap_as_pat.to_string(),
10181018
// Suggest `lo..=max` instead.
10191019
suggestion: suggested_range.to_string(),
10201020
},
@@ -1028,7 +1028,7 @@ impl<'p, 'tcx: 'p> PatCx for RustcPatCtxt<'p, 'tcx> {
10281028
// Point at this range.
10291029
first_range: thir_pat.span,
10301030
// That's the gap that isn't covered.
1031-
gap: gap_as_pat.clone(),
1031+
gap: gap_as_pat.to_string(),
10321032
// Suggest `lo..=gap` instead.
10331033
suggestion: suggested_range.to_string(),
10341034
// All these ranges skipped over `gap` which we think is probably a
@@ -1037,8 +1037,8 @@ impl<'p, 'tcx: 'p> PatCx for RustcPatCtxt<'p, 'tcx> {
10371037
.iter()
10381038
.map(|pat| errors::GappedRange {
10391039
span: pat.data().span,
1040-
gap: gap_as_pat.clone(),
1041-
first_range: thir_pat.clone(),
1040+
gap: gap_as_pat.to_string(),
1041+
first_range: thir_pat.to_string(),
10421042
})
10431043
.collect(),
10441044
},

0 commit comments

Comments
 (0)