Skip to content

Commit 0d52934

Browse files
committed
Auto merge of #16420 - Nadrieril:use-upstream-pattern-analysis, r=Veykril
Use upstream exhaustiveness checker! Because it has been librarified! The extra `Apache-2.0 WITH LLVM-exception` license is for `rustc_apfloat`. Also this duplicates `rustc_index` because the other upstream deps are still on an earlier version. They should be bumpable now though. Good thing is that we don't need this new crate to be synchronized with the others, which will make our lives easier.
2 parents d410d4a + 2370b70 commit 0d52934

File tree

11 files changed

+603
-1952
lines changed

11 files changed

+603
-1952
lines changed

Cargo.lock

Lines changed: 85 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ ra-ap-rustc_lexer = { version = "0.21.0", default-features = false }
8383
ra-ap-rustc_parse_format = { version = "0.21.0", default-features = false }
8484
ra-ap-rustc_index = { version = "0.21.0", default-features = false }
8585
ra-ap-rustc_abi = { version = "0.21.0", default-features = false }
86+
ra-ap-rustc_pattern_analysis = { version = "0.33.0", default-features = false }
8687

8788
# local crates that aren't published to crates.io. These should not have versions.
8889
sourcegen = { path = "./crates/sourcegen" }

crates/hir-def/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,15 @@ impl From<AssocItemId> for AttrDefId {
939939
}
940940
}
941941
}
942+
impl From<VariantId> for AttrDefId {
943+
fn from(vid: VariantId) -> Self {
944+
match vid {
945+
VariantId::EnumVariantId(id) => id.into(),
946+
VariantId::StructId(id) => id.into(),
947+
VariantId::UnionId(id) => id.into(),
948+
}
949+
}
950+
}
942951

943952
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
944953
pub enum VariantId {

crates/hir-ty/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ indexmap.workspace = true
3636

3737
ra-ap-rustc_abi.workspace = true
3838
ra-ap-rustc_index.workspace = true
39+
ra-ap-rustc_pattern_analysis.workspace = true
3940

4041

4142
# local deps

crates/hir-ty/src/diagnostics/expr.rs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ use hir_def::{ItemContainerId, Lookup};
1111
use hir_expand::name;
1212
use itertools::Itertools;
1313
use rustc_hash::FxHashSet;
14+
use rustc_pattern_analysis::usefulness::{compute_match_usefulness, ValidityConstraint};
1415
use triomphe::Arc;
1516
use typed_arena::Arena;
1617

1718
use crate::{
1819
db::HirDatabase,
1920
diagnostics::match_check::{
2021
self,
21-
deconstruct_pat::DeconstructedPat,
22-
usefulness::{compute_match_usefulness, MatchCheckCtx},
22+
pat_analysis::{self, DeconstructedPat, MatchCheckCtx, WitnessPat},
2323
},
2424
display::HirDisplay,
2525
InferenceResult, Ty, TyExt,
@@ -152,7 +152,14 @@ impl ExprValidator {
152152
}
153153

154154
let pattern_arena = Arena::new();
155-
let cx = MatchCheckCtx::new(self.owner.module(db.upcast()), self.owner, db, &pattern_arena);
155+
let ty_arena = Arena::new();
156+
let cx = MatchCheckCtx::new(
157+
self.owner.module(db.upcast()),
158+
self.owner,
159+
db,
160+
&pattern_arena,
161+
&ty_arena,
162+
);
156163

157164
let mut m_arms = Vec::with_capacity(arms.len());
158165
let mut has_lowering_errors = false;
@@ -178,9 +185,10 @@ impl ExprValidator {
178185
// If we had a NotUsefulMatchArm diagnostic, we could
179186
// check the usefulness of each pattern as we added it
180187
// to the matrix here.
181-
let m_arm = match_check::MatchArm {
188+
let m_arm = pat_analysis::MatchArm {
182189
pat: self.lower_pattern(&cx, arm.pat, db, &body, &mut has_lowering_errors),
183190
has_guard: arm.guard.is_some(),
191+
arm_data: (),
184192
};
185193
m_arms.push(m_arm);
186194
if !has_lowering_errors {
@@ -197,7 +205,15 @@ impl ExprValidator {
197205
return;
198206
}
199207

200-
let report = compute_match_usefulness(&cx, &m_arms, scrut_ty);
208+
let report = match compute_match_usefulness(
209+
rustc_pattern_analysis::MatchCtxt { tycx: &cx },
210+
m_arms.as_slice(),
211+
scrut_ty.clone(),
212+
ValidityConstraint::ValidOnly,
213+
) {
214+
Ok(report) => report,
215+
Err(void) => match void {},
216+
};
201217

202218
// FIXME Report unreachable arms
203219
// https://github.com/rust-lang/rust/blob/f31622a50/compiler/rustc_mir_build/src/thir/pattern/check_match.rs#L200
@@ -213,15 +229,15 @@ impl ExprValidator {
213229

214230
fn lower_pattern<'p>(
215231
&self,
216-
cx: &MatchCheckCtx<'_, 'p>,
232+
cx: &MatchCheckCtx<'p>,
217233
pat: PatId,
218234
db: &dyn HirDatabase,
219235
body: &Body,
220236
have_errors: &mut bool,
221237
) -> &'p DeconstructedPat<'p> {
222238
let mut patcx = match_check::PatCtxt::new(db, &self.infer, body);
223239
let pattern = patcx.lower_pattern(pat);
224-
let pattern = cx.pattern_arena.alloc(DeconstructedPat::from_pat(cx, &pattern));
240+
let pattern = cx.pattern_arena.alloc(cx.lower_pat(&pattern));
225241
if !patcx.errors.is_empty() {
226242
*have_errors = true;
227243
}
@@ -364,16 +380,16 @@ fn types_of_subpatterns_do_match(pat: PatId, body: &Body, infer: &InferenceResul
364380
}
365381

366382
fn missing_match_arms<'p>(
367-
cx: &MatchCheckCtx<'_, 'p>,
383+
cx: &MatchCheckCtx<'p>,
368384
scrut_ty: &Ty,
369-
witnesses: Vec<DeconstructedPat<'p>>,
385+
witnesses: Vec<WitnessPat<'p>>,
370386
arms: &[MatchArm],
371387
) -> String {
372-
struct DisplayWitness<'a, 'p>(&'a DeconstructedPat<'p>, &'a MatchCheckCtx<'a, 'p>);
388+
struct DisplayWitness<'a, 'p>(&'a WitnessPat<'p>, &'a MatchCheckCtx<'p>);
373389
impl fmt::Display for DisplayWitness<'_, '_> {
374390
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
375391
let DisplayWitness(witness, cx) = *self;
376-
let pat = witness.to_pat(cx);
392+
let pat = cx.hoist_witness_pat(witness);
377393
write!(f, "{}", pat.display(cx.db))
378394
}
379395
}

crates/hir-ty/src/diagnostics/match_check.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
88
mod pat_util;
99

10-
pub(crate) mod deconstruct_pat;
11-
pub(crate) mod usefulness;
10+
pub(crate) mod pat_analysis;
1211

1312
use chalk_ir::Mutability;
1413
use hir_def::{
@@ -27,8 +26,6 @@ use crate::{
2726

2827
use self::pat_util::EnumerateAndAdjustIterator;
2928

30-
pub(crate) use self::usefulness::MatchArm;
31-
3229
#[derive(Clone, Debug)]
3330
pub(crate) enum PatternError {
3431
Unimplemented,

0 commit comments

Comments
 (0)