Skip to content

Commit a0296c2

Browse files
committed
Auto merge of #13745 - Veykril:ty-hint-variant-field, r=Veykril
Show type info on hover of enum variant fields Small addition to #13490
2 parents 34e654c + e80674e commit a0296c2

File tree

5 files changed

+43
-15
lines changed

5 files changed

+43
-15
lines changed

crates/hir-ty/src/layout.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
use std::sync::Arc;
44

55
use chalk_ir::{AdtId, TyKind};
6-
pub(self) use hir_def::layout::*;
7-
use hir_def::LocalFieldId;
6+
use hir_def::{
7+
layout::{
8+
Abi, FieldsShape, Integer, Layout, LayoutCalculator, LayoutError, Primitive, ReprOptions,
9+
RustcEnumVariantIdx, Scalar, Size, StructKind, TargetDataLayout, Variants, WrappingRange,
10+
},
11+
LocalFieldId,
12+
};
813
use stdx::never;
914

1015
use crate::{db::HirDatabase, Interner, Substitution, Ty};

crates/hir-ty/src/layout/adt.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use hir_def::{
88
AdtId, EnumVariantId, LocalEnumVariantId, VariantId,
99
};
1010
use la_arena::RawIdx;
11-
use rustc_index::vec::IndexVec;
11+
use smallvec::SmallVec;
1212

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

@@ -34,13 +34,13 @@ pub fn layout_of_adt_query(
3434
let (variants, is_enum, is_union, repr) = match def {
3535
AdtId::StructId(s) => {
3636
let data = db.struct_data(s);
37-
let mut r = IndexVec::new();
37+
let mut r = SmallVec::<[_; 1]>::new();
3838
r.push(handle_variant(s.into(), &data.variant_data)?);
3939
(r, false, false, data.repr.unwrap_or_default())
4040
}
4141
AdtId::UnionId(id) => {
4242
let data = db.union_data(id);
43-
let mut r = IndexVec::new();
43+
let mut r = SmallVec::new();
4444
r.push(handle_variant(id.into(), &data.variant_data)?);
4545
(r, false, true, data.repr.unwrap_or_default())
4646
}
@@ -55,11 +55,12 @@ pub fn layout_of_adt_query(
5555
&v.variant_data,
5656
)
5757
})
58-
.collect::<Result<IndexVec<RustcEnumVariantIdx, _>, _>>()?;
58+
.collect::<Result<SmallVec<_>, _>>()?;
5959
(r, true, false, data.repr.unwrap_or_default())
6060
}
6161
};
62-
let variants = variants.iter().map(|x| x.iter().collect::<Vec<_>>()).collect::<Vec<_>>();
62+
let variants =
63+
variants.iter().map(|x| x.iter().collect::<Vec<_>>()).collect::<SmallVec<[_; 1]>>();
6364
let variants = variants.iter().map(|x| x.iter().collect()).collect();
6465
if is_union {
6566
cx.layout_of_union(&repr, &variants).ok_or(LayoutError::Unknown)

crates/hir-ty/src/layout/target.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use hir_def::layout::TargetDataLayout;
66

77
use crate::db::HirDatabase;
88

9-
use super::{AbiAndPrefAlign, AddressSpace, Align, Endian, Integer, Size};
9+
use hir_def::layout::{AbiAndPrefAlign, AddressSpace, Align, Endian, Integer, Size};
1010

1111
pub fn current_target_data_layout_query(db: &dyn HirDatabase) -> Arc<TargetDataLayout> {
1212
let crate_graph = db.crate_graph();

crates/ide/src/hover/render.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -395,17 +395,17 @@ pub(super) fn definition(
395395
let id = it.index();
396396
let layout = it.layout(db).ok()?;
397397
let offset = match var_def {
398-
hir::VariantDef::Struct(s) => {
399-
let layout = Adt::from(s).layout(db).ok()?;
400-
layout.fields.offset(id)
401-
}
402-
_ => return None,
398+
hir::VariantDef::Struct(s) => Adt::from(s)
399+
.layout(db)
400+
.ok()
401+
.map(|layout| format!(", offset = {}", layout.fields.offset(id).bytes())),
402+
_ => None,
403403
};
404404
Some(format!(
405-
"size = {}, align = {}, offset = {}",
405+
"size = {}, align = {}{}",
406406
layout.size.bytes(),
407407
layout.align.abi.bytes(),
408-
offset.bytes()
408+
offset.as_deref().unwrap_or_default()
409409
))
410410
}),
411411
Definition::Module(it) => label_and_docs(db, it),

crates/ide/src/hover/tests.rs

+22
Original file line numberDiff line numberDiff line change
@@ -5176,6 +5176,28 @@ enum Enum {
51765176
);
51775177
}
51785178

5179+
#[test]
5180+
fn hover_record_variant_field() {
5181+
check(
5182+
r#"
5183+
enum Enum {
5184+
RecordV { field$0: u32 }
5185+
}
5186+
"#,
5187+
expect![[r#"
5188+
*field*
5189+
5190+
```rust
5191+
test::RecordV
5192+
```
5193+
5194+
```rust
5195+
field: u32 // size = 4, align = 4
5196+
```
5197+
"#]],
5198+
);
5199+
}
5200+
51795201
#[test]
51805202
fn hover_trait_impl_assoc_item_def_doc_forwarding() {
51815203
check(

0 commit comments

Comments
 (0)