Skip to content

Commit c4e8c34

Browse files
authored
Merge pull request #20232 from ShoyuVanilla/issue-20225
fix: Normalize projection types before calculating memory maps
2 parents 928bfbf + f15dfa8 commit c4e8c34

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

src/tools/rust-analyzer/crates/hir-ty/src/display.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,14 @@ fn render_const_scalar(
795795
let Some(bytes) = memory_map.get(addr, size_one * count) else {
796796
return f.write_str("<ref-data-not-available>");
797797
};
798+
let expected_len = count * size_one;
799+
if bytes.len() < expected_len {
800+
never!(
801+
"Memory map size is too small. Expected {expected_len}, got {}",
802+
bytes.len(),
803+
);
804+
return f.write_str("<layout-error>");
805+
}
798806
f.write_str("&[")?;
799807
let mut first = true;
800808
for i in 0..count {

src/tools/rust-analyzer/crates/hir-ty/src/mir/eval.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ use syntax::{SyntaxNodePtr, TextRange};
3131
use triomphe::Arc;
3232

3333
use crate::{
34-
CallableDefId, ClosureId, ComplexMemoryMap, Const, ConstData, ConstScalar, FnDefId, Interner,
35-
MemoryMap, Substitution, ToChalk, TraitEnvironment, Ty, TyBuilder, TyExt, TyKind,
34+
AliasTy, CallableDefId, ClosureId, ComplexMemoryMap, Const, ConstData, ConstScalar, FnDefId,
35+
Interner, MemoryMap, Substitution, ToChalk, TraitEnvironment, Ty, TyBuilder, TyExt, TyKind,
3636
consteval::{ConstEvalError, intern_const_scalar, try_const_usize},
3737
db::{HirDatabase, InternedClosure},
3838
display::{ClosureStyle, DisplayTarget, HirDisplay},
@@ -2195,7 +2195,7 @@ impl Evaluator<'_> {
21952195
}
21962196
}
21972197
}
2198-
chalk_ir::TyKind::Array(inner, len) => {
2198+
TyKind::Array(inner, len) => {
21992199
let len = match try_const_usize(this.db, len) {
22002200
Some(it) => it as usize,
22012201
None => not_supported!("non evaluatable array len in patching addresses"),
@@ -2213,7 +2213,7 @@ impl Evaluator<'_> {
22132213
)?;
22142214
}
22152215
}
2216-
chalk_ir::TyKind::Tuple(_, subst) => {
2216+
TyKind::Tuple(_, subst) => {
22172217
let layout = this.layout(ty)?;
22182218
for (id, ty) in subst.iter(Interner).enumerate() {
22192219
let ty = ty.assert_ty_ref(Interner); // Tuple only has type argument
@@ -2229,7 +2229,7 @@ impl Evaluator<'_> {
22292229
)?;
22302230
}
22312231
}
2232-
chalk_ir::TyKind::Adt(adt, subst) => match adt.0 {
2232+
TyKind::Adt(adt, subst) => match adt.0 {
22332233
AdtId::StructId(s) => {
22342234
let data = s.fields(this.db);
22352235
let layout = this.layout(ty)?;
@@ -2280,6 +2280,10 @@ impl Evaluator<'_> {
22802280
}
22812281
AdtId::UnionId(_) => (),
22822282
},
2283+
TyKind::Alias(AliasTy::Projection(proj)) => {
2284+
let ty = this.db.normalize_projection(proj.clone(), this.trait_env.clone());
2285+
rec(this, bytes, &ty, locals, mm, stack_depth_limit - 1)?;
2286+
}
22832287
_ => (),
22842288
}
22852289
Ok(())

src/tools/rust-analyzer/crates/ide/src/hover/tests.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10985,3 +10985,41 @@ fn has_docs$0() {}
1098510985
"#]],
1098610986
);
1098710987
}
10988+
10989+
#[test]
10990+
fn regression_20225() {
10991+
check(
10992+
r#"
10993+
//- minicore: coerce_unsized
10994+
trait Trait {
10995+
type Type<'a, T: ?Sized + 'a>;
10996+
}
10997+
10998+
enum Borrowed {}
10999+
11000+
impl Trait for Borrowed {
11001+
type Type<'a, T: ?Sized + 'a> = &'a T;
11002+
}
11003+
11004+
enum Enum<'a, T: Trait + 'a> {
11005+
Variant1(T::Type<'a, [Enum<'a, T>]>),
11006+
Variant2,
11007+
}
11008+
11009+
impl Enum<'_, Borrowed> {
11010+
const CONSTANT$0: Self = Self::Variant1(&[Self::Variant2]);
11011+
}
11012+
"#,
11013+
expect![[r#"
11014+
*CONSTANT*
11015+
11016+
```rust
11017+
ra_test_fixture::Enum
11018+
```
11019+
11020+
```rust
11021+
const CONSTANT: Self = Variant1(&[Variant2])
11022+
```
11023+
"#]],
11024+
);
11025+
}

0 commit comments

Comments
 (0)