Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
38 changes: 36 additions & 2 deletions crates/hir_ty/src/method_resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use rustc_hash::{FxHashMap, FxHashSet};

use crate::{
autoderef,
consteval::{self, ConstExt},
db::HirDatabase,
from_foreign_def_id,
primitive::{self, FloatTy, IntTy, UintTy},
Expand Down Expand Up @@ -725,7 +726,27 @@ fn iterate_inherent_methods(
for krate in def_crates {
let impls = db.inherent_impls_in_crate(krate);

for &impl_def in impls.for_self_ty(&self_ty.value) {
let impls_for_self_ty = impls.for_self_ty(&self_ty.value).iter().chain(
{
if let TyKind::Array(parameters, array_len) = self_ty.value.kind(&Interner) {
if !array_len.is_unknown() {
let unknown_array_len_ty =
TyKind::Array(parameters.clone(), consteval::usize_const(None))
.intern(&Interner);

Some(impls.for_self_ty(&unknown_array_len_ty))
} else {
None
}
} else {
None
}
}
.into_iter()
.flatten(),
);

for &impl_def in impls_for_self_ty {
for &item in db.impl_data(impl_def).items.iter() {
if !is_valid_candidate(
db,
Expand Down Expand Up @@ -803,7 +824,20 @@ fn is_valid_candidate(
None => return false,
};
if transformed_receiver_ty != receiver_ty.value {
return false;
match (
transformed_receiver_ty.kind(&Interner),
receiver_ty.value.kind(&Interner),
) {
(
TyKind::Array(transformed_array_ty, transformed_array_len),
TyKind::Array(receiver_array_ty, receiver_array_len),
) if transformed_array_ty == receiver_array_ty
&& transformed_array_len.is_unknown()
&& !receiver_array_len.is_unknown() => {}
_ => {
return false;
}
}
}
}
if let Some(from_module) = visible_from_module {
Expand Down
29 changes: 29 additions & 0 deletions crates/hir_ty/src/tests/method_resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,35 @@ fn f() {
);
}

#[test]
fn resolve_const_generic_array_methods() {
check_types(
r#"
#[lang = "array"]
impl<T, const N: usize> [T; N] {
pub fn map<F, U>(self, f: F) -> [U; N]
where
F: FnMut(T) -> U,
{ loop {} }
}

#[lang = "slice"]
impl<T> [T] {
pub fn map<F, U>(self, f: F) -> &[U]
where
F: FnMut(T) -> U,
{ loop {} }
}

fn f() {
let v = [1, 2].map::<_, usize>(|x| -> x * 2);
v;
//^ [usize; _]
}
"#,
);
}

#[test]
fn skip_array_during_method_dispatch() {
check_types(
Expand Down