Skip to content

Commit

Permalink
Auto merge of rust-lang#16420 - Nadrieril:use-upstream-pattern-analys…
Browse files Browse the repository at this point in the history
…is, 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.
  • Loading branch information
bors committed Jan 24, 2024
2 parents d410d4a + 2370b70 commit 0d52934
Show file tree
Hide file tree
Showing 11 changed files with 603 additions and 1,952 deletions.
100 changes: 85 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ ra-ap-rustc_lexer = { version = "0.21.0", default-features = false }
ra-ap-rustc_parse_format = { version = "0.21.0", default-features = false }
ra-ap-rustc_index = { version = "0.21.0", default-features = false }
ra-ap-rustc_abi = { version = "0.21.0", default-features = false }
ra-ap-rustc_pattern_analysis = { version = "0.33.0", default-features = false }

# local crates that aren't published to crates.io. These should not have versions.
sourcegen = { path = "./crates/sourcegen" }
Expand Down
9 changes: 9 additions & 0 deletions crates/hir-def/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,15 @@ impl From<AssocItemId> for AttrDefId {
}
}
}
impl From<VariantId> for AttrDefId {
fn from(vid: VariantId) -> Self {
match vid {
VariantId::EnumVariantId(id) => id.into(),
VariantId::StructId(id) => id.into(),
VariantId::UnionId(id) => id.into(),
}
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum VariantId {
Expand Down
1 change: 1 addition & 0 deletions crates/hir-ty/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ indexmap.workspace = true

ra-ap-rustc_abi.workspace = true
ra-ap-rustc_index.workspace = true
ra-ap-rustc_pattern_analysis.workspace = true


# local deps
Expand Down
38 changes: 27 additions & 11 deletions crates/hir-ty/src/diagnostics/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ use hir_def::{ItemContainerId, Lookup};
use hir_expand::name;
use itertools::Itertools;
use rustc_hash::FxHashSet;
use rustc_pattern_analysis::usefulness::{compute_match_usefulness, ValidityConstraint};
use triomphe::Arc;
use typed_arena::Arena;

use crate::{
db::HirDatabase,
diagnostics::match_check::{
self,
deconstruct_pat::DeconstructedPat,
usefulness::{compute_match_usefulness, MatchCheckCtx},
pat_analysis::{self, DeconstructedPat, MatchCheckCtx, WitnessPat},
},
display::HirDisplay,
InferenceResult, Ty, TyExt,
Expand Down Expand Up @@ -152,7 +152,14 @@ impl ExprValidator {
}

let pattern_arena = Arena::new();
let cx = MatchCheckCtx::new(self.owner.module(db.upcast()), self.owner, db, &pattern_arena);
let ty_arena = Arena::new();
let cx = MatchCheckCtx::new(
self.owner.module(db.upcast()),
self.owner,
db,
&pattern_arena,
&ty_arena,
);

let mut m_arms = Vec::with_capacity(arms.len());
let mut has_lowering_errors = false;
Expand All @@ -178,9 +185,10 @@ impl ExprValidator {
// If we had a NotUsefulMatchArm diagnostic, we could
// check the usefulness of each pattern as we added it
// to the matrix here.
let m_arm = match_check::MatchArm {
let m_arm = pat_analysis::MatchArm {
pat: self.lower_pattern(&cx, arm.pat, db, &body, &mut has_lowering_errors),
has_guard: arm.guard.is_some(),
arm_data: (),
};
m_arms.push(m_arm);
if !has_lowering_errors {
Expand All @@ -197,7 +205,15 @@ impl ExprValidator {
return;
}

let report = compute_match_usefulness(&cx, &m_arms, scrut_ty);
let report = match compute_match_usefulness(
rustc_pattern_analysis::MatchCtxt { tycx: &cx },
m_arms.as_slice(),
scrut_ty.clone(),
ValidityConstraint::ValidOnly,
) {
Ok(report) => report,
Err(void) => match void {},
};

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

fn lower_pattern<'p>(
&self,
cx: &MatchCheckCtx<'_, 'p>,
cx: &MatchCheckCtx<'p>,
pat: PatId,
db: &dyn HirDatabase,
body: &Body,
have_errors: &mut bool,
) -> &'p DeconstructedPat<'p> {
let mut patcx = match_check::PatCtxt::new(db, &self.infer, body);
let pattern = patcx.lower_pattern(pat);
let pattern = cx.pattern_arena.alloc(DeconstructedPat::from_pat(cx, &pattern));
let pattern = cx.pattern_arena.alloc(cx.lower_pat(&pattern));
if !patcx.errors.is_empty() {
*have_errors = true;
}
Expand Down Expand Up @@ -364,16 +380,16 @@ fn types_of_subpatterns_do_match(pat: PatId, body: &Body, infer: &InferenceResul
}

fn missing_match_arms<'p>(
cx: &MatchCheckCtx<'_, 'p>,
cx: &MatchCheckCtx<'p>,
scrut_ty: &Ty,
witnesses: Vec<DeconstructedPat<'p>>,
witnesses: Vec<WitnessPat<'p>>,
arms: &[MatchArm],
) -> String {
struct DisplayWitness<'a, 'p>(&'a DeconstructedPat<'p>, &'a MatchCheckCtx<'a, 'p>);
struct DisplayWitness<'a, 'p>(&'a WitnessPat<'p>, &'a MatchCheckCtx<'p>);
impl fmt::Display for DisplayWitness<'_, '_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let DisplayWitness(witness, cx) = *self;
let pat = witness.to_pat(cx);
let pat = cx.hoist_witness_pat(witness);
write!(f, "{}", pat.display(cx.db))
}
}
Expand Down
5 changes: 1 addition & 4 deletions crates/hir-ty/src/diagnostics/match_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
mod pat_util;

pub(crate) mod deconstruct_pat;
pub(crate) mod usefulness;
pub(crate) mod pat_analysis;

use chalk_ir::Mutability;
use hir_def::{
Expand All @@ -27,8 +26,6 @@ use crate::{

use self::pat_util::EnumerateAndAdjustIterator;

pub(crate) use self::usefulness::MatchArm;

#[derive(Clone, Debug)]
pub(crate) enum PatternError {
Unimplemented,
Expand Down
Loading

0 comments on commit 0d52934

Please sign in to comment.