Skip to content

fix: Skip match check on patterns of unexpected TyKind::FnDef #11899

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions crates/hir_ty/src/diagnostics/match_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub(crate) mod deconstruct_pat;
pub(crate) mod usefulness;

use hir_def::{body::Body, expr::PatId, EnumVariantId, LocalFieldId, VariantId};
use stdx::never;
use stdx::{always, never};

use crate::{
db::HirDatabase, infer::BindingMode, InferenceResult, Interner, Substitution, Ty, TyKind,
Expand Down Expand Up @@ -127,7 +127,11 @@ impl<'a> PatCtxt<'a> {
hir_def::expr::Pat::Tuple { ref args, ellipsis } => {
let arity = match *ty.kind(Interner) {
TyKind::Tuple(arity, _) => arity,
_ => panic!("unexpected type for tuple pattern: {:?}", ty),
_ => {
never!("unexpected type for tuple pattern: {:?}", ty);
self.errors.push(PatternError::UnexpectedType);
return Pat { ty: ty.clone(), kind: PatKind::Wild.into() };
}
};
let subpatterns = self.lower_tuple_subpats(args, arity, ellipsis);
PatKind::Leaf { subpatterns }
Expand Down Expand Up @@ -227,11 +231,16 @@ impl<'a> PatCtxt<'a> {
Some(variant_id) => {
if let VariantId::EnumVariantId(enum_variant) = variant_id {
let substs = match ty.kind(Interner) {
TyKind::Adt(_, substs) | TyKind::FnDef(_, substs) => substs.clone(),
TyKind::Error => {
TyKind::Adt(_, substs) => substs.clone(),
kind => {
always!(
matches!(kind, TyKind::FnDef(..) | TyKind::Error),
"inappropriate type for def: {:?}",
ty
);
self.errors.push(PatternError::UnexpectedType);
return PatKind::Wild;
}
_ => panic!("inappropriate type for def: {:?}", ty),
};
PatKind::Variant { substs, enum_variant, subpatterns }
} else {
Expand Down
16 changes: 16 additions & 0 deletions crates/ide_diagnostics/src/handlers/missing_match_arms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,22 @@ fn f(ty: Enum) {
);
}

#[test]
fn unexpected_ty_fndef() {
cov_mark::check!(validate_match_bailed_out);
check_diagnostics(
r"
enum Exp {
Tuple(()),
}
fn f() {
match __unknown {
Exp::Tuple => {}
}
}",
);
}

mod false_negatives {
//! The implementation of match checking here is a work in progress. As we roll this out, we
//! prefer false negatives to false positives (ideally there would be no false positives). This
Expand Down