Pass the target crate in HirFormatter

This is required to format evaluated consts, because we need trait env, and it needs the crate (currently it uses the last crate in topological order, which is wrong, the next commit will fix that).
This commit is contained in:
Chayim Refael Friedman 2025-02-27 14:19:54 +02:00
parent 1cd9e683e8
commit 2fc0dc0f13
62 changed files with 842 additions and 517 deletions

View file

@ -1,12 +1,11 @@
use std::fmt;
use hir::{Field, HirDisplay, Layout, Semantics, Type};
use hir::{DisplayTarget, Field, HirDisplay, Layout, Semantics, Type};
use ide_db::{
defs::Definition,
helpers::{get_definition, pick_best_token},
RootDatabase,
};
use span::Edition;
use syntax::{AstNode, SyntaxKind};
use crate::FilePosition;
@ -84,10 +83,7 @@ pub(crate) fn view_memory_layout(
) -> Option<RecursiveMemoryLayout> {
let sema = Semantics::new(db);
let file = sema.parse_guess_edition(position.file_id);
let edition = sema
.attach_first_edition(position.file_id)
.map(|it| it.edition())
.unwrap_or(Edition::CURRENT);
let display_target = sema.first_crate_or_default(position.file_id).to_display_target(db);
let token =
pick_best_token(file.syntax().token_at_offset(position.offset), |kind| match kind {
SyntaxKind::IDENT => 3,
@ -114,7 +110,7 @@ pub(crate) fn view_memory_layout(
ty: &Type,
layout: &Layout,
parent_idx: usize,
edition: Edition,
display_target: DisplayTarget,
) {
let mut fields = ty
.fields(db)
@ -145,7 +141,7 @@ pub(crate) fn view_memory_layout(
if let Ok(child_layout) = child_ty.layout(db) {
nodes.push(MemoryLayoutNode {
item_name: field.name(db),
typename: child_ty.display(db, edition).to_string(),
typename: child_ty.display(db, display_target).to_string(),
size: child_layout.size(),
alignment: child_layout.align(),
offset: match *field {
@ -161,7 +157,7 @@ pub(crate) fn view_memory_layout(
item_name: field.name(db)
+ format!("(no layout data: {:?})", child_ty.layout(db).unwrap_err())
.as_ref(),
typename: child_ty.display(db, edition).to_string(),
typename: child_ty.display(db, display_target).to_string(),
size: 0,
offset: 0,
alignment: 0,
@ -174,7 +170,7 @@ pub(crate) fn view_memory_layout(
for (i, (_, child_ty)) in fields.iter().enumerate() {
if let Ok(child_layout) = child_ty.layout(db) {
read_layout(nodes, db, child_ty, &child_layout, children_start + i, edition);
read_layout(nodes, db, child_ty, &child_layout, children_start + i, display_target);
}
}
}
@ -192,7 +188,7 @@ pub(crate) fn view_memory_layout(
def => def.name(db).map(|n| n.as_str().to_owned()).unwrap_or("[ROOT]".to_owned()),
};
let typename = ty.display(db, edition).to_string();
let typename = ty.display(db, display_target).to_string();
let mut nodes = vec![MemoryLayoutNode {
item_name,
@ -204,7 +200,7 @@ pub(crate) fn view_memory_layout(
children_start: -1,
children_len: 0,
}];
read_layout(&mut nodes, db, &ty, &layout, 0, edition);
read_layout(&mut nodes, db, &ty, &layout, 0, display_target);
RecursiveMemoryLayout { nodes }
})