mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-02 10:04:23 +00:00
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.
This commit is contained in:
parent
3148b35f6a
commit
e1fc4a997d
@ -9,7 +9,6 @@
|
|||||||
//! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/thir.html
|
//! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/thir.html
|
||||||
|
|
||||||
use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
|
use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
|
||||||
use rustc_errors::{DiagArgValue, IntoDiagArg};
|
|
||||||
use rustc_hir as hir;
|
use rustc_hir as hir;
|
||||||
use rustc_hir::def_id::DefId;
|
use rustc_hir::def_id::DefId;
|
||||||
use rustc_hir::{BindingMode, ByRef, HirId, MatchSource, RangeEnd};
|
use rustc_hir::{BindingMode, ByRef, HirId, MatchSource, RangeEnd};
|
||||||
@ -702,12 +701,6 @@ impl<'tcx> Pat<'tcx> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'tcx> IntoDiagArg for Pat<'tcx> {
|
|
||||||
fn into_diag_arg(self) -> DiagArgValue {
|
|
||||||
format!("{self}").into_diag_arg()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, HashStable, TypeVisitable)]
|
#[derive(Clone, Debug, HashStable, TypeVisitable)]
|
||||||
pub struct Ascription<'tcx> {
|
pub struct Ascription<'tcx> {
|
||||||
pub annotation: CanonicalUserTypeAnnotation<'tcx>,
|
pub annotation: CanonicalUserTypeAnnotation<'tcx>,
|
||||||
|
@ -856,7 +856,7 @@ pub(crate) struct PatternNotCovered<'s, 'tcx> {
|
|||||||
pub(crate) span: Span,
|
pub(crate) span: Span,
|
||||||
pub(crate) origin: &'s str,
|
pub(crate) origin: &'s str,
|
||||||
#[subdiagnostic]
|
#[subdiagnostic]
|
||||||
pub(crate) uncovered: Uncovered<'tcx>,
|
pub(crate) uncovered: Uncovered,
|
||||||
#[subdiagnostic]
|
#[subdiagnostic]
|
||||||
pub(crate) inform: Option<Inform>,
|
pub(crate) inform: Option<Inform>,
|
||||||
#[subdiagnostic]
|
#[subdiagnostic]
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
use rustc_errors::{Diag, EmissionGuarantee, SubdiagMessageOp, Subdiagnostic};
|
use rustc_errors::{Diag, EmissionGuarantee, SubdiagMessageOp, Subdiagnostic};
|
||||||
use rustc_macros::{LintDiagnostic, Subdiagnostic};
|
use rustc_macros::{LintDiagnostic, Subdiagnostic};
|
||||||
use rustc_middle::thir::Pat;
|
|
||||||
use rustc_middle::ty::Ty;
|
use rustc_middle::ty::Ty;
|
||||||
use rustc_span::Span;
|
use rustc_span::Span;
|
||||||
|
|
||||||
@ -8,18 +7,18 @@ use crate::rustc::{RustcPatCtxt, WitnessPat};
|
|||||||
|
|
||||||
#[derive(Subdiagnostic)]
|
#[derive(Subdiagnostic)]
|
||||||
#[label(pattern_analysis_uncovered)]
|
#[label(pattern_analysis_uncovered)]
|
||||||
pub struct Uncovered<'tcx> {
|
pub struct Uncovered {
|
||||||
#[primary_span]
|
#[primary_span]
|
||||||
span: Span,
|
span: Span,
|
||||||
count: usize,
|
count: usize,
|
||||||
witness_1: Pat<'tcx>,
|
witness_1: String, // a printed pattern
|
||||||
witness_2: Pat<'tcx>,
|
witness_2: String, // a printed pattern
|
||||||
witness_3: Pat<'tcx>,
|
witness_3: String, // a printed pattern
|
||||||
remainder: usize,
|
remainder: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'tcx> Uncovered<'tcx> {
|
impl Uncovered {
|
||||||
pub fn new<'p>(
|
pub fn new<'p, 'tcx>(
|
||||||
span: Span,
|
span: Span,
|
||||||
cx: &RustcPatCtxt<'p, 'tcx>,
|
cx: &RustcPatCtxt<'p, 'tcx>,
|
||||||
witnesses: Vec<WitnessPat<'p, 'tcx>>,
|
witnesses: Vec<WitnessPat<'p, 'tcx>>,
|
||||||
@ -27,19 +26,19 @@ impl<'tcx> Uncovered<'tcx> {
|
|||||||
where
|
where
|
||||||
'tcx: 'p,
|
'tcx: 'p,
|
||||||
{
|
{
|
||||||
let witness_1 = cx.hoist_witness_pat(witnesses.get(0).unwrap());
|
let witness_1 = cx.hoist_witness_pat(witnesses.get(0).unwrap()).to_string();
|
||||||
Self {
|
Self {
|
||||||
span,
|
span,
|
||||||
count: witnesses.len(),
|
count: witnesses.len(),
|
||||||
// Substitute dummy values if witnesses is smaller than 3. These will never be read.
|
// Substitute dummy values if witnesses is smaller than 3. These will never be read.
|
||||||
witness_2: witnesses
|
witness_2: witnesses
|
||||||
.get(1)
|
.get(1)
|
||||||
.map(|w| cx.hoist_witness_pat(w))
|
.map(|w| cx.hoist_witness_pat(w).to_string())
|
||||||
.unwrap_or_else(|| witness_1.clone()),
|
.unwrap_or_default(),
|
||||||
witness_3: witnesses
|
witness_3: witnesses
|
||||||
.get(2)
|
.get(2)
|
||||||
.map(|w| cx.hoist_witness_pat(w))
|
.map(|w| cx.hoist_witness_pat(w).to_string())
|
||||||
.unwrap_or_else(|| witness_1.clone()),
|
.unwrap_or_default(),
|
||||||
witness_1,
|
witness_1,
|
||||||
remainder: witnesses.len().saturating_sub(3),
|
remainder: witnesses.len().saturating_sub(3),
|
||||||
}
|
}
|
||||||
@ -49,19 +48,19 @@ impl<'tcx> Uncovered<'tcx> {
|
|||||||
#[derive(LintDiagnostic)]
|
#[derive(LintDiagnostic)]
|
||||||
#[diag(pattern_analysis_overlapping_range_endpoints)]
|
#[diag(pattern_analysis_overlapping_range_endpoints)]
|
||||||
#[note]
|
#[note]
|
||||||
pub struct OverlappingRangeEndpoints<'tcx> {
|
pub struct OverlappingRangeEndpoints {
|
||||||
#[label]
|
#[label]
|
||||||
pub range: Span,
|
pub range: Span,
|
||||||
#[subdiagnostic]
|
#[subdiagnostic]
|
||||||
pub overlap: Vec<Overlap<'tcx>>,
|
pub overlap: Vec<Overlap>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Overlap<'tcx> {
|
pub struct Overlap {
|
||||||
pub span: Span,
|
pub span: Span,
|
||||||
pub range: Pat<'tcx>,
|
pub range: String, // a printed pattern
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'tcx> Subdiagnostic for Overlap<'tcx> {
|
impl Subdiagnostic for Overlap {
|
||||||
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
|
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
|
||||||
self,
|
self,
|
||||||
diag: &mut Diag<'_, G>,
|
diag: &mut Diag<'_, G>,
|
||||||
@ -78,38 +77,38 @@ impl<'tcx> Subdiagnostic for Overlap<'tcx> {
|
|||||||
|
|
||||||
#[derive(LintDiagnostic)]
|
#[derive(LintDiagnostic)]
|
||||||
#[diag(pattern_analysis_excluside_range_missing_max)]
|
#[diag(pattern_analysis_excluside_range_missing_max)]
|
||||||
pub struct ExclusiveRangeMissingMax<'tcx> {
|
pub struct ExclusiveRangeMissingMax {
|
||||||
#[label]
|
#[label]
|
||||||
#[suggestion(code = "{suggestion}", applicability = "maybe-incorrect")]
|
#[suggestion(code = "{suggestion}", applicability = "maybe-incorrect")]
|
||||||
/// This is an exclusive range that looks like `lo..max` (i.e. doesn't match `max`).
|
/// This is an exclusive range that looks like `lo..max` (i.e. doesn't match `max`).
|
||||||
pub first_range: Span,
|
pub first_range: Span,
|
||||||
/// Suggest `lo..=max` instead.
|
/// Suggest `lo..=max` instead.
|
||||||
pub suggestion: String,
|
pub suggestion: String,
|
||||||
pub max: Pat<'tcx>,
|
pub max: String, // a printed pattern
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(LintDiagnostic)]
|
#[derive(LintDiagnostic)]
|
||||||
#[diag(pattern_analysis_excluside_range_missing_gap)]
|
#[diag(pattern_analysis_excluside_range_missing_gap)]
|
||||||
pub struct ExclusiveRangeMissingGap<'tcx> {
|
pub struct ExclusiveRangeMissingGap {
|
||||||
#[label]
|
#[label]
|
||||||
#[suggestion(code = "{suggestion}", applicability = "maybe-incorrect")]
|
#[suggestion(code = "{suggestion}", applicability = "maybe-incorrect")]
|
||||||
/// This is an exclusive range that looks like `lo..gap` (i.e. doesn't match `gap`).
|
/// This is an exclusive range that looks like `lo..gap` (i.e. doesn't match `gap`).
|
||||||
pub first_range: Span,
|
pub first_range: Span,
|
||||||
pub gap: Pat<'tcx>,
|
pub gap: String, // a printed pattern
|
||||||
/// Suggest `lo..=gap` instead.
|
/// Suggest `lo..=gap` instead.
|
||||||
pub suggestion: String,
|
pub suggestion: String,
|
||||||
#[subdiagnostic]
|
#[subdiagnostic]
|
||||||
/// All these ranges skipped over `gap` which we think is probably a mistake.
|
/// All these ranges skipped over `gap` which we think is probably a mistake.
|
||||||
pub gap_with: Vec<GappedRange<'tcx>>,
|
pub gap_with: Vec<GappedRange>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct GappedRange<'tcx> {
|
pub struct GappedRange {
|
||||||
pub span: Span,
|
pub span: Span,
|
||||||
pub gap: Pat<'tcx>,
|
pub gap: String, // a printed pattern
|
||||||
pub first_range: Pat<'tcx>,
|
pub first_range: String, // a printed pattern
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'tcx> Subdiagnostic for GappedRange<'tcx> {
|
impl Subdiagnostic for GappedRange {
|
||||||
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
|
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
|
||||||
self,
|
self,
|
||||||
diag: &mut Diag<'_, G>,
|
diag: &mut Diag<'_, G>,
|
||||||
@ -134,7 +133,7 @@ impl<'tcx> Subdiagnostic for GappedRange<'tcx> {
|
|||||||
pub(crate) struct NonExhaustiveOmittedPattern<'tcx> {
|
pub(crate) struct NonExhaustiveOmittedPattern<'tcx> {
|
||||||
pub scrut_ty: Ty<'tcx>,
|
pub scrut_ty: Ty<'tcx>,
|
||||||
#[subdiagnostic]
|
#[subdiagnostic]
|
||||||
pub uncovered: Uncovered<'tcx>,
|
pub uncovered: Uncovered,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(LintDiagnostic)]
|
#[derive(LintDiagnostic)]
|
||||||
|
@ -966,7 +966,7 @@ impl<'p, 'tcx: 'p> PatCx for RustcPatCtxt<'p, 'tcx> {
|
|||||||
let overlaps: Vec<_> = overlaps_with
|
let overlaps: Vec<_> = overlaps_with
|
||||||
.iter()
|
.iter()
|
||||||
.map(|pat| pat.data().span)
|
.map(|pat| pat.data().span)
|
||||||
.map(|span| errors::Overlap { range: overlap_as_pat.clone(), span })
|
.map(|span| errors::Overlap { range: overlap_as_pat.to_string(), span })
|
||||||
.collect();
|
.collect();
|
||||||
let pat_span = pat.data().span;
|
let pat_span = pat.data().span;
|
||||||
self.tcx.emit_node_span_lint(
|
self.tcx.emit_node_span_lint(
|
||||||
@ -1014,7 +1014,7 @@ impl<'p, 'tcx: 'p> PatCx for RustcPatCtxt<'p, 'tcx> {
|
|||||||
// Point at this range.
|
// Point at this range.
|
||||||
first_range: thir_pat.span,
|
first_range: thir_pat.span,
|
||||||
// That's the gap that isn't covered.
|
// That's the gap that isn't covered.
|
||||||
max: gap_as_pat.clone(),
|
max: gap_as_pat.to_string(),
|
||||||
// Suggest `lo..=max` instead.
|
// Suggest `lo..=max` instead.
|
||||||
suggestion: suggested_range.to_string(),
|
suggestion: suggested_range.to_string(),
|
||||||
},
|
},
|
||||||
@ -1028,7 +1028,7 @@ impl<'p, 'tcx: 'p> PatCx for RustcPatCtxt<'p, 'tcx> {
|
|||||||
// Point at this range.
|
// Point at this range.
|
||||||
first_range: thir_pat.span,
|
first_range: thir_pat.span,
|
||||||
// That's the gap that isn't covered.
|
// That's the gap that isn't covered.
|
||||||
gap: gap_as_pat.clone(),
|
gap: gap_as_pat.to_string(),
|
||||||
// Suggest `lo..=gap` instead.
|
// Suggest `lo..=gap` instead.
|
||||||
suggestion: suggested_range.to_string(),
|
suggestion: suggested_range.to_string(),
|
||||||
// All these ranges skipped over `gap` which we think is probably a
|
// 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> {
|
|||||||
.iter()
|
.iter()
|
||||||
.map(|pat| errors::GappedRange {
|
.map(|pat| errors::GappedRange {
|
||||||
span: pat.data().span,
|
span: pat.data().span,
|
||||||
gap: gap_as_pat.clone(),
|
gap: gap_as_pat.to_string(),
|
||||||
first_range: thir_pat.clone(),
|
first_range: thir_pat.to_string(),
|
||||||
})
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user