From 9ce3c075ad70b509dbc540bb96016a36011c5451 Mon Sep 17 00:00:00 2001 From: Dawer <7803845+iDawer@users.noreply.github.com> Date: Tue, 7 Sep 2021 12:36:15 +0500 Subject: [PATCH 1/3] internal: add implicit `: Sized` bound to type parameters. --- crates/hir_ty/src/lower.rs | 12 ++++++++++-- crates/ide/src/hover.rs | 33 ++++++++++++++++++++++++--------- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index 86678b802f8..c4f9f670582 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs @@ -1024,7 +1024,7 @@ pub(crate) fn generic_predicates_for_param_query( let ctx = TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); let generics = generics(db.upcast(), param_id.parent); - resolver + let mut predicates: Vec<_> = resolver .where_predicates_in_scope() // we have to filter out all other predicates *first*, before attempting to lower them .filter(|pred| match pred { @@ -1038,7 +1038,15 @@ pub(crate) fn generic_predicates_for_param_query( WherePredicate::Lifetime { .. } => false, }) .flat_map(|pred| ctx.lower_where_predicate(pred, true).map(|p| make_binders(&generics, p))) - .collect() + .collect(); + + let subst = generics.bound_vars_subst(DebruijnIndex::INNERMOST); + let explicitly_unsized_tys = ctx.unsized_types.into_inner(); + let implicitly_sized_predicates = + implicitly_sized_clauses(db, param_id.parent, &explicitly_unsized_tys, &subst, &resolver) + .map(|p| make_binders(&generics, crate::wrap_empty_binders(p))); + predicates.extend(implicitly_sized_predicates); + predicates.into() } pub(crate) fn generic_predicates_for_param_recover( diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 2a505c621f2..465ad906581 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs @@ -3520,15 +3520,15 @@ fn hover_type_param() { r#" //- minicore: sized struct Foo(T); -trait Copy {} -trait Clone {} -impl Foo where T: Sized {} +trait TraitA {} +trait TraitB {} +impl Foo where T: Sized {} "#, expect![[r#" *T* ```rust - T: Copy + Clone + T: TraitA + TraitB ``` "#]], ); @@ -3562,20 +3562,35 @@ impl Foo {} } #[test] - fn hover_type_param_not_sized() { + fn hover_type_param_sized_bounds() { + // implicit `: Sized` bound check( r#" //- minicore: sized +trait Trait {} struct Foo(T); -trait Copy {} -trait Clone {} -impl Foo where T: ?Sized {} +impl Foo {} "#, expect![[r#" *T* ```rust - T: Copy + Clone + ?Sized + T: Trait + ``` + "#]], + ); + check( + r#" +//- minicore: sized +trait Trait {} +struct Foo(T); +impl Foo {} +"#, + expect![[r#" + *T* + + ```rust + T: Trait + ?Sized ``` "#]], ); From dfb15292c2c84a22fd3bc9d1574d370bfe7c5976 Mon Sep 17 00:00:00 2001 From: Dawer <7803845+iDawer@users.noreply.github.com> Date: Fri, 10 Sep 2021 20:48:39 +0500 Subject: [PATCH 2/3] fix: Type param hover shows correct sized bounds. --- crates/hir/src/display.rs | 35 +++++++--- crates/ide/src/hover.rs | 140 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 165 insertions(+), 10 deletions(-) diff --git a/crates/hir/src/display.rs b/crates/hir/src/display.rs index dff4372738b..4ef7d399de3 100644 --- a/crates/hir/src/display.rs +++ b/crates/hir/src/display.rs @@ -5,16 +5,19 @@ type_ref::{TypeBound, TypeRef}, AdtId, GenericDefId, }; -use hir_ty::display::{ - write_bounds_like_dyn_trait_with_prefix, write_visibility, HirDisplay, HirDisplayError, - HirFormatter, SizedByDefault, +use hir_ty::{ + display::{ + write_bounds_like_dyn_trait_with_prefix, write_visibility, HirDisplay, HirDisplayError, + HirFormatter, SizedByDefault, + }, + Interner, TraitRefExt, WhereClause, }; -use hir_ty::Interner; use syntax::ast::{self, NameOwner}; use crate::{ - Adt, Const, ConstParam, Enum, Field, Function, GenericParam, HasVisibility, LifetimeParam, - Module, Static, Struct, Trait, TyBuilder, Type, TypeAlias, TypeParam, Union, Variant, + Adt, Const, ConstParam, Enum, Field, Function, GenericParam, HasCrate, HasVisibility, + LifetimeParam, Module, Static, Struct, Trait, TyBuilder, Type, TypeAlias, TypeParam, Union, + Variant, }; impl HirDisplay for Function { @@ -234,12 +237,24 @@ fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { impl HirDisplay for TypeParam { fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { write!(f, "{}", self.name(f.db))?; + if f.omit_verbose_types() { + return Ok(()); + } + let bounds = f.db.generic_predicates_for_param(self.id); let substs = TyBuilder::type_params_subst(f.db, self.id.parent); - let predicates = - bounds.iter().cloned().map(|b| b.substitute(&Interner, &substs)).collect::>(); - if !(predicates.is_empty() || f.omit_verbose_types()) { - let default_sized = SizedByDefault::Sized { anchor: self.module(f.db).krate().id }; + let predicates: Vec<_> = + bounds.iter().cloned().map(|b| b.substitute(&Interner, &substs)).collect(); + let krate = self.id.parent.krate(f.db).id; + let sized_trait = + f.db.lang_item(krate, "sized".into()).and_then(|lang_item| lang_item.as_trait()); + let has_only_sized_bound = predicates.iter().all(move |pred| match pred.skip_binders() { + WhereClause::Implemented(it) => Some(it.hir_trait_id()) == sized_trait, + _ => false, + }); + let has_only_not_sized_bound = predicates.is_empty(); + if !has_only_sized_bound || has_only_not_sized_bound { + let default_sized = SizedByDefault::Sized { anchor: krate }; write_bounds_like_dyn_trait_with_prefix(":", &predicates, default_sized, f)?; } Ok(()) diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 465ad906581..30c695700ce 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs @@ -3596,6 +3596,146 @@ impl Foo {} ); } + mod type_param_sized_bounds { + use super::*; + + #[test] + fn single_implicit() { + check( + r#" +//- minicore: sized +fn foo() {} +"#, + expect![[r#" + *T* + + ```rust + T + ``` + "#]], + ); + } + + #[test] + fn single_explicit() { + check( + r#" +//- minicore: sized +fn foo() {} +"#, + expect![[r#" + *T* + + ```rust + T + ``` + "#]], + ); + } + + #[test] + fn single_relaxed() { + check( + r#" +//- minicore: sized +fn foo() {} +"#, + expect![[r#" + *T* + + ```rust + T: ?Sized + ``` + "#]], + ); + } + + #[test] + fn multiple_implicit() { + check( + r#" +//- minicore: sized +trait Trait {} +fn foo() {} +"#, + expect![[r#" + *T* + + ```rust + T: Trait + ``` + "#]], + ); + } + + #[test] + fn multiple_explicit() { + check( + r#" +//- minicore: sized +trait Trait {} +fn foo() {} +"#, + expect![[r#" + *T* + + ```rust + T: Trait + ``` + "#]], + ); + } + + #[test] + fn multiple_relaxed() { + check( + r#" +//- minicore: sized +trait Trait {} +fn foo() {} +"#, + expect![[r#" + *T* + + ```rust + T: Trait + ?Sized + ``` + "#]], + ); + } + + #[test] + fn mixed() { + check( + r#" +//- minicore: sized +fn foo() {} +"#, + expect![[r#" + *T* + + ```rust + T + ``` + "#]], + ); + check( + r#" +//- minicore: sized +trait Trait {} +fn foo() {} +"#, + expect![[r#" + *T* + + ```rust + T: Trait + ``` + "#]], + ); + } + } + #[test] fn hover_const_param() { check( From 964c7b3211f0e317cffe9047761f46269087ddbd Mon Sep 17 00:00:00 2001 From: Dawer <7803845+iDawer@users.noreply.github.com> Date: Fri, 10 Sep 2021 23:02:37 +0500 Subject: [PATCH 3/3] minor: fix tests. --- crates/ide/src/hover.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 30c695700ce..1589a02b982 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs @@ -3534,6 +3534,7 @@ impl Foo where T: Sized {} ); check( r#" +//- minicore: sized struct Foo(T); impl Foo {} "#, @@ -3548,6 +3549,7 @@ impl Foo {} // lifetimes bounds arent being tracked yet check( r#" +//- minicore: sized struct Foo(T); impl Foo {} "#,