mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-26 03:45:04 +00:00
Show variance of parameters on hover
This commit is contained in:
parent
60e28c6bd9
commit
bb921fbe94
5 changed files with 69 additions and 12 deletions
|
@ -26,14 +26,14 @@ use triomphe::Arc;
|
||||||
|
|
||||||
use crate::{db::HirDatabase, lt_to_placeholder_idx, to_placeholder_idx, Interner, Substitution};
|
use crate::{db::HirDatabase, lt_to_placeholder_idx, to_placeholder_idx, Interner, Substitution};
|
||||||
|
|
||||||
pub(crate) fn generics(db: &dyn DefDatabase, def: GenericDefId) -> Generics {
|
pub fn generics(db: &dyn DefDatabase, def: GenericDefId) -> Generics {
|
||||||
let parent_generics = parent_generic_def(db, def).map(|def| Box::new(generics(db, def)));
|
let parent_generics = parent_generic_def(db, def).map(|def| Box::new(generics(db, def)));
|
||||||
let params = db.generic_params(def);
|
let params = db.generic_params(def);
|
||||||
let has_trait_self_param = params.trait_self_param().is_some();
|
let has_trait_self_param = params.trait_self_param().is_some();
|
||||||
Generics { def, params, parent_generics, has_trait_self_param }
|
Generics { def, params, parent_generics, has_trait_self_param }
|
||||||
}
|
}
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub(crate) struct Generics {
|
pub struct Generics {
|
||||||
def: GenericDefId,
|
def: GenericDefId,
|
||||||
params: Arc<GenericParams>,
|
params: Arc<GenericParams>,
|
||||||
parent_generics: Option<Box<Generics>>,
|
parent_generics: Option<Box<Generics>>,
|
||||||
|
@ -153,7 +153,7 @@ impl Generics {
|
||||||
(parent_len, self_param, type_params, const_params, impl_trait_params, lifetime_params)
|
(parent_len, self_param, type_params, const_params, impl_trait_params, lifetime_params)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn type_or_const_param_idx(&self, param: TypeOrConstParamId) -> Option<usize> {
|
pub fn type_or_const_param_idx(&self, param: TypeOrConstParamId) -> Option<usize> {
|
||||||
self.find_type_or_const_param(param)
|
self.find_type_or_const_param(param)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -174,7 +174,7 @@ impl Generics {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn lifetime_idx(&self, lifetime: LifetimeParamId) -> Option<usize> {
|
pub fn lifetime_idx(&self, lifetime: LifetimeParamId) -> Option<usize> {
|
||||||
self.find_lifetime(lifetime)
|
self.find_lifetime(lifetime)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,7 +24,6 @@ extern crate ra_ap_rustc_pattern_analysis as rustc_pattern_analysis;
|
||||||
mod builder;
|
mod builder;
|
||||||
mod chalk_db;
|
mod chalk_db;
|
||||||
mod chalk_ext;
|
mod chalk_ext;
|
||||||
mod generics;
|
|
||||||
mod infer;
|
mod infer;
|
||||||
mod inhabitedness;
|
mod inhabitedness;
|
||||||
mod interner;
|
mod interner;
|
||||||
|
@ -39,6 +38,7 @@ pub mod db;
|
||||||
pub mod diagnostics;
|
pub mod diagnostics;
|
||||||
pub mod display;
|
pub mod display;
|
||||||
pub mod dyn_compatibility;
|
pub mod dyn_compatibility;
|
||||||
|
pub mod generics;
|
||||||
pub mod lang_items;
|
pub mod lang_items;
|
||||||
pub mod layout;
|
pub mod layout;
|
||||||
pub mod method_resolution;
|
pub mod method_resolution;
|
||||||
|
@ -89,10 +89,9 @@ pub use infer::{
|
||||||
PointerCast,
|
PointerCast,
|
||||||
};
|
};
|
||||||
pub use interner::Interner;
|
pub use interner::Interner;
|
||||||
pub use lower::diagnostics::*;
|
|
||||||
pub use lower::{
|
pub use lower::{
|
||||||
associated_type_shorthand_candidates, ImplTraitLoweringMode, ParamLoweringMode, TyDefId,
|
associated_type_shorthand_candidates, diagnostics::*, ImplTraitLoweringMode, ParamLoweringMode,
|
||||||
TyLoweringContext, ValueTyDefId,
|
TyDefId, TyLoweringContext, ValueTyDefId,
|
||||||
};
|
};
|
||||||
pub use mapping::{
|
pub use mapping::{
|
||||||
from_assoc_type_id, from_chalk_trait_id, from_foreign_def_id, from_placeholder_idx,
|
from_assoc_type_id, from_chalk_trait_id, from_foreign_def_id, from_placeholder_idx,
|
||||||
|
@ -102,6 +101,7 @@ pub use mapping::{
|
||||||
pub use method_resolution::check_orphan_rules;
|
pub use method_resolution::check_orphan_rules;
|
||||||
pub use traits::TraitEnvironment;
|
pub use traits::TraitEnvironment;
|
||||||
pub use utils::{all_super_traits, direct_super_traits, is_fn_unsafe_to_call};
|
pub use utils::{all_super_traits, direct_super_traits, is_fn_unsafe_to_call};
|
||||||
|
pub use variance::Variance;
|
||||||
|
|
||||||
pub use chalk_ir::{
|
pub use chalk_ir::{
|
||||||
cast::Cast,
|
cast::Cast,
|
||||||
|
|
|
@ -101,7 +101,6 @@ pub use crate::{
|
||||||
PathResolution, Semantics, SemanticsImpl, SemanticsScope, TypeInfo, VisibleTraits,
|
PathResolution, Semantics, SemanticsImpl, SemanticsScope, TypeInfo, VisibleTraits,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
pub use hir_ty::method_resolution::TyFingerprint;
|
|
||||||
|
|
||||||
// Be careful with these re-exports.
|
// Be careful with these re-exports.
|
||||||
//
|
//
|
||||||
|
@ -151,8 +150,9 @@ pub use {
|
||||||
display::{ClosureStyle, HirDisplay, HirDisplayError, HirWrite},
|
display::{ClosureStyle, HirDisplay, HirDisplayError, HirWrite},
|
||||||
dyn_compatibility::{DynCompatibilityViolation, MethodViolationCode},
|
dyn_compatibility::{DynCompatibilityViolation, MethodViolationCode},
|
||||||
layout::LayoutError,
|
layout::LayoutError,
|
||||||
|
method_resolution::TyFingerprint,
|
||||||
mir::{MirEvalError, MirLowerError},
|
mir::{MirEvalError, MirLowerError},
|
||||||
CastError, FnAbi, PointerCast, Safety,
|
CastError, FnAbi, PointerCast, Safety, Variance,
|
||||||
},
|
},
|
||||||
// FIXME: Properly encapsulate mir
|
// FIXME: Properly encapsulate mir
|
||||||
hir_ty::{mir, Interner as ChalkTyInterner},
|
hir_ty::{mir, Interner as ChalkTyInterner},
|
||||||
|
@ -3957,6 +3957,22 @@ impl GenericParam {
|
||||||
GenericParam::LifetimeParam(it) => it.id.parent.into(),
|
GenericParam::LifetimeParam(it) => it.id.parent.into(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn variance(self, db: &dyn HirDatabase) -> Option<Variance> {
|
||||||
|
let parent = match self {
|
||||||
|
GenericParam::TypeParam(it) => it.id.parent(),
|
||||||
|
// const parameters are always invariant
|
||||||
|
GenericParam::ConstParam(_) => return None,
|
||||||
|
GenericParam::LifetimeParam(it) => it.id.parent,
|
||||||
|
};
|
||||||
|
let generics = hir_ty::generics::generics(db.upcast(), parent);
|
||||||
|
let index = match self {
|
||||||
|
GenericParam::TypeParam(it) => generics.type_or_const_param_idx(it.id.into())?,
|
||||||
|
GenericParam::ConstParam(_) => return None,
|
||||||
|
GenericParam::LifetimeParam(it) => generics.lifetime_idx(it.id)?,
|
||||||
|
};
|
||||||
|
db.variances_of(parent)?.get(index).copied()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||||
|
|
|
@ -594,12 +594,21 @@ pub(super) fn definition(
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let variance_info = || match def {
|
||||||
|
Definition::GenericParam(it) => it.variance(db).as_ref().map(ToString::to_string),
|
||||||
|
_ => None,
|
||||||
|
};
|
||||||
|
|
||||||
let mut extra = String::new();
|
let mut extra = String::new();
|
||||||
if hovered_definition {
|
if hovered_definition {
|
||||||
if let Some(notable_traits) = render_notable_trait(db, notable_traits, edition) {
|
if let Some(notable_traits) = render_notable_trait(db, notable_traits, edition) {
|
||||||
extra.push_str("\n___\n");
|
extra.push_str("\n___\n");
|
||||||
extra.push_str(¬able_traits);
|
extra.push_str(¬able_traits);
|
||||||
}
|
}
|
||||||
|
if let Some(variance_info) = variance_info() {
|
||||||
|
extra.push_str("\n___\n");
|
||||||
|
extra.push_str(&variance_info);
|
||||||
|
}
|
||||||
if let Some(layout_info) = layout_info() {
|
if let Some(layout_info) = layout_info() {
|
||||||
extra.push_str("\n___\n");
|
extra.push_str("\n___\n");
|
||||||
extra.push_str(&layout_info);
|
extra.push_str(&layout_info);
|
||||||
|
|
|
@ -4721,7 +4721,7 @@ fn hover_type_param_sized_bounds() {
|
||||||
//- minicore: sized
|
//- minicore: sized
|
||||||
trait Trait {}
|
trait Trait {}
|
||||||
struct Foo<T>(T);
|
struct Foo<T>(T);
|
||||||
impl<T: Trait> Foo<T$0> {}
|
impl<T$0: Trait> Foo<T> {}
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
*T*
|
*T*
|
||||||
|
@ -4736,7 +4736,7 @@ impl<T: Trait> Foo<T$0> {}
|
||||||
//- minicore: sized
|
//- minicore: sized
|
||||||
trait Trait {}
|
trait Trait {}
|
||||||
struct Foo<T>(T);
|
struct Foo<T>(T);
|
||||||
impl<T: Trait + ?Sized> Foo<T$0> {}
|
impl<T$0: Trait + ?Sized> Foo<T> {}
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
*T*
|
*T*
|
||||||
|
@ -4764,6 +4764,10 @@ fn foo<T$0>() {}
|
||||||
```rust
|
```rust
|
||||||
T
|
T
|
||||||
```
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
invariant
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -4781,6 +4785,10 @@ fn foo<T$0: Sized>() {}
|
||||||
```rust
|
```rust
|
||||||
T
|
T
|
||||||
```
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
invariant
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -4798,6 +4806,10 @@ fn foo<T$0: ?Sized>() {}
|
||||||
```rust
|
```rust
|
||||||
T: ?Sized
|
T: ?Sized
|
||||||
```
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
invariant
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -4816,6 +4828,10 @@ fn foo<T$0: Trait>() {}
|
||||||
```rust
|
```rust
|
||||||
T: Trait
|
T: Trait
|
||||||
```
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
invariant
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -4834,6 +4850,10 @@ fn foo<T$0: Trait + Sized>() {}
|
||||||
```rust
|
```rust
|
||||||
T: Trait
|
T: Trait
|
||||||
```
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
invariant
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -4852,6 +4872,10 @@ fn foo<T$0: Trait + ?Sized>() {}
|
||||||
```rust
|
```rust
|
||||||
T: Trait + ?Sized
|
T: Trait + ?Sized
|
||||||
```
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
invariant
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -4869,6 +4893,10 @@ fn foo<T$0: ?Sized + Sized + Sized>() {}
|
||||||
```rust
|
```rust
|
||||||
T
|
T
|
||||||
```
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
invariant
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -4887,6 +4915,10 @@ fn foo<T$0: Sized + ?Sized + Sized + Trait>() {}
|
||||||
```rust
|
```rust
|
||||||
T: Trait
|
T: Trait
|
||||||
```
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
invariant
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue