Skip to content

Commit 860637a

Browse files
Normalize projections in evaluated const display and layout calculation
1 parent 6a3ede1 commit 860637a

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ use crate::{
4545
db::{HirDatabase, InternedClosure},
4646
from_assoc_type_id, from_foreign_def_id, from_placeholder_idx,
4747
generics::generics,
48+
infer::normalize,
4849
layout::Layout,
4950
lt_from_placeholder_idx,
5051
mapping::from_chalk,
@@ -657,6 +658,7 @@ fn render_const_scalar(
657658
// infrastructure and have it here as a field on `f`.
658659
let trait_env =
659660
TraitEnvironment::empty(*f.db.crate_graph().crates_in_topological_order().last().unwrap());
661+
let ty = normalize(f.db, trait_env.clone(), ty.clone());
660662
match ty.kind(Interner) {
661663
TyKind::Scalar(s) => match s {
662664
Scalar::Bool => write!(f, "{}", b[0] != 0),

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

+3
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,9 @@ pub fn layout_of_ty_query(
435435
TyKind::Error => return Err(LayoutError::HasErrorType),
436436
TyKind::AssociatedType(id, subst) => {
437437
// Try again with `TyKind::Alias` to normalize the associated type.
438+
// Usually we should not try to normalize `TyKind::AssociatedType`, but layout calculation is used
439+
// in monomorphized MIR where this is okay. If outside monomorphization, this will lead to cycle,
440+
// which we will recover from with an error.
438441
let ty = TyKind::Alias(chalk_ir::AliasTy::Projection(ProjectionTy {
439442
associated_ty_id: *id,
440443
substitution: subst.clone(),

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

+34
Original file line numberDiff line numberDiff line change
@@ -10947,3 +10947,37 @@ pub struct ManuallyDrop$0<T: ?Sized> {
1094710947
"#]],
1094810948
);
1094910949
}
10950+
10951+
#[test]
10952+
fn projection_const() {
10953+
check(
10954+
r#"
10955+
pub trait PublicFlags {
10956+
type Internal;
10957+
}
10958+
10959+
pub struct NoteDialects(<NoteDialects as PublicFlags>::Internal);
10960+
10961+
impl NoteDialects {
10962+
pub const CLAP$0: Self = Self(InternalBitFlags);
10963+
}
10964+
10965+
pub struct InternalBitFlags;
10966+
10967+
impl PublicFlags for NoteDialects {
10968+
type Internal = InternalBitFlags;
10969+
}
10970+
"#,
10971+
expect![[r#"
10972+
*CLAP*
10973+
10974+
```rust
10975+
ra_test_fixture::NoteDialects
10976+
```
10977+
10978+
```rust
10979+
pub const CLAP: Self = NoteDialects(InternalBitFlags)
10980+
```
10981+
"#]],
10982+
);
10983+
}

0 commit comments

Comments
 (0)