Normalize projections in evaluated const display and layout calculation

This commit is contained in:
Chayim Refael Friedman 2025-02-27 11:21:01 +02:00
parent bd0289e0e9
commit 1cd9e683e8
3 changed files with 39 additions and 0 deletions

View file

@ -45,6 +45,7 @@ use crate::{
db::{HirDatabase, InternedClosure},
from_assoc_type_id, from_foreign_def_id, from_placeholder_idx,
generics::generics,
infer::normalize,
layout::Layout,
lt_from_placeholder_idx,
mapping::from_chalk,
@ -657,6 +658,7 @@ fn render_const_scalar(
// infrastructure and have it here as a field on `f`.
let trait_env =
TraitEnvironment::empty(*f.db.crate_graph().crates_in_topological_order().last().unwrap());
let ty = normalize(f.db, trait_env.clone(), ty.clone());
match ty.kind(Interner) {
TyKind::Scalar(s) => match s {
Scalar::Bool => write!(f, "{}", b[0] != 0),

View file

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

View file

@ -10947,3 +10947,37 @@ pub struct ManuallyDrop$0<T: ?Sized> {
"#]],
);
}
#[test]
fn projection_const() {
check(
r#"
pub trait PublicFlags {
type Internal;
}
pub struct NoteDialects(<NoteDialects as PublicFlags>::Internal);
impl NoteDialects {
pub const CLAP$0: Self = Self(InternalBitFlags);
}
pub struct InternalBitFlags;
impl PublicFlags for NoteDialects {
type Internal = InternalBitFlags;
}
"#,
expect![[r#"
*CLAP*
```rust
ra_test_fixture::NoteDialects
```
```rust
pub const CLAP: Self = NoteDialects(InternalBitFlags)
```
"#]],
);
}