Skip to content

Commit 8696703

Browse files
committed
Consider ADT generic parameter defaults for unsubstituted layout calculations
1 parent c3b8c2a commit 8696703

File tree

4 files changed

+61
-10
lines changed

4 files changed

+61
-10
lines changed

crates/hir-ty/src/builder.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ impl<D> TyBuilder<D> {
7474
(self.data, subst)
7575
}
7676

77+
pub fn build_into_subst(self) -> Substitution {
78+
self.build_internal().1
79+
}
80+
7781
pub fn push(mut self, arg: impl CastTo<GenericArg>) -> Self {
7882
assert!(self.remaining() > 0);
7983
let arg = arg.cast(Interner);
@@ -291,7 +295,6 @@ impl TyBuilder<hir_def::AdtId> {
291295
) -> Self {
292296
// Note that we're building ADT, so we never have parent generic parameters.
293297
let defaults = db.generic_defaults(self.data.into());
294-
let dummy_ty = TyKind::Error.intern(Interner).cast(Interner);
295298
for default_ty in defaults.iter().skip(self.vec.len()) {
296299
// NOTE(skip_binders): we only check if the arg type is error type.
297300
if let Some(x) = default_ty.skip_binders().ty(Interner) {
@@ -301,13 +304,16 @@ impl TyBuilder<hir_def::AdtId> {
301304
}
302305
}
303306
// Each default can only depend on the previous parameters.
304-
// FIXME: we don't handle const generics here.
305307
let subst_so_far = Substitution::from_iter(
306308
Interner,
307309
self.vec
308310
.iter()
309311
.cloned()
310-
.chain(iter::repeat(dummy_ty.clone()))
312+
.chain(self.param_kinds[self.vec.len()..].iter().map(|it| match it {
313+
ParamKind::Type => TyKind::Error.intern(Interner).cast(Interner),
314+
ParamKind::Lifetime => error_lifetime().cast(Interner),
315+
ParamKind::Const(ty) => unknown_const_as_generic(ty.clone()),
316+
}))
311317
.take(self.param_kinds.len()),
312318
);
313319
self.vec.push(default_ty.clone().substitute(Interner, &subst_so_far).cast(Interner));

crates/hir/src/lib.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,16 +1418,14 @@ impl Adt {
14181418
}
14191419

14201420
pub fn layout(self, db: &dyn HirDatabase) -> Result<Layout, LayoutError> {
1421-
if !db.generic_params(self.into()).is_empty() {
1422-
return Err(LayoutError::HasPlaceholder);
1423-
}
1424-
let krate = self.krate(db).id;
14251421
db.layout_of_adt(
14261422
self.into(),
1427-
Substitution::empty(Interner),
1423+
TyBuilder::adt(db, self.into())
1424+
.fill_with_defaults(db, || TyKind::Error.intern(Interner))
1425+
.build_into_subst(),
14281426
db.trait_environment(self.into()),
14291427
)
1430-
.map(|layout| Layout(layout, db.target_data_layout(krate).unwrap()))
1428+
.map(|layout| Layout(layout, db.target_data_layout(self.krate(db).id).unwrap()))
14311429
}
14321430

14331431
/// Turns this ADT into a type. Any type parameters of the ADT will be

crates/ide/src/hover/tests.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2322,6 +2322,49 @@ fn test_hover_layout_of_variant() {
23222322
);
23232323
}
23242324

2325+
#[test]
2326+
fn test_hover_layout_of_variant_generic() {
2327+
check(
2328+
r#"enum Option<T> {
2329+
Some(T),
2330+
None$0
2331+
}"#,
2332+
expect![[r#"
2333+
*None*
2334+
2335+
```rust
2336+
test::Option
2337+
```
2338+
2339+
```rust
2340+
None
2341+
```
2342+
"#]],
2343+
);
2344+
}
2345+
2346+
#[test]
2347+
fn test_hover_layout_generic_unused() {
2348+
check(
2349+
r#"
2350+
//- minicore: phantom_data
2351+
struct S$0<T>(core::marker::PhantomData<T>);
2352+
"#,
2353+
expect![[r#"
2354+
*S*
2355+
2356+
```rust
2357+
test
2358+
```
2359+
2360+
```rust
2361+
// size = 0, align = 1
2362+
struct S<T>(PhantomData<T>)
2363+
```
2364+
"#]],
2365+
);
2366+
}
2367+
23252368
#[test]
23262369
fn test_hover_layout_of_enum() {
23272370
check(
@@ -3673,6 +3716,7 @@ struct S$0T<const C: usize = 1, T = Foo>(T);
36733716
```
36743717
36753718
```rust
3719+
// size = 0, align = 1
36763720
struct ST<const C: usize = 1, T = Foo>(T)
36773721
```
36783722
"#]],
@@ -3694,6 +3738,7 @@ struct S$0T<const C: usize = {40 + 2}, T = Foo>(T);
36943738
```
36953739
36963740
```rust
3741+
// size = 0, align = 1
36973742
struct ST<const C: usize = {const}, T = Foo>(T)
36983743
```
36993744
"#]],
@@ -3716,6 +3761,7 @@ struct S$0T<const C: usize = VAL, T = Foo>(T);
37163761
```
37173762
37183763
```rust
3764+
// size = 0, align = 1
37193765
struct ST<const C: usize = VAL, T = Foo>(T)
37203766
```
37213767
"#]],
@@ -7872,6 +7918,7 @@ struct Pedro$0<'a> {
78727918
```
78737919
78747920
```rust
7921+
// size = 16 (0x10), align = 8, niches = 1
78757922
struct Pedro<'a>
78767923
```
78777924
"#]],

crates/proc-macro-api/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub enum ProcMacroKind {
3737
CustomDerive,
3838
Attr,
3939
// This used to be called FuncLike, so that's what the server expects currently.
40-
#[serde(alias = "bang")]
40+
#[serde(alias = "Bang")]
4141
#[serde(rename(serialize = "FuncLike", deserialize = "FuncLike"))]
4242
Bang,
4343
}

0 commit comments

Comments
 (0)