Skip to content

Show type info on hover of enum variant fields #13745

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions crates/hir-ty/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
use std::sync::Arc;

use chalk_ir::{AdtId, TyKind};
pub(self) use hir_def::layout::*;
use hir_def::LocalFieldId;
use hir_def::{
layout::{
Abi, FieldsShape, Integer, Layout, LayoutCalculator, LayoutError, Primitive, ReprOptions,
RustcEnumVariantIdx, Scalar, Size, StructKind, TargetDataLayout, Variants, WrappingRange,
},
LocalFieldId,
};
use stdx::never;

use crate::{db::HirDatabase, Interner, Substitution, Ty};
Expand Down
11 changes: 6 additions & 5 deletions crates/hir-ty/src/layout/adt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use hir_def::{
AdtId, EnumVariantId, LocalEnumVariantId, VariantId,
};
use la_arena::RawIdx;
use rustc_index::vec::IndexVec;
use smallvec::SmallVec;

use crate::{db::HirDatabase, lang_items::is_unsafe_cell, layout::field_ty, Substitution};

Expand All @@ -34,13 +34,13 @@ pub fn layout_of_adt_query(
let (variants, is_enum, is_union, repr) = match def {
AdtId::StructId(s) => {
let data = db.struct_data(s);
let mut r = IndexVec::new();
let mut r = SmallVec::<[_; 1]>::new();
r.push(handle_variant(s.into(), &data.variant_data)?);
(r, false, false, data.repr.unwrap_or_default())
}
AdtId::UnionId(id) => {
let data = db.union_data(id);
let mut r = IndexVec::new();
let mut r = SmallVec::new();
r.push(handle_variant(id.into(), &data.variant_data)?);
(r, false, true, data.repr.unwrap_or_default())
}
Expand All @@ -55,11 +55,12 @@ pub fn layout_of_adt_query(
&v.variant_data,
)
})
.collect::<Result<IndexVec<RustcEnumVariantIdx, _>, _>>()?;
.collect::<Result<SmallVec<_>, _>>()?;
(r, true, false, data.repr.unwrap_or_default())
}
};
let variants = variants.iter().map(|x| x.iter().collect::<Vec<_>>()).collect::<Vec<_>>();
let variants =
variants.iter().map(|x| x.iter().collect::<Vec<_>>()).collect::<SmallVec<[_; 1]>>();
let variants = variants.iter().map(|x| x.iter().collect()).collect();
if is_union {
cx.layout_of_union(&repr, &variants).ok_or(LayoutError::Unknown)
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/layout/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use hir_def::layout::TargetDataLayout;

use crate::db::HirDatabase;

use super::{AbiAndPrefAlign, AddressSpace, Align, Endian, Integer, Size};
use hir_def::layout::{AbiAndPrefAlign, AddressSpace, Align, Endian, Integer, Size};

pub fn current_target_data_layout_query(db: &dyn HirDatabase) -> Arc<TargetDataLayout> {
let crate_graph = db.crate_graph();
Expand Down
14 changes: 7 additions & 7 deletions crates/ide/src/hover/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,17 +395,17 @@ pub(super) fn definition(
let id = it.index();
let layout = it.layout(db).ok()?;
let offset = match var_def {
hir::VariantDef::Struct(s) => {
let layout = Adt::from(s).layout(db).ok()?;
layout.fields.offset(id)
}
_ => return None,
hir::VariantDef::Struct(s) => Adt::from(s)
.layout(db)
.ok()
.map(|layout| format!(", offset = {}", layout.fields.offset(id).bytes())),
_ => None,
};
Some(format!(
"size = {}, align = {}, offset = {}",
"size = {}, align = {}{}",
layout.size.bytes(),
layout.align.abi.bytes(),
offset.bytes()
offset.as_deref().unwrap_or_default()
))
}),
Definition::Module(it) => label_and_docs(db, it),
Expand Down
22 changes: 22 additions & 0 deletions crates/ide/src/hover/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5176,6 +5176,28 @@ enum Enum {
);
}

#[test]
fn hover_record_variant_field() {
check(
r#"
enum Enum {
RecordV { field$0: u32 }
}
"#,
expect![[r#"
*field*

```rust
test::RecordV
```

```rust
field: u32 // size = 4, align = 4
```
"#]],
);
}

#[test]
fn hover_trait_impl_assoc_item_def_doc_forwarding() {
check(
Expand Down