Skip to content

Commit

Permalink
Normalize projections in evaluated const display and layout calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
ChayimFriedman2 committed Feb 26, 2025
1 parent 8925544 commit 4b11fdb
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
2 changes: 2 additions & 0 deletions crates/hir-ty/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down
8 changes: 6 additions & 2 deletions crates/hir-ty/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use triomphe::Arc;
use crate::{
consteval::try_const_usize,
db::{HirDatabase, InternedClosure},
display::HirDisplay,
infer::normalize,
layout::adt::struct_variant_idx,
utils::ClosureSubst,
Expand Down Expand Up @@ -214,7 +215,9 @@ pub fn layout_of_ty_query(
};
let dl = &*target;
let cx = LayoutCx::new(dl);
eprintln!("ty before={}, {ty:?}", ty.display_test(db));
let ty = normalize(db, trait_env.clone(), ty);
eprintln!("ty after={}, {ty:?}", ty.display_test(db));
let result = match ty.kind(Interner) {
TyKind::Adt(AdtId(def), subst) => {
if let hir_def::AdtId::StructId(s) = def {
Expand Down Expand Up @@ -451,11 +454,12 @@ pub fn layout_of_ty_query(
}

pub fn layout_of_ty_recover(
_: &dyn HirDatabase,
_: &Cycle,
db: &dyn HirDatabase,
cycle: &Cycle,
_: &Ty,
_: &Arc<TraitEnvironment>,
) -> Result<Arc<Layout>, LayoutError> {
dbg!(cycle.debug(db));
Err(LayoutError::RecursiveTypeWithoutIndirection)
}

Expand Down
5 changes: 3 additions & 2 deletions crates/hir-ty/src/layout/adt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,13 @@ fn layout_scalar_valid_range(db: &dyn HirDatabase, def: AdtId) -> (Bound<u128>,
}

pub fn layout_of_adt_recover(
_: &dyn HirDatabase,
_: &Cycle,
db: &dyn HirDatabase,
cycle: &Cycle,
_: &AdtId,
_: &Substitution,
_: &Arc<TraitEnvironment>,
) -> Result<Arc<Layout>, LayoutError> {
dbg!(cycle.debug(db));
Err(LayoutError::RecursiveTypeWithoutIndirection)
}

Expand Down
34 changes: 34 additions & 0 deletions crates/ide/src/hover/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```
"#]],
);
}

0 comments on commit 4b11fdb

Please sign in to comment.