mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 12:29:21 +00:00
Split pattern inference into more functions
This commit is contained in:
parent
fc2b395e00
commit
ec273c3d12
6 changed files with 234 additions and 172 deletions
|
@ -85,7 +85,7 @@ pub enum ResolveValueResult {
|
||||||
Partial(TypeNs, usize),
|
Partial(TypeNs, usize),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
||||||
pub enum ValueNs {
|
pub enum ValueNs {
|
||||||
ImplSelf(ImplId),
|
ImplSelf(ImplId),
|
||||||
LocalBinding(PatId),
|
LocalBinding(PatId),
|
||||||
|
|
|
@ -144,44 +144,6 @@ impl Default for BindingMode {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Used to generalize patterns and assignee expressions.
|
|
||||||
trait PatLike: Into<ExprOrPatId> + Copy {
|
|
||||||
type BindingMode: Copy;
|
|
||||||
|
|
||||||
fn infer(
|
|
||||||
this: &mut InferenceContext<'_>,
|
|
||||||
id: Self,
|
|
||||||
expected_ty: &Ty,
|
|
||||||
default_bm: Self::BindingMode,
|
|
||||||
) -> Ty;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PatLike for ExprId {
|
|
||||||
type BindingMode = ();
|
|
||||||
|
|
||||||
fn infer(
|
|
||||||
this: &mut InferenceContext<'_>,
|
|
||||||
id: Self,
|
|
||||||
expected_ty: &Ty,
|
|
||||||
_: Self::BindingMode,
|
|
||||||
) -> Ty {
|
|
||||||
this.infer_assignee_expr(id, expected_ty)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PatLike for PatId {
|
|
||||||
type BindingMode = BindingMode;
|
|
||||||
|
|
||||||
fn infer(
|
|
||||||
this: &mut InferenceContext<'_>,
|
|
||||||
id: Self,
|
|
||||||
expected_ty: &Ty,
|
|
||||||
default_bm: Self::BindingMode,
|
|
||||||
) -> Ty {
|
|
||||||
this.infer_pat(id, expected_ty, default_bm)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) struct InferOk<T> {
|
pub(crate) struct InferOk<T> {
|
||||||
value: T,
|
value: T,
|
||||||
|
@ -581,7 +543,7 @@ impl<'a> InferenceContext<'a> {
|
||||||
let ty = self.insert_type_vars(ty);
|
let ty = self.insert_type_vars(ty);
|
||||||
let ty = self.normalize_associated_types_in(ty);
|
let ty = self.normalize_associated_types_in(ty);
|
||||||
|
|
||||||
self.infer_pat(*pat, &ty, BindingMode::default());
|
self.infer_top_pat(*pat, &ty);
|
||||||
}
|
}
|
||||||
let error_ty = &TypeRef::Error;
|
let error_ty = &TypeRef::Error;
|
||||||
let return_ty = if data.has_async_kw() {
|
let return_ty = if data.has_async_kw() {
|
||||||
|
|
|
@ -25,7 +25,9 @@ use syntax::ast::RangeOp;
|
||||||
use crate::{
|
use crate::{
|
||||||
autoderef::{self, Autoderef},
|
autoderef::{self, Autoderef},
|
||||||
consteval,
|
consteval,
|
||||||
infer::{coerce::CoerceMany, find_continuable, BreakableKind},
|
infer::{
|
||||||
|
coerce::CoerceMany, find_continuable, pat::contains_explicit_ref_binding, BreakableKind,
|
||||||
|
},
|
||||||
lower::{
|
lower::{
|
||||||
const_or_path_to_chalk, generic_arg_to_chalk, lower_to_chalk_mutability, ParamLoweringMode,
|
const_or_path_to_chalk, generic_arg_to_chalk, lower_to_chalk_mutability, ParamLoweringMode,
|
||||||
},
|
},
|
||||||
|
@ -39,8 +41,8 @@ use crate::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
coerce::auto_deref_adjust_steps, find_breakable, BindingMode, BreakableContext, Diverges,
|
coerce::auto_deref_adjust_steps, find_breakable, BreakableContext, Diverges, Expectation,
|
||||||
Expectation, InferenceContext, InferenceDiagnostic, TypeMismatch,
|
InferenceContext, InferenceDiagnostic, TypeMismatch,
|
||||||
};
|
};
|
||||||
|
|
||||||
impl<'a> InferenceContext<'a> {
|
impl<'a> InferenceContext<'a> {
|
||||||
|
@ -111,7 +113,7 @@ impl<'a> InferenceContext<'a> {
|
||||||
}
|
}
|
||||||
&Expr::Let { pat, expr } => {
|
&Expr::Let { pat, expr } => {
|
||||||
let input_ty = self.infer_expr(expr, &Expectation::none());
|
let input_ty = self.infer_expr(expr, &Expectation::none());
|
||||||
self.infer_pat(pat, &input_ty, BindingMode::default());
|
self.infer_top_pat(pat, &input_ty);
|
||||||
self.result.standard_types.bool_.clone()
|
self.result.standard_types.bool_.clone()
|
||||||
}
|
}
|
||||||
Expr::Block { statements, tail, label, id: _ } => {
|
Expr::Block { statements, tail, label, id: _ } => {
|
||||||
|
@ -223,7 +225,7 @@ impl<'a> InferenceContext<'a> {
|
||||||
let pat_ty =
|
let pat_ty =
|
||||||
self.resolve_associated_type(into_iter_ty, self.resolve_iterator_item());
|
self.resolve_associated_type(into_iter_ty, self.resolve_iterator_item());
|
||||||
|
|
||||||
self.infer_pat(pat, &pat_ty, BindingMode::default());
|
self.infer_top_pat(pat, &pat_ty);
|
||||||
self.with_breakable_ctx(BreakableKind::Loop, self.err_ty(), label, |this| {
|
self.with_breakable_ctx(BreakableKind::Loop, self.err_ty(), label, |this| {
|
||||||
this.infer_expr(body, &Expectation::HasType(TyBuilder::unit()));
|
this.infer_expr(body, &Expectation::HasType(TyBuilder::unit()));
|
||||||
});
|
});
|
||||||
|
@ -298,7 +300,7 @@ impl<'a> InferenceContext<'a> {
|
||||||
|
|
||||||
// Now go through the argument patterns
|
// Now go through the argument patterns
|
||||||
for (arg_pat, arg_ty) in args.iter().zip(sig_tys) {
|
for (arg_pat, arg_ty) in args.iter().zip(sig_tys) {
|
||||||
self.infer_pat(*arg_pat, &arg_ty, BindingMode::default());
|
self.infer_top_pat(*arg_pat, &arg_ty);
|
||||||
}
|
}
|
||||||
|
|
||||||
let prev_diverges = mem::replace(&mut self.diverges, Diverges::Maybe);
|
let prev_diverges = mem::replace(&mut self.diverges, Diverges::Maybe);
|
||||||
|
@ -395,7 +397,8 @@ impl<'a> InferenceContext<'a> {
|
||||||
|
|
||||||
for arm in arms.iter() {
|
for arm in arms.iter() {
|
||||||
self.diverges = Diverges::Maybe;
|
self.diverges = Diverges::Maybe;
|
||||||
let _pat_ty = self.infer_pat(arm.pat, &input_ty, BindingMode::default());
|
let input_ty = self.resolve_ty_shallow(&input_ty);
|
||||||
|
let _pat_ty = self.infer_top_pat(arm.pat, &input_ty);
|
||||||
if let Some(guard_expr) = arm.guard {
|
if let Some(guard_expr) = arm.guard {
|
||||||
self.infer_expr(
|
self.infer_expr(
|
||||||
guard_expr,
|
guard_expr,
|
||||||
|
@ -1142,27 +1145,33 @@ impl<'a> InferenceContext<'a> {
|
||||||
let decl_ty = type_ref
|
let decl_ty = type_ref
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|tr| self.make_ty(tr))
|
.map(|tr| self.make_ty(tr))
|
||||||
.unwrap_or_else(|| self.err_ty());
|
.unwrap_or_else(|| self.table.new_type_var());
|
||||||
|
|
||||||
// Always use the declared type when specified
|
let ty = if let Some(expr) = initializer {
|
||||||
let mut ty = decl_ty.clone();
|
let ty = if contains_explicit_ref_binding(&self.body, *pat) {
|
||||||
|
self.infer_expr(*expr, &Expectation::has_type(decl_ty.clone()))
|
||||||
|
} else {
|
||||||
|
self.infer_expr_coerce(*expr, &Expectation::has_type(decl_ty.clone()))
|
||||||
|
};
|
||||||
|
if type_ref.is_some() {
|
||||||
|
decl_ty
|
||||||
|
} else {
|
||||||
|
ty
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
decl_ty
|
||||||
|
};
|
||||||
|
|
||||||
if let Some(expr) = initializer {
|
self.infer_top_pat(*pat, &ty);
|
||||||
let actual_ty =
|
|
||||||
self.infer_expr_coerce(*expr, &Expectation::has_type(decl_ty.clone()));
|
|
||||||
if decl_ty.is_unknown() {
|
|
||||||
ty = actual_ty;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(expr) = else_branch {
|
if let Some(expr) = else_branch {
|
||||||
|
let previous_diverges = mem::replace(&mut self.diverges, Diverges::Maybe);
|
||||||
self.infer_expr_coerce(
|
self.infer_expr_coerce(
|
||||||
*expr,
|
*expr,
|
||||||
&Expectation::HasType(self.result.standard_types.never.clone()),
|
&Expectation::HasType(self.result.standard_types.never.clone()),
|
||||||
);
|
);
|
||||||
|
self.diverges = previous_diverges;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.infer_pat(*pat, &ty, BindingMode::default());
|
|
||||||
}
|
}
|
||||||
Statement::Expr { expr, .. } => {
|
Statement::Expr { expr, .. } => {
|
||||||
self.infer_expr(*expr, &Expectation::none());
|
self.infer_expr(*expr, &Expectation::none());
|
||||||
|
|
|
@ -4,7 +4,8 @@ use std::iter::repeat_with;
|
||||||
|
|
||||||
use chalk_ir::Mutability;
|
use chalk_ir::Mutability;
|
||||||
use hir_def::{
|
use hir_def::{
|
||||||
expr::{BindingAnnotation, Expr, Literal, Pat, PatId},
|
body::Body,
|
||||||
|
expr::{BindingAnnotation, Expr, ExprId, ExprOrPatId, Literal, Pat, PatId, RecordFieldPat},
|
||||||
path::Path,
|
path::Path,
|
||||||
};
|
};
|
||||||
use hir_expand::name::Name;
|
use hir_expand::name::Name;
|
||||||
|
@ -17,7 +18,43 @@ use crate::{
|
||||||
static_lifetime, Interner, Scalar, Substitution, Ty, TyBuilder, TyExt, TyKind,
|
static_lifetime, Interner, Scalar, Substitution, Ty, TyBuilder, TyExt, TyKind,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::PatLike;
|
/// Used to generalize patterns and assignee expressions.
|
||||||
|
pub(super) trait PatLike: Into<ExprOrPatId> + Copy {
|
||||||
|
type BindingMode: Copy;
|
||||||
|
|
||||||
|
fn infer(
|
||||||
|
this: &mut InferenceContext<'_>,
|
||||||
|
id: Self,
|
||||||
|
expected_ty: &Ty,
|
||||||
|
default_bm: Self::BindingMode,
|
||||||
|
) -> Ty;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PatLike for ExprId {
|
||||||
|
type BindingMode = ();
|
||||||
|
|
||||||
|
fn infer(
|
||||||
|
this: &mut InferenceContext<'_>,
|
||||||
|
id: Self,
|
||||||
|
expected_ty: &Ty,
|
||||||
|
(): Self::BindingMode,
|
||||||
|
) -> Ty {
|
||||||
|
this.infer_assignee_expr(id, expected_ty)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PatLike for PatId {
|
||||||
|
type BindingMode = BindingMode;
|
||||||
|
|
||||||
|
fn infer(
|
||||||
|
this: &mut InferenceContext<'_>,
|
||||||
|
id: Self,
|
||||||
|
expected_ty: &Ty,
|
||||||
|
default_bm: Self::BindingMode,
|
||||||
|
) -> Ty {
|
||||||
|
this.infer_pat(id, expected_ty, default_bm)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a> InferenceContext<'a> {
|
impl<'a> InferenceContext<'a> {
|
||||||
/// Infers type for tuple struct pattern or its corresponding assignee expression.
|
/// Infers type for tuple struct pattern or its corresponding assignee expression.
|
||||||
|
@ -110,6 +147,7 @@ impl<'a> InferenceContext<'a> {
|
||||||
ellipsis: Option<usize>,
|
ellipsis: Option<usize>,
|
||||||
subs: &[T],
|
subs: &[T],
|
||||||
) -> Ty {
|
) -> Ty {
|
||||||
|
let expected = self.resolve_ty_shallow(expected);
|
||||||
let expectations = match expected.as_tuple() {
|
let expectations = match expected.as_tuple() {
|
||||||
Some(parameters) => &*parameters.as_slice(Interner),
|
Some(parameters) => &*parameters.as_slice(Interner),
|
||||||
_ => &[],
|
_ => &[],
|
||||||
|
@ -143,12 +181,11 @@ impl<'a> InferenceContext<'a> {
|
||||||
.intern(Interner)
|
.intern(Interner)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn infer_pat(
|
pub(super) fn infer_top_pat(&mut self, pat: PatId, expected: &Ty) -> Ty {
|
||||||
&mut self,
|
self.infer_pat(pat, expected, BindingMode::default())
|
||||||
pat: PatId,
|
}
|
||||||
expected: &Ty,
|
|
||||||
mut default_bm: BindingMode,
|
fn infer_pat(&mut self, pat: PatId, expected: &Ty, mut default_bm: BindingMode) -> Ty {
|
||||||
) -> Ty {
|
|
||||||
let mut expected = self.resolve_ty_shallow(expected);
|
let mut expected = self.resolve_ty_shallow(expected);
|
||||||
|
|
||||||
if is_non_ref_pat(self.body, pat) {
|
if is_non_ref_pat(self.body, pat) {
|
||||||
|
@ -183,25 +220,17 @@ impl<'a> InferenceContext<'a> {
|
||||||
self.infer_tuple_pat_like(&expected, default_bm, *ellipsis, args)
|
self.infer_tuple_pat_like(&expected, default_bm, *ellipsis, args)
|
||||||
}
|
}
|
||||||
Pat::Or(pats) => {
|
Pat::Or(pats) => {
|
||||||
if let Some((first_pat, rest)) = pats.split_first() {
|
for pat in pats.iter() {
|
||||||
let ty = self.infer_pat(*first_pat, &expected, default_bm);
|
|
||||||
for pat in rest {
|
|
||||||
self.infer_pat(*pat, &expected, default_bm);
|
self.infer_pat(*pat, &expected, default_bm);
|
||||||
}
|
}
|
||||||
ty
|
expected.clone()
|
||||||
} else {
|
|
||||||
self.err_ty()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Pat::Ref { pat, mutability } => {
|
|
||||||
let mutability = lower_to_chalk_mutability(*mutability);
|
|
||||||
let expectation = match expected.as_reference() {
|
|
||||||
Some((inner_ty, _lifetime, exp_mut)) => inner_ty.clone(),
|
|
||||||
_ => self.result.standard_types.unknown.clone(),
|
|
||||||
};
|
|
||||||
let subty = self.infer_pat(*pat, &expectation, default_bm);
|
|
||||||
TyKind::Ref(mutability, static_lifetime(), subty).intern(Interner)
|
|
||||||
}
|
}
|
||||||
|
&Pat::Ref { pat, mutability } => self.infer_ref_pat(
|
||||||
|
pat,
|
||||||
|
lower_to_chalk_mutability(mutability),
|
||||||
|
&expected,
|
||||||
|
default_bm,
|
||||||
|
),
|
||||||
Pat::TupleStruct { path: p, args: subpats, ellipsis } => self
|
Pat::TupleStruct { path: p, args: subpats, ellipsis } => self
|
||||||
.infer_tuple_struct_pat_like(
|
.infer_tuple_struct_pat_like(
|
||||||
p.as_deref(),
|
p.as_deref(),
|
||||||
|
@ -221,91 +250,17 @@ impl<'a> InferenceContext<'a> {
|
||||||
self.infer_path(&resolver, path, pat.into()).unwrap_or_else(|| self.err_ty())
|
self.infer_path(&resolver, path, pat.into()).unwrap_or_else(|| self.err_ty())
|
||||||
}
|
}
|
||||||
Pat::Bind { mode, name: _, subpat } => {
|
Pat::Bind { mode, name: _, subpat } => {
|
||||||
let mode = if mode == &BindingAnnotation::Unannotated {
|
return self.infer_bind_pat(pat, *mode, default_bm, *subpat, &expected);
|
||||||
default_bm
|
|
||||||
} else {
|
|
||||||
BindingMode::convert(*mode)
|
|
||||||
};
|
|
||||||
self.result.pat_binding_modes.insert(pat, mode);
|
|
||||||
|
|
||||||
let inner_ty = match subpat {
|
|
||||||
Some(subpat) => self.infer_pat(*subpat, &expected, default_bm),
|
|
||||||
None => expected,
|
|
||||||
};
|
|
||||||
let inner_ty = self.insert_type_vars_shallow(inner_ty);
|
|
||||||
|
|
||||||
let bound_ty = match mode {
|
|
||||||
BindingMode::Ref(mutability) => {
|
|
||||||
TyKind::Ref(mutability, static_lifetime(), inner_ty.clone())
|
|
||||||
.intern(Interner)
|
|
||||||
}
|
|
||||||
BindingMode::Move => inner_ty.clone(),
|
|
||||||
};
|
|
||||||
self.write_pat_ty(pat, bound_ty);
|
|
||||||
return inner_ty;
|
|
||||||
}
|
}
|
||||||
Pat::Slice { prefix, slice, suffix } => {
|
Pat::Slice { prefix, slice, suffix } => {
|
||||||
let elem_ty = match expected.kind(Interner) {
|
self.infer_slice_pat(&expected, prefix, slice, suffix, default_bm)
|
||||||
TyKind::Array(st, _) | TyKind::Slice(st) => st.clone(),
|
|
||||||
_ => self.err_ty(),
|
|
||||||
};
|
|
||||||
|
|
||||||
for &pat_id in prefix.iter().chain(suffix.iter()) {
|
|
||||||
self.infer_pat(pat_id, &elem_ty, default_bm);
|
|
||||||
}
|
|
||||||
|
|
||||||
if let &Some(slice_pat_id) = slice {
|
|
||||||
let rest_pat_ty = match expected.kind(Interner) {
|
|
||||||
TyKind::Array(_, length) => {
|
|
||||||
let len = try_const_usize(length);
|
|
||||||
let len = len.and_then(|len| {
|
|
||||||
len.checked_sub((prefix.len() + suffix.len()) as u128)
|
|
||||||
});
|
|
||||||
TyKind::Array(
|
|
||||||
elem_ty.clone(),
|
|
||||||
usize_const(self.db, len, self.resolver.krate()),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
_ => TyKind::Slice(elem_ty.clone()),
|
|
||||||
}
|
|
||||||
.intern(Interner);
|
|
||||||
self.infer_pat(slice_pat_id, &rest_pat_ty, default_bm);
|
|
||||||
}
|
|
||||||
|
|
||||||
match expected.kind(Interner) {
|
|
||||||
TyKind::Array(_, const_) => TyKind::Array(elem_ty, const_.clone()),
|
|
||||||
_ => TyKind::Slice(elem_ty),
|
|
||||||
}
|
|
||||||
.intern(Interner)
|
|
||||||
}
|
}
|
||||||
Pat::Wild => expected.clone(),
|
Pat::Wild => expected.clone(),
|
||||||
Pat::Range { start, end } => {
|
Pat::Range { start, end } => {
|
||||||
let start_ty = self.infer_expr(*start, &Expectation::has_type(expected.clone()));
|
let start_ty = self.infer_expr(*start, &Expectation::has_type(expected.clone()));
|
||||||
self.infer_expr(*end, &Expectation::has_type(start_ty))
|
self.infer_expr(*end, &Expectation::has_type(start_ty))
|
||||||
}
|
}
|
||||||
&Pat::Lit(expr) => {
|
&Pat::Lit(expr) => self.infer_lit_pat(expr, &expected),
|
||||||
// FIXME: using `Option` here is a workaround until we can use if-let chains in stable.
|
|
||||||
let mut pat_ty = None;
|
|
||||||
|
|
||||||
// Like slice patterns, byte string patterns can denote both `&[u8; N]` and `&[u8]`.
|
|
||||||
if let Expr::Literal(Literal::ByteString(_)) = self.body[expr] {
|
|
||||||
if let Some((inner, ..)) = expected.as_reference() {
|
|
||||||
let inner = self.resolve_ty_shallow(inner);
|
|
||||||
if matches!(inner.kind(Interner), TyKind::Slice(_)) {
|
|
||||||
let elem_ty = TyKind::Scalar(Scalar::Uint(UintTy::U8)).intern(Interner);
|
|
||||||
let slice_ty = TyKind::Slice(elem_ty).intern(Interner);
|
|
||||||
let ty = TyKind::Ref(Mutability::Not, static_lifetime(), slice_ty)
|
|
||||||
.intern(Interner);
|
|
||||||
self.write_expr_ty(expr, ty.clone());
|
|
||||||
pat_ty = Some(ty);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pat_ty.unwrap_or_else(|| {
|
|
||||||
self.infer_expr(expr, &Expectation::has_type(expected.clone()))
|
|
||||||
})
|
|
||||||
}
|
|
||||||
Pat::Box { inner } => match self.resolve_boxed_box() {
|
Pat::Box { inner } => match self.resolve_boxed_box() {
|
||||||
Some(box_adt) => {
|
Some(box_adt) => {
|
||||||
let (inner_ty, alloc_ty) = match expected.as_adt() {
|
let (inner_ty, alloc_ty) = match expected.as_adt() {
|
||||||
|
@ -341,6 +296,109 @@ impl<'a> InferenceContext<'a> {
|
||||||
self.write_pat_ty(pat, ty.clone());
|
self.write_pat_ty(pat, ty.clone());
|
||||||
ty
|
ty
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn infer_ref_pat(
|
||||||
|
&mut self,
|
||||||
|
pat: PatId,
|
||||||
|
mutability: Mutability,
|
||||||
|
expected: &Ty,
|
||||||
|
default_bm: BindingMode,
|
||||||
|
) -> Ty {
|
||||||
|
let expectation = match expected.as_reference() {
|
||||||
|
Some((inner_ty, _lifetime, _exp_mut)) => inner_ty.clone(),
|
||||||
|
_ => self.result.standard_types.unknown.clone(),
|
||||||
|
};
|
||||||
|
let subty = self.infer_pat(pat, &expectation, default_bm);
|
||||||
|
TyKind::Ref(mutability, static_lifetime(), subty).intern(Interner)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn infer_bind_pat(
|
||||||
|
&mut self,
|
||||||
|
pat: PatId,
|
||||||
|
mode: BindingAnnotation,
|
||||||
|
default_bm: BindingMode,
|
||||||
|
subpat: Option<PatId>,
|
||||||
|
expected: &Ty,
|
||||||
|
) -> Ty {
|
||||||
|
let mode = if mode == BindingAnnotation::Unannotated {
|
||||||
|
default_bm
|
||||||
|
} else {
|
||||||
|
BindingMode::convert(mode)
|
||||||
|
};
|
||||||
|
self.result.pat_binding_modes.insert(pat, mode);
|
||||||
|
|
||||||
|
let inner_ty = match subpat {
|
||||||
|
Some(subpat) => self.infer_pat(subpat, &expected, default_bm),
|
||||||
|
None => expected.clone(),
|
||||||
|
};
|
||||||
|
let inner_ty = self.insert_type_vars_shallow(inner_ty);
|
||||||
|
|
||||||
|
let bound_ty = match mode {
|
||||||
|
BindingMode::Ref(mutability) => {
|
||||||
|
TyKind::Ref(mutability, static_lifetime(), inner_ty.clone()).intern(Interner)
|
||||||
|
}
|
||||||
|
BindingMode::Move => inner_ty.clone(),
|
||||||
|
};
|
||||||
|
self.write_pat_ty(pat, bound_ty);
|
||||||
|
return inner_ty;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn infer_slice_pat(
|
||||||
|
&mut self,
|
||||||
|
expected: &Ty,
|
||||||
|
prefix: &[PatId],
|
||||||
|
slice: &Option<PatId>,
|
||||||
|
suffix: &[PatId],
|
||||||
|
default_bm: BindingMode,
|
||||||
|
) -> Ty {
|
||||||
|
let elem_ty = match expected.kind(Interner) {
|
||||||
|
TyKind::Array(st, _) | TyKind::Slice(st) => st.clone(),
|
||||||
|
_ => self.err_ty(),
|
||||||
|
};
|
||||||
|
|
||||||
|
for &pat_id in prefix.iter().chain(suffix.iter()) {
|
||||||
|
self.infer_pat(pat_id, &elem_ty, default_bm);
|
||||||
|
}
|
||||||
|
|
||||||
|
if let &Some(slice_pat_id) = slice {
|
||||||
|
let rest_pat_ty = match expected.kind(Interner) {
|
||||||
|
TyKind::Array(_, length) => {
|
||||||
|
let len = try_const_usize(length);
|
||||||
|
let len =
|
||||||
|
len.and_then(|len| len.checked_sub((prefix.len() + suffix.len()) as u128));
|
||||||
|
TyKind::Array(elem_ty.clone(), usize_const(self.db, len, self.resolver.krate()))
|
||||||
|
}
|
||||||
|
_ => TyKind::Slice(elem_ty.clone()),
|
||||||
|
}
|
||||||
|
.intern(Interner);
|
||||||
|
self.infer_pat(slice_pat_id, &rest_pat_ty, default_bm);
|
||||||
|
}
|
||||||
|
|
||||||
|
match expected.kind(Interner) {
|
||||||
|
TyKind::Array(_, const_) => TyKind::Array(elem_ty, const_.clone()),
|
||||||
|
_ => TyKind::Slice(elem_ty),
|
||||||
|
}
|
||||||
|
.intern(Interner)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn infer_lit_pat(&mut self, expr: ExprId, expected: &Ty) -> Ty {
|
||||||
|
// Like slice patterns, byte string patterns can denote both `&[u8; N]` and `&[u8]`.
|
||||||
|
if let Expr::Literal(Literal::ByteString(_)) = self.body[expr] {
|
||||||
|
if let Some((inner, ..)) = expected.as_reference() {
|
||||||
|
let inner = self.resolve_ty_shallow(inner);
|
||||||
|
if matches!(inner.kind(Interner), TyKind::Slice(_)) {
|
||||||
|
let elem_ty = TyKind::Scalar(Scalar::Uint(UintTy::U8)).intern(Interner);
|
||||||
|
let slice_ty = TyKind::Slice(elem_ty).intern(Interner);
|
||||||
|
let ty =
|
||||||
|
TyKind::Ref(Mutability::Not, static_lifetime(), slice_ty).intern(Interner);
|
||||||
|
self.write_expr_ty(expr, ty.clone());
|
||||||
|
return ty;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self.infer_expr(expr, &Expectation::has_type(expected.clone()))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_non_ref_pat(body: &hir_def::body::Body, pat: PatId) -> bool {
|
fn is_non_ref_pat(body: &hir_def::body::Body, pat: PatId) -> bool {
|
||||||
|
@ -365,3 +423,41 @@ fn is_non_ref_pat(body: &hir_def::body::Body, pat: PatId) -> bool {
|
||||||
Pat::Wild | Pat::Bind { .. } | Pat::Ref { .. } | Pat::Box { .. } | Pat::Missing => false,
|
Pat::Wild | Pat::Bind { .. } | Pat::Ref { .. } | Pat::Box { .. } | Pat::Missing => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(super) fn contains_explicit_ref_binding(body: &Body, pat_id: PatId) -> bool {
|
||||||
|
let mut res = false;
|
||||||
|
walk_pats(body, pat_id, &mut |pat| {
|
||||||
|
res |= matches!(pat, Pat::Bind { mode: BindingAnnotation::Ref, .. })
|
||||||
|
});
|
||||||
|
res
|
||||||
|
}
|
||||||
|
|
||||||
|
fn walk_pats(body: &Body, pat_id: PatId, f: &mut impl FnMut(&Pat)) {
|
||||||
|
let pat = &body[pat_id];
|
||||||
|
f(pat);
|
||||||
|
match pat {
|
||||||
|
Pat::Range { .. }
|
||||||
|
| Pat::Lit(..)
|
||||||
|
| Pat::Path(..)
|
||||||
|
| Pat::ConstBlock(..)
|
||||||
|
| Pat::Wild
|
||||||
|
| Pat::Missing => {}
|
||||||
|
&Pat::Bind { subpat, .. } => {
|
||||||
|
if let Some(subpat) = subpat {
|
||||||
|
walk_pats(body, subpat, f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Pat::Or(args) | Pat::Tuple { args, .. } | Pat::TupleStruct { args, .. } => {
|
||||||
|
args.iter().copied().for_each(|p| walk_pats(body, p, f));
|
||||||
|
}
|
||||||
|
Pat::Ref { pat, .. } => walk_pats(body, *pat, f),
|
||||||
|
Pat::Slice { prefix, slice, suffix } => {
|
||||||
|
let total_iter = prefix.iter().chain(slice.iter()).chain(suffix.iter());
|
||||||
|
total_iter.copied().for_each(|p| walk_pats(body, p, f));
|
||||||
|
}
|
||||||
|
Pat::Record { args, .. } => {
|
||||||
|
args.iter().for_each(|RecordFieldPat { pat, .. }| walk_pats(body, *pat, f));
|
||||||
|
}
|
||||||
|
Pat::Box { inner } => walk_pats(body, *inner, f),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -953,9 +953,9 @@ fn main() {
|
||||||
42..51 'true | ()': bool
|
42..51 'true | ()': bool
|
||||||
49..51 '()': ()
|
49..51 '()': ()
|
||||||
57..59 '{}': ()
|
57..59 '{}': ()
|
||||||
68..80 '(() | true,)': ((),)
|
68..80 '(() | true,)': (bool,)
|
||||||
69..71 '()': ()
|
69..71 '()': ()
|
||||||
69..78 '() | true': ()
|
69..78 '() | true': bool
|
||||||
74..78 'true': bool
|
74..78 'true': bool
|
||||||
74..78 'true': bool
|
74..78 'true': bool
|
||||||
84..86 '{}': ()
|
84..86 '{}': ()
|
||||||
|
@ -964,19 +964,15 @@ fn main() {
|
||||||
96..102 '_ | ()': bool
|
96..102 '_ | ()': bool
|
||||||
100..102 '()': ()
|
100..102 '()': ()
|
||||||
108..110 '{}': ()
|
108..110 '{}': ()
|
||||||
119..128 '(() | _,)': ((),)
|
119..128 '(() | _,)': (bool,)
|
||||||
120..122 '()': ()
|
120..122 '()': ()
|
||||||
120..126 '() | _': ()
|
120..126 '() | _': bool
|
||||||
125..126 '_': bool
|
125..126 '_': bool
|
||||||
132..134 '{}': ()
|
132..134 '{}': ()
|
||||||
49..51: expected bool, got ()
|
49..51: expected bool, got ()
|
||||||
68..80: expected (bool,), got ((),)
|
|
||||||
69..71: expected bool, got ()
|
69..71: expected bool, got ()
|
||||||
69..78: expected bool, got ()
|
|
||||||
100..102: expected bool, got ()
|
100..102: expected bool, got ()
|
||||||
119..128: expected (bool,), got ((),)
|
|
||||||
120..122: expected bool, got ()
|
120..122: expected bool, got ()
|
||||||
120..126: expected bool, got ()
|
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -606,14 +606,13 @@ fn a() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn bug() {
|
fn let_stmt_explicit_ty() {
|
||||||
check_with_config(
|
check_with_config(
|
||||||
InlayHintsConfig { adjustment_hints: AdjustmentHints::Always, ..DISABLED_CONFIG },
|
InlayHintsConfig { adjustment_hints: AdjustmentHints::Always, ..DISABLED_CONFIG },
|
||||||
r#"
|
r#"
|
||||||
fn main() {
|
fn main() {
|
||||||
// These should be identical, but they are not...
|
|
||||||
|
|
||||||
let () = return;
|
let () = return;
|
||||||
|
//^^^^^^<never-to-any>
|
||||||
let (): () = return;
|
let (): () = return;
|
||||||
//^^^^^^<never-to-any>
|
//^^^^^^<never-to-any>
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue