Skip to content

Commit

Permalink
pattern migration: move labels out of the suggestion struct
Browse files Browse the repository at this point in the history
  • Loading branch information
dianne committed Feb 3, 2025
1 parent 613bdd4 commit 724b885
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
10 changes: 4 additions & 6 deletions compiler/rustc_mir_build/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1097,20 +1097,18 @@ pub(crate) enum MiscPatternSuggestion {

#[derive(LintDiagnostic)]
#[diag(mir_build_rust_2024_incompatible_pat)]
pub(crate) struct Rust2024IncompatiblePat<'a> {
pub(crate) struct Rust2024IncompatiblePat {
#[subdiagnostic]
pub(crate) sugg: Rust2024IncompatiblePatSugg<'a>,
pub(crate) sugg: Rust2024IncompatiblePatSugg,
}

pub(crate) struct Rust2024IncompatiblePatSugg<'a> {
pub(crate) struct Rust2024IncompatiblePatSugg {
pub(crate) suggestion: Vec<(Span, String)>,
pub(crate) ref_pattern_count: usize,
pub(crate) binding_mode_count: usize,
/// Labeled spans for subpatterns invalid in Rust 2024.
pub(crate) labels: &'a [(Span, String)],
}

impl<'a> Subdiagnostic for Rust2024IncompatiblePatSugg<'a> {
impl Subdiagnostic for Rust2024IncompatiblePatSugg {
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
self,
diag: &mut Diag<'_, G>,
Expand Down
24 changes: 11 additions & 13 deletions compiler/rustc_mir_build/src/thir/pattern/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ struct PatCtxt<'a, 'tcx> {
typeck_results: &'a ty::TypeckResults<'tcx>,

/// Used by the Rust 2024 migration lint.
rust_2024_migration_suggestion: Option<Rust2024IncompatiblePatSugg<'a>>,
rust_2024_migration_suggestion: Option<Rust2024IncompatiblePatSugg>,
}

pub(super) fn pat_from_hir<'a, 'tcx>(
Expand All @@ -44,25 +44,23 @@ pub(super) fn pat_from_hir<'a, 'tcx>(
typeck_results: &'a ty::TypeckResults<'tcx>,
pat: &'tcx hir::Pat<'tcx>,
) -> Box<Pat<'tcx>> {
let migration_labels = typeck_results.rust_2024_migration_desugared_pats().get(pat.hir_id);
let mut pcx = PatCtxt {
tcx,
typing_env,
typeck_results,
rust_2024_migration_suggestion: typeck_results
.rust_2024_migration_desugared_pats()
.get(pat.hir_id)
.map(|labels| Rust2024IncompatiblePatSugg {
suggestion: Vec::new(),
ref_pattern_count: 0,
binding_mode_count: 0,
labels: labels.as_slice(),
}),
rust_2024_migration_suggestion: migration_labels.and(Some(Rust2024IncompatiblePatSugg {
suggestion: Vec::new(),
ref_pattern_count: 0,
binding_mode_count: 0,
})),
};
let result = pcx.lower_pattern(pat);
debug!("pat_from_hir({:?}) = {:?}", pat, result);
if let Some(sugg) = pcx.rust_2024_migration_suggestion {
let mut spans = MultiSpan::from_spans(sugg.labels.iter().map(|(span, _)| *span).collect());
for (span, label) in sugg.labels {
if let Some(labels) = migration_labels {
let sugg = pcx.rust_2024_migration_suggestion.expect("suggestion should be present");
let mut spans = MultiSpan::from_spans(labels.iter().map(|(span, _)| *span).collect());
for (span, label) in labels {
spans.push_span_label(*span, label.clone());
}
// If a relevant span is from at least edition 2024, this is a hard error.
Expand Down

0 comments on commit 724b885

Please sign in to comment.