Skip to content

Commit a4d3a4a

Browse files
committed
Auto merge of #14068 - HKalbasi:unsize, r=flodiebold
Unsize cast array only on pointer type fix #14000
2 parents 0fcef7f + 0bf0d93 commit a4d3a4a

File tree

4 files changed

+40
-15
lines changed

4 files changed

+40
-15
lines changed

crates/hir-ty/src/autoderef.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ use crate::{
1717

1818
static AUTODEREF_RECURSION_LIMIT: Limit = Limit::new(10);
1919

20+
#[derive(Debug)]
2021
pub(crate) enum AutoderefKind {
2122
Builtin,
2223
Overloaded,
2324
}
2425

26+
#[derive(Debug)]
2527
pub(crate) struct Autoderef<'a, 'db> {
2628
pub(crate) table: &'a mut InferenceTable<'db>,
2729
ty: Ty,

crates/hir-ty/src/method_resolution.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use std::{ops::ControlFlow, sync::Arc};
66

77
use base_db::{CrateId, Edition};
8-
use chalk_ir::{cast::Cast, Mutability, UniverseIndex};
8+
use chalk_ir::{cast::Cast, Mutability, TyKind, UniverseIndex};
99
use hir_def::{
1010
data::ImplData, item_scope::ItemScope, lang_item::LangItem, nameres::DefMap, AssocItemId,
1111
BlockId, ConstId, FunctionId, HasModule, ImplId, ItemContainerId, Lookup, ModuleDefId,
@@ -25,7 +25,7 @@ use crate::{
2525
static_lifetime, to_chalk_trait_id,
2626
utils::all_super_traits,
2727
AdtId, Canonical, CanonicalVarKinds, DebruijnIndex, ForeignDefId, InEnvironment, Interner,
28-
Scalar, Substitution, TraitEnvironment, TraitRef, TraitRefExt, Ty, TyBuilder, TyExt, TyKind,
28+
Scalar, Substitution, TraitEnvironment, TraitRef, TraitRefExt, Ty, TyBuilder, TyExt,
2929
};
3030

3131
/// This is used as a key for indexing impls.
@@ -588,25 +588,31 @@ impl ReceiverAdjustments {
588588
}
589589
}
590590
}
591+
if let Some(m) = self.autoref {
592+
ty = TyKind::Ref(m, static_lifetime(), ty).intern(Interner);
593+
adjust
594+
.push(Adjustment { kind: Adjust::Borrow(AutoBorrow::Ref(m)), target: ty.clone() });
595+
}
591596
if self.unsize_array {
592-
ty = match ty.kind(Interner) {
593-
TyKind::Array(inner, _) => TyKind::Slice(inner.clone()).intern(Interner),
594-
_ => {
595-
never!("unsize_array with non-array {:?}", ty);
596-
ty
597+
ty = 'x: {
598+
if let TyKind::Ref(m, l, inner) = ty.kind(Interner) {
599+
if let TyKind::Array(inner, _) = inner.kind(Interner) {
600+
break 'x TyKind::Ref(
601+
m.clone(),
602+
l.clone(),
603+
TyKind::Slice(inner.clone()).intern(Interner),
604+
)
605+
.intern(Interner);
606+
}
597607
}
608+
never!("unsize_array with non-reference-to-array {:?}", ty);
609+
ty
598610
};
599-
// FIXME this is kind of wrong since the unsize needs to happen to a pointer/reference
600611
adjust.push(Adjustment {
601612
kind: Adjust::Pointer(PointerCast::Unsize),
602613
target: ty.clone(),
603614
});
604615
}
605-
if let Some(m) = self.autoref {
606-
ty = TyKind::Ref(m, static_lifetime(), ty).intern(Interner);
607-
adjust
608-
.push(Adjustment { kind: Adjust::Borrow(AutoBorrow::Ref(m)), target: ty.clone() });
609-
}
610616
(ty, adjust)
611617
}
612618

crates/hir-ty/src/tests/method_resolution.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,14 +1725,13 @@ fn test() {
17251725

17261726
#[test]
17271727
fn receiver_adjustment_unsize_array() {
1728-
// FIXME not quite correct
17291728
check(
17301729
r#"
17311730
//- minicore: slice
17321731
fn test() {
17331732
let a = [1, 2, 3];
17341733
a.len();
1735-
} //^ adjustments: Pointer(Unsize), Borrow(Ref(Not))
1734+
} //^ adjustments: Borrow(Ref(Not)), Pointer(Unsize)
17361735
"#,
17371736
);
17381737
}

crates/test-utils/src/minicore.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,24 @@ pub mod ops {
273273
}
274274
}
275275

276+
impl<T, I, const N: usize> Index<I> for [T; N]
277+
where
278+
I: SliceIndex<[T]>,
279+
{
280+
type Output = I::Output;
281+
fn index(&self, index: I) -> &I::Output {
282+
loop {}
283+
}
284+
}
285+
impl<T, I, const N: usize> IndexMut<I> for [T; N]
286+
where
287+
I: SliceIndex<[T]>,
288+
{
289+
fn index_mut(&mut self, index: I) -> &mut I::Output {
290+
loop {}
291+
}
292+
}
293+
276294
pub unsafe trait SliceIndex<T: ?Sized> {
277295
type Output: ?Sized;
278296
}

0 commit comments

Comments
 (0)