mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 06:11:35 +00:00
fix: introduce new type var when expectation for ref pat is not ref
This commit is contained in:
parent
a04d8456be
commit
01f42d2405
2 changed files with 26 additions and 4 deletions
|
@ -313,16 +313,23 @@ impl<'a> InferenceContext<'a> {
|
||||||
|
|
||||||
fn infer_ref_pat(
|
fn infer_ref_pat(
|
||||||
&mut self,
|
&mut self,
|
||||||
pat: PatId,
|
inner_pat: PatId,
|
||||||
mutability: Mutability,
|
mutability: Mutability,
|
||||||
expected: &Ty,
|
expected: &Ty,
|
||||||
default_bm: BindingMode,
|
default_bm: BindingMode,
|
||||||
) -> Ty {
|
) -> Ty {
|
||||||
let expectation = match expected.as_reference() {
|
let expectation = match expected.as_reference() {
|
||||||
Some((inner_ty, _lifetime, _exp_mut)) => inner_ty.clone(),
|
Some((inner_ty, _lifetime, _exp_mut)) => inner_ty.clone(),
|
||||||
_ => self.result.standard_types.unknown.clone(),
|
None => {
|
||||||
|
let inner_ty = self.table.new_type_var();
|
||||||
|
let ref_ty =
|
||||||
|
TyKind::Ref(mutability, static_lifetime(), inner_ty.clone()).intern(Interner);
|
||||||
|
// Unification failure will be reported by the caller.
|
||||||
|
self.unify(&ref_ty, expected);
|
||||||
|
inner_ty
|
||||||
|
}
|
||||||
};
|
};
|
||||||
let subty = self.infer_pat(pat, &expectation, default_bm);
|
let subty = self.infer_pat(inner_pat, &expectation, default_bm);
|
||||||
TyKind::Ref(mutability, static_lifetime(), subty).intern(Interner)
|
TyKind::Ref(mutability, static_lifetime(), subty).intern(Interner)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use expect_test::expect;
|
use expect_test::expect;
|
||||||
|
|
||||||
use super::{check, check_infer, check_infer_with_mismatches, check_types};
|
use super::{check, check_infer, check_infer_with_mismatches, check_no_mismatches, check_types};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn infer_pattern() {
|
fn infer_pattern() {
|
||||||
|
@ -240,6 +240,21 @@ fn infer_pattern_match_ergonomics_ref() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn ref_pat_with_inference_variable() {
|
||||||
|
check_no_mismatches(
|
||||||
|
r#"
|
||||||
|
enum E { A }
|
||||||
|
fn test() {
|
||||||
|
let f = |e| match e {
|
||||||
|
&E::A => {}
|
||||||
|
};
|
||||||
|
f(&E::A);
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn infer_pattern_match_slice() {
|
fn infer_pattern_match_slice() {
|
||||||
check_infer(
|
check_infer(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue