Skip to content

Commit dfb1529

Browse files
committed
fix: Type param hover shows correct sized bounds.
1 parent 9ce3c07 commit dfb1529

File tree

2 files changed

+165
-10
lines changed

2 files changed

+165
-10
lines changed

crates/hir/src/display.rs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@ use hir_def::{
55
type_ref::{TypeBound, TypeRef},
66
AdtId, GenericDefId,
77
};
8-
use hir_ty::display::{
9-
write_bounds_like_dyn_trait_with_prefix, write_visibility, HirDisplay, HirDisplayError,
10-
HirFormatter, SizedByDefault,
8+
use hir_ty::{
9+
display::{
10+
write_bounds_like_dyn_trait_with_prefix, write_visibility, HirDisplay, HirDisplayError,
11+
HirFormatter, SizedByDefault,
12+
},
13+
Interner, TraitRefExt, WhereClause,
1114
};
12-
use hir_ty::Interner;
1315
use syntax::ast::{self, NameOwner};
1416

1517
use crate::{
16-
Adt, Const, ConstParam, Enum, Field, Function, GenericParam, HasVisibility, LifetimeParam,
17-
Module, Static, Struct, Trait, TyBuilder, Type, TypeAlias, TypeParam, Union, Variant,
18+
Adt, Const, ConstParam, Enum, Field, Function, GenericParam, HasCrate, HasVisibility,
19+
LifetimeParam, Module, Static, Struct, Trait, TyBuilder, Type, TypeAlias, TypeParam, Union,
20+
Variant,
1821
};
1922

2023
impl HirDisplay for Function {
@@ -234,12 +237,24 @@ impl HirDisplay for GenericParam {
234237
impl HirDisplay for TypeParam {
235238
fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
236239
write!(f, "{}", self.name(f.db))?;
240+
if f.omit_verbose_types() {
241+
return Ok(());
242+
}
243+
237244
let bounds = f.db.generic_predicates_for_param(self.id);
238245
let substs = TyBuilder::type_params_subst(f.db, self.id.parent);
239-
let predicates =
240-
bounds.iter().cloned().map(|b| b.substitute(&Interner, &substs)).collect::<Vec<_>>();
241-
if !(predicates.is_empty() || f.omit_verbose_types()) {
242-
let default_sized = SizedByDefault::Sized { anchor: self.module(f.db).krate().id };
246+
let predicates: Vec<_> =
247+
bounds.iter().cloned().map(|b| b.substitute(&Interner, &substs)).collect();
248+
let krate = self.id.parent.krate(f.db).id;
249+
let sized_trait =
250+
f.db.lang_item(krate, "sized".into()).and_then(|lang_item| lang_item.as_trait());
251+
let has_only_sized_bound = predicates.iter().all(move |pred| match pred.skip_binders() {
252+
WhereClause::Implemented(it) => Some(it.hir_trait_id()) == sized_trait,
253+
_ => false,
254+
});
255+
let has_only_not_sized_bound = predicates.is_empty();
256+
if !has_only_sized_bound || has_only_not_sized_bound {
257+
let default_sized = SizedByDefault::Sized { anchor: krate };
243258
write_bounds_like_dyn_trait_with_prefix(":", &predicates, default_sized, f)?;
244259
}
245260
Ok(())

crates/ide/src/hover.rs

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3596,6 +3596,146 @@ impl<T: Trait + ?Sized> Foo<T$0> {}
35963596
);
35973597
}
35983598

3599+
mod type_param_sized_bounds {
3600+
use super::*;
3601+
3602+
#[test]
3603+
fn single_implicit() {
3604+
check(
3605+
r#"
3606+
//- minicore: sized
3607+
fn foo<T$0>() {}
3608+
"#,
3609+
expect![[r#"
3610+
*T*
3611+
3612+
```rust
3613+
T
3614+
```
3615+
"#]],
3616+
);
3617+
}
3618+
3619+
#[test]
3620+
fn single_explicit() {
3621+
check(
3622+
r#"
3623+
//- minicore: sized
3624+
fn foo<T$0: Sized>() {}
3625+
"#,
3626+
expect![[r#"
3627+
*T*
3628+
3629+
```rust
3630+
T
3631+
```
3632+
"#]],
3633+
);
3634+
}
3635+
3636+
#[test]
3637+
fn single_relaxed() {
3638+
check(
3639+
r#"
3640+
//- minicore: sized
3641+
fn foo<T$0: ?Sized>() {}
3642+
"#,
3643+
expect![[r#"
3644+
*T*
3645+
3646+
```rust
3647+
T: ?Sized
3648+
```
3649+
"#]],
3650+
);
3651+
}
3652+
3653+
#[test]
3654+
fn multiple_implicit() {
3655+
check(
3656+
r#"
3657+
//- minicore: sized
3658+
trait Trait {}
3659+
fn foo<T$0: Trait>() {}
3660+
"#,
3661+
expect![[r#"
3662+
*T*
3663+
3664+
```rust
3665+
T: Trait
3666+
```
3667+
"#]],
3668+
);
3669+
}
3670+
3671+
#[test]
3672+
fn multiple_explicit() {
3673+
check(
3674+
r#"
3675+
//- minicore: sized
3676+
trait Trait {}
3677+
fn foo<T$0: Trait + Sized>() {}
3678+
"#,
3679+
expect![[r#"
3680+
*T*
3681+
3682+
```rust
3683+
T: Trait
3684+
```
3685+
"#]],
3686+
);
3687+
}
3688+
3689+
#[test]
3690+
fn multiple_relaxed() {
3691+
check(
3692+
r#"
3693+
//- minicore: sized
3694+
trait Trait {}
3695+
fn foo<T$0: Trait + ?Sized>() {}
3696+
"#,
3697+
expect![[r#"
3698+
*T*
3699+
3700+
```rust
3701+
T: Trait + ?Sized
3702+
```
3703+
"#]],
3704+
);
3705+
}
3706+
3707+
#[test]
3708+
fn mixed() {
3709+
check(
3710+
r#"
3711+
//- minicore: sized
3712+
fn foo<T$0: ?Sized + Sized + Sized>() {}
3713+
"#,
3714+
expect![[r#"
3715+
*T*
3716+
3717+
```rust
3718+
T
3719+
```
3720+
"#]],
3721+
);
3722+
check(
3723+
r#"
3724+
//- minicore: sized
3725+
trait Trait {}
3726+
fn foo<T$0: Sized + ?Sized + Sized + Trait>() {}
3727+
"#,
3728+
expect![[r#"
3729+
*T*
3730+
3731+
```rust
3732+
T: Trait
3733+
```
3734+
"#]],
3735+
);
3736+
}
3737+
}
3738+
35993739
#[test]
36003740
fn hover_const_param() {
36013741
check(

0 commit comments

Comments
 (0)