Skip to content

Commit 5f11c9a

Browse files
committed
Auto merge of #15081 - adenine-dev:master, r=HKalbasi
Feature: Add a memory layout viewer **Motivation**: rustc by default doesn't enforce a particular memory layout, however it can be useful to see what it is doing under the hood, or if using a particular repr ensure it is behaving how you want it to. This command provides a way to visually explore memory layouts of structures. **Example**: this structure: ```rust struct X { x: i32, y: u8, z: Vec<bool>, w: usize, } ``` produces this output: <img width="692" alt="image" src="https://github.com/rust-lang/rust-analyzer/assets/22418744/e0312233-18a7-4bb9-ae5b-7b52fcff158a"> **Work yet to be done**: - tests (see below) - html is mildly janky (see below) - enums and unions are viewed flatly, how should they be represented? - should niches be marked somehow? This was written for my own use, and the jank is fine for me, but in its current state it is probably not ready to merge mostly because it is missing tests, and also because the code quality is not great. However, before I spend time fixing those things idk if this is even something wanted, if it is I am happy to clean it up, if not that's cool too.
2 parents dcda13a + d0df00d commit 5f11c9a

File tree

12 files changed

+848
-15
lines changed

12 files changed

+848
-15
lines changed

crates/ide-db/src/helpers.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ use syntax::{
99
AstToken, SyntaxKind, SyntaxToken, TokenAtOffset,
1010
};
1111

12-
use crate::{defs::Definition, generated, RootDatabase};
12+
use crate::{
13+
defs::{Definition, IdentClass},
14+
generated, RootDatabase,
15+
};
1316

1417
pub fn item_name(db: &RootDatabase, item: ItemInNs) -> Option<Name> {
1518
match item {
@@ -109,3 +112,16 @@ pub fn is_editable_crate(krate: Crate, db: &RootDatabase) -> bool {
109112
let source_root_id = db.file_source_root(root_file);
110113
!db.source_root(source_root_id).is_library
111114
}
115+
116+
pub fn get_definition(
117+
sema: &Semantics<'_, RootDatabase>,
118+
token: SyntaxToken,
119+
) -> Option<Definition> {
120+
for token in sema.descend_into_macros(token) {
121+
let def = IdentClass::classify_token(sema, &token).map(IdentClass::definitions_no_ops);
122+
if let Some(&[x]) = def.as_deref() {
123+
return Some(x);
124+
}
125+
}
126+
None
127+
}

crates/ide/src/lib.rs

+9
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ mod interpret_function;
6060
mod view_item_tree;
6161
mod shuffle_crate_graph;
6262
mod fetch_crates;
63+
mod view_memory_layout;
6364

6465
use std::ffi::OsStr;
6566

@@ -74,6 +75,7 @@ use ide_db::{
7475
};
7576
use syntax::SourceFile;
7677
use triomphe::Arc;
78+
use view_memory_layout::{view_memory_layout, RecursiveMemoryLayout};
7779

7880
use crate::navigation_target::{ToNav, TryToNav};
7981

@@ -724,6 +726,13 @@ impl Analysis {
724726
self.with_db(|db| move_item::move_item(db, range, direction))
725727
}
726728

729+
pub fn get_recursive_memory_layout(
730+
&self,
731+
position: FilePosition,
732+
) -> Cancellable<Option<RecursiveMemoryLayout>> {
733+
self.with_db(|db| view_memory_layout(db, position))
734+
}
735+
727736
/// Performs an operation on the database that may be canceled.
728737
///
729738
/// rust-analyzer needs to be able to answer semantic questions about the

crates/ide/src/static_index.rs

+4-13
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
44
use std::collections::HashMap;
55

6-
use hir::{db::HirDatabase, Crate, Module, Semantics};
6+
use hir::{db::HirDatabase, Crate, Module};
7+
use ide_db::helpers::get_definition;
78
use ide_db::{
89
base_db::{FileId, FileRange, SourceDatabaseExt},
9-
defs::{Definition, IdentClass},
10+
defs::Definition,
1011
FxHashSet, RootDatabase,
1112
};
12-
use syntax::{AstNode, SyntaxKind::*, SyntaxToken, TextRange, T};
13+
use syntax::{AstNode, SyntaxKind::*, TextRange, T};
1314

1415
use crate::{
1516
hover::hover_for_definition,
@@ -214,16 +215,6 @@ impl StaticIndex<'_> {
214215
}
215216
}
216217

217-
fn get_definition(sema: &Semantics<'_, RootDatabase>, token: SyntaxToken) -> Option<Definition> {
218-
for token in sema.descend_into_macros(token) {
219-
let def = IdentClass::classify_token(sema, &token).map(IdentClass::definitions_no_ops);
220-
if let Some(&[it]) = def.as_deref() {
221-
return Some(it);
222-
}
223-
}
224-
None
225-
}
226-
227218
#[cfg(test)]
228219
mod tests {
229220
use crate::{fixture, StaticIndex};

0 commit comments

Comments
 (0)