Skip to content

Commit

Permalink
Auto merge of rust-lang#119395 - Nilstrieb:walk-pat, r=est31
Browse files Browse the repository at this point in the history
Use `Pat::walk_always` instead of manual walk

It's also a bit faster, but I doubt that it will have a noticeable perf impact. Mostly doing it because it's shorter and nicer.
  • Loading branch information
bors committed Dec 29, 2023
2 parents 6362304 + 8fe4d0d commit dfb1f5e
Showing 1 changed file with 6 additions and 29 deletions.
35 changes: 6 additions & 29 deletions compiler/rustc_passes/src/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ use rustc_span::symbol::{kw, sym, Symbol};
use rustc_span::DUMMY_SP;
use rustc_span::{BytePos, Span};

use std::collections::VecDeque;
use std::io;
use std::io::prelude::*;
use std::rc::Rc;
Expand Down Expand Up @@ -317,35 +316,13 @@ impl<'tcx> IrMaps<'tcx> {
// For struct patterns, take note of which fields used shorthand
// (`x` rather than `x: x`).
let mut shorthand_field_ids = HirIdSet::default();
let mut pats = VecDeque::new();
pats.push_back(pat);

while let Some(pat) = pats.pop_front() {
use rustc_hir::PatKind::*;
match &pat.kind {
Binding(.., inner_pat) => {
pats.extend(inner_pat.iter());
}
Struct(_, fields, _) => {
let (short, not_short): (Vec<hir::PatField<'_>>, _) =
fields.iter().partition(|f| f.is_shorthand);
shorthand_field_ids.extend(short.iter().map(|f| f.pat.hir_id));
pats.extend(not_short.iter().map(|f| f.pat));
}
Ref(inner_pat, _) | Box(inner_pat) => {
pats.push_back(inner_pat);
}
TupleStruct(_, inner_pats, _) | Tuple(inner_pats, _) | Or(inner_pats) => {
pats.extend(inner_pats.iter());
}
Slice(pre_pats, inner_pat, post_pats) => {
pats.extend(pre_pats.iter());
pats.extend(inner_pat.iter());
pats.extend(post_pats.iter());
}
_ => {}

pat.walk_always(|pat| {
if let hir::PatKind::Struct(_, fields, _) = pat.kind {
let short = fields.iter().filter(|f| f.is_shorthand);
shorthand_field_ids.extend(short.map(|f| f.pat.hir_id));
}
}
});

shorthand_field_ids
}
Expand Down

0 comments on commit dfb1f5e

Please sign in to comment.