mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-14 06:03:58 +00:00
Light docs and privacy
This commit is contained in:
parent
585cc9e014
commit
cf2b757a1a
3 changed files with 84 additions and 106 deletions
|
@ -494,7 +494,7 @@ impl HirDisplay for Const {
|
||||||
ConstValue::Placeholder(idx) => {
|
ConstValue::Placeholder(idx) => {
|
||||||
let id = from_placeholder_idx(f.db, *idx);
|
let id = from_placeholder_idx(f.db, *idx);
|
||||||
let generics = generics(f.db.upcast(), id.parent);
|
let generics = generics(f.db.upcast(), id.parent);
|
||||||
let param_data = &generics.params[id.local_id];
|
let param_data = &generics[id.local_id];
|
||||||
write!(f, "{}", param_data.name().unwrap().display(f.db.upcast()))?;
|
write!(f, "{}", param_data.name().unwrap().display(f.db.upcast()))?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -1216,7 +1216,7 @@ impl HirDisplay for Ty {
|
||||||
TyKind::Placeholder(idx) => {
|
TyKind::Placeholder(idx) => {
|
||||||
let id = from_placeholder_idx(db, *idx);
|
let id = from_placeholder_idx(db, *idx);
|
||||||
let generics = generics(db.upcast(), id.parent);
|
let generics = generics(db.upcast(), id.parent);
|
||||||
let param_data = &generics.params[id.local_id];
|
let param_data = &generics[id.local_id];
|
||||||
match param_data {
|
match param_data {
|
||||||
TypeOrConstParamData::TypeParamData(p) => match p.provenance {
|
TypeOrConstParamData::TypeParamData(p) => match p.provenance {
|
||||||
TypeParamProvenance::TypeParamList | TypeParamProvenance::TraitSelf => {
|
TypeParamProvenance::TypeParamList | TypeParamProvenance::TraitSelf => {
|
||||||
|
@ -1798,7 +1798,7 @@ impl HirDisplay for LifetimeData {
|
||||||
LifetimeData::Placeholder(idx) => {
|
LifetimeData::Placeholder(idx) => {
|
||||||
let id = lt_from_placeholder_idx(f.db, *idx);
|
let id = lt_from_placeholder_idx(f.db, *idx);
|
||||||
let generics = generics(f.db.upcast(), id.parent);
|
let generics = generics(f.db.upcast(), id.parent);
|
||||||
let param_data = &generics.params[id.local_id];
|
let param_data = &generics[id.local_id];
|
||||||
write!(f, "{}", param_data.name.display(f.db.upcast()))?;
|
write!(f, "{}", param_data.name.display(f.db.upcast()))?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,14 @@
|
||||||
|
//! Utilities for working with generics.
|
||||||
|
//!
|
||||||
|
//! The layout for generics as expected by chalk are as follows:
|
||||||
|
//! - Optional Self parameter
|
||||||
|
//! - Type or Const parameters
|
||||||
|
//! - Lifetime parameters
|
||||||
|
//! - Parent parameters
|
||||||
|
//!
|
||||||
|
//! where parent follows the same scheme.
|
||||||
|
use std::ops;
|
||||||
|
|
||||||
use chalk_ir::{cast::Cast as _, BoundVar, DebruijnIndex};
|
use chalk_ir::{cast::Cast as _, BoundVar, DebruijnIndex};
|
||||||
use hir_def::{
|
use hir_def::{
|
||||||
db::DefDatabase,
|
db::DefDatabase,
|
||||||
|
@ -5,12 +16,12 @@ use hir_def::{
|
||||||
GenericParamDataRef, GenericParams, LifetimeParamData, TypeOrConstParamData,
|
GenericParamDataRef, GenericParams, LifetimeParamData, TypeOrConstParamData,
|
||||||
TypeParamProvenance,
|
TypeParamProvenance,
|
||||||
},
|
},
|
||||||
ConstParamId, GenericDefId, GenericParamId, ItemContainerId, LifetimeParamId, Lookup,
|
ConstParamId, GenericDefId, GenericParamId, ItemContainerId, LifetimeParamId,
|
||||||
TypeOrConstParamId, TypeParamId,
|
LocalLifetimeParamId, LocalTypeOrConstParamId, Lookup, TypeOrConstParamId, TypeParamId,
|
||||||
};
|
};
|
||||||
use intern::Interned;
|
use intern::Interned;
|
||||||
|
|
||||||
use crate::{db::HirDatabase, 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(crate) 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)));
|
||||||
|
@ -19,85 +30,46 @@ pub(crate) fn generics(db: &dyn DefDatabase, def: GenericDefId) -> Generics {
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub(crate) struct Generics {
|
pub(crate) struct Generics {
|
||||||
def: GenericDefId,
|
def: GenericDefId,
|
||||||
pub(crate) params: Interned<GenericParams>,
|
params: Interned<GenericParams>,
|
||||||
parent_generics: Option<Box<Generics>>,
|
parent_generics: Option<Box<Generics>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Generics {
|
impl<T> ops::Index<T> for Generics
|
||||||
pub(crate) fn iter_id(&self) -> impl Iterator<Item = GenericParamId> + '_ {
|
where
|
||||||
self.iter().map(|(id, _)| id)
|
GenericParams: ops::Index<T>,
|
||||||
|
{
|
||||||
|
type Output = <GenericParams as ops::Index<T>>::Output;
|
||||||
|
fn index(&self, index: T) -> &Self::Output {
|
||||||
|
&self.params[index]
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Generics {
|
||||||
pub(crate) fn def(&self) -> GenericDefId {
|
pub(crate) fn def(&self) -> GenericDefId {
|
||||||
self.def
|
self.def
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Iterator over types and const params of self, then parent.
|
pub(crate) fn iter_id(&self) -> impl Iterator<Item = GenericParamId> + '_ {
|
||||||
pub(crate) fn iter<'a>(
|
self.iter().map(|(id, _)| id)
|
||||||
&'a self,
|
|
||||||
) -> impl DoubleEndedIterator<Item = (GenericParamId, GenericParamDataRef<'a>)> + 'a {
|
|
||||||
let from_toc_id = |it: &'a Generics| {
|
|
||||||
move |(local_id, p): (_, &'a TypeOrConstParamData)| {
|
|
||||||
let id = TypeOrConstParamId { parent: it.def, local_id };
|
|
||||||
match p {
|
|
||||||
TypeOrConstParamData::TypeParamData(p) => (
|
|
||||||
GenericParamId::TypeParamId(TypeParamId::from_unchecked(id)),
|
|
||||||
GenericParamDataRef::TypeParamData(p),
|
|
||||||
),
|
|
||||||
TypeOrConstParamData::ConstParamData(p) => (
|
|
||||||
GenericParamId::ConstParamId(ConstParamId::from_unchecked(id)),
|
|
||||||
GenericParamDataRef::ConstParamData(p),
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let from_lt_id = |it: &'a Generics| {
|
|
||||||
move |(local_id, p): (_, &'a LifetimeParamData)| {
|
|
||||||
(
|
|
||||||
GenericParamId::LifetimeParamId(LifetimeParamId { parent: it.def, local_id }),
|
|
||||||
GenericParamDataRef::LifetimeParamData(p),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let lt_iter = self.params.iter_lt().map(from_lt_id(self));
|
|
||||||
self.params
|
|
||||||
.iter_type_or_consts()
|
|
||||||
.map(from_toc_id(self))
|
|
||||||
.chain(lt_iter)
|
|
||||||
.chain(self.iter_parent())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Iterate over types and const params without parent params.
|
pub(crate) fn iter_self_type_or_consts(
|
||||||
pub(crate) fn iter_self<'a>(
|
&self,
|
||||||
&'a self,
|
) -> impl DoubleEndedIterator<Item = (LocalTypeOrConstParamId, &TypeOrConstParamData)> {
|
||||||
) -> impl DoubleEndedIterator<Item = (GenericParamId, GenericParamDataRef<'a>)> + 'a {
|
self.params.iter_type_or_consts()
|
||||||
let from_toc_id = |it: &'a Generics| {
|
}
|
||||||
move |(local_id, p): (_, &'a TypeOrConstParamData)| {
|
|
||||||
let id = TypeOrConstParamId { parent: it.def, local_id };
|
|
||||||
match p {
|
|
||||||
TypeOrConstParamData::TypeParamData(p) => (
|
|
||||||
GenericParamId::TypeParamId(TypeParamId::from_unchecked(id)),
|
|
||||||
GenericParamDataRef::TypeParamData(p),
|
|
||||||
),
|
|
||||||
TypeOrConstParamData::ConstParamData(p) => (
|
|
||||||
GenericParamId::ConstParamId(ConstParamId::from_unchecked(id)),
|
|
||||||
GenericParamDataRef::ConstParamData(p),
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let from_lt_id = |it: &'a Generics| {
|
/// Iterate over the params followed by the parent params.
|
||||||
move |(local_id, p): (_, &'a LifetimeParamData)| {
|
pub(crate) fn iter(
|
||||||
(
|
&self,
|
||||||
GenericParamId::LifetimeParamId(LifetimeParamId { parent: it.def, local_id }),
|
) -> impl DoubleEndedIterator<Item = (GenericParamId, GenericParamDataRef<'_>)> + '_ {
|
||||||
GenericParamDataRef::LifetimeParamData(p),
|
self.iter_self().chain(self.iter_parent())
|
||||||
)
|
}
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
/// Iterate over the params without parent params.
|
||||||
|
pub(crate) fn iter_self(
|
||||||
|
&self,
|
||||||
|
) -> impl DoubleEndedIterator<Item = (GenericParamId, GenericParamDataRef<'_>)> + '_ {
|
||||||
self.params
|
self.params
|
||||||
.iter_type_or_consts()
|
.iter_type_or_consts()
|
||||||
.map(from_toc_id(self))
|
.map(from_toc_id(self))
|
||||||
|
@ -109,29 +81,8 @@ impl Generics {
|
||||||
&self,
|
&self,
|
||||||
) -> impl DoubleEndedIterator<Item = (GenericParamId, GenericParamDataRef<'_>)> + '_ {
|
) -> impl DoubleEndedIterator<Item = (GenericParamId, GenericParamDataRef<'_>)> + '_ {
|
||||||
self.parent_generics().into_iter().flat_map(|it| {
|
self.parent_generics().into_iter().flat_map(|it| {
|
||||||
let from_toc_id = move |(local_id, p)| {
|
let lt_iter = it.params.iter_lt().map(from_lt_id(it));
|
||||||
let p: &_ = p;
|
it.params.iter_type_or_consts().map(from_toc_id(it)).chain(lt_iter)
|
||||||
let id = TypeOrConstParamId { parent: it.def, local_id };
|
|
||||||
match p {
|
|
||||||
TypeOrConstParamData::TypeParamData(p) => (
|
|
||||||
GenericParamId::TypeParamId(TypeParamId::from_unchecked(id)),
|
|
||||||
GenericParamDataRef::TypeParamData(p),
|
|
||||||
),
|
|
||||||
TypeOrConstParamData::ConstParamData(p) => (
|
|
||||||
GenericParamId::ConstParamId(ConstParamId::from_unchecked(id)),
|
|
||||||
GenericParamDataRef::ConstParamData(p),
|
|
||||||
),
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let from_lt_id = move |(local_id, p): (_, _)| {
|
|
||||||
(
|
|
||||||
GenericParamId::LifetimeParamId(LifetimeParamId { parent: it.def, local_id }),
|
|
||||||
GenericParamDataRef::LifetimeParamData(p),
|
|
||||||
)
|
|
||||||
};
|
|
||||||
let lt_iter = it.params.iter_lt().map(from_lt_id);
|
|
||||||
it.params.iter_type_or_consts().map(from_toc_id).chain(lt_iter)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -147,11 +98,6 @@ impl Generics {
|
||||||
self.params.len()
|
self.params.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns number of generic parameter excluding those from parent
|
|
||||||
fn len_type_and_const_params(&self) -> usize {
|
|
||||||
self.params.type_or_consts.len()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// (parent total, self param, type params, const params, impl trait list, lifetimes)
|
/// (parent total, self param, type params, const params, impl trait list, lifetimes)
|
||||||
pub(crate) fn provenance_split(&self) -> (usize, usize, usize, usize, usize, usize) {
|
pub(crate) fn provenance_split(&self) -> (usize, usize, usize, usize, usize, usize) {
|
||||||
let mut self_params = 0;
|
let mut self_params = 0;
|
||||||
|
@ -207,7 +153,7 @@ impl Generics {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
Some((
|
Some((
|
||||||
self.len_type_and_const_params() + idx,
|
self.params.type_or_consts.len() + idx,
|
||||||
&self.params.lifetimes[lifetime.local_id],
|
&self.params.lifetimes[lifetime.local_id],
|
||||||
))
|
))
|
||||||
} else {
|
} else {
|
||||||
|
@ -253,13 +199,13 @@ impl Generics {
|
||||||
Interner,
|
Interner,
|
||||||
self.iter_id().map(|id| match id {
|
self.iter_id().map(|id| match id {
|
||||||
GenericParamId::TypeParamId(id) => {
|
GenericParamId::TypeParamId(id) => {
|
||||||
crate::to_placeholder_idx(db, id.into()).to_ty(Interner).cast(Interner)
|
to_placeholder_idx(db, id.into()).to_ty(Interner).cast(Interner)
|
||||||
}
|
}
|
||||||
GenericParamId::ConstParamId(id) => crate::to_placeholder_idx(db, id.into())
|
GenericParamId::ConstParamId(id) => to_placeholder_idx(db, id.into())
|
||||||
.to_const(Interner, db.const_param_ty(id))
|
.to_const(Interner, db.const_param_ty(id))
|
||||||
.cast(Interner),
|
.cast(Interner),
|
||||||
GenericParamId::LifetimeParamId(id) => {
|
GenericParamId::LifetimeParamId(id) => {
|
||||||
crate::lt_to_placeholder_idx(db, id).to_lifetime(Interner).cast(Interner)
|
lt_to_placeholder_idx(db, id).to_lifetime(Interner).cast(Interner)
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
@ -284,3 +230,35 @@ fn parent_generic_def(db: &dyn DefDatabase, def: GenericDefId) -> Option<Generic
|
||||||
ItemContainerId::ModuleId(_) | ItemContainerId::ExternBlockId(_) => None,
|
ItemContainerId::ModuleId(_) | ItemContainerId::ExternBlockId(_) => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn from_toc_id<'a>(
|
||||||
|
it: &'a Generics,
|
||||||
|
) -> impl Fn(
|
||||||
|
(LocalTypeOrConstParamId, &'a TypeOrConstParamData),
|
||||||
|
) -> (GenericParamId, GenericParamDataRef<'a>) {
|
||||||
|
move |(local_id, p): (_, _)| {
|
||||||
|
let id = TypeOrConstParamId { parent: it.def, local_id };
|
||||||
|
match p {
|
||||||
|
TypeOrConstParamData::TypeParamData(p) => (
|
||||||
|
GenericParamId::TypeParamId(TypeParamId::from_unchecked(id)),
|
||||||
|
GenericParamDataRef::TypeParamData(p),
|
||||||
|
),
|
||||||
|
TypeOrConstParamData::ConstParamData(p) => (
|
||||||
|
GenericParamId::ConstParamId(ConstParamId::from_unchecked(id)),
|
||||||
|
GenericParamDataRef::ConstParamData(p),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn from_lt_id<'a>(
|
||||||
|
it: &'a Generics,
|
||||||
|
) -> impl Fn((LocalLifetimeParamId, &'a LifetimeParamData)) -> (GenericParamId, GenericParamDataRef<'a>)
|
||||||
|
{
|
||||||
|
move |(local_id, p): (_, _)| {
|
||||||
|
(
|
||||||
|
GenericParamId::LifetimeParamId(LifetimeParamId { parent: it.def, local_id }),
|
||||||
|
GenericParamDataRef::LifetimeParamData(p),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1178,7 +1178,7 @@ impl<'a> TyLoweringContext<'a> {
|
||||||
let ty = if let Some(target_param_idx) = target_param_idx {
|
let ty = if let Some(target_param_idx) = target_param_idx {
|
||||||
let mut counter = 0;
|
let mut counter = 0;
|
||||||
let generics = self.generics().expect("generics in scope");
|
let generics = self.generics().expect("generics in scope");
|
||||||
for (idx, data) in generics.params.type_or_consts.iter() {
|
for (idx, data) in generics.iter_self_type_or_consts() {
|
||||||
// Count the number of `impl Trait` things that appear before
|
// Count the number of `impl Trait` things that appear before
|
||||||
// the target of our `bound`.
|
// the target of our `bound`.
|
||||||
// Our counter within `impl_trait_mode` should be that number
|
// Our counter within `impl_trait_mode` should be that number
|
||||||
|
@ -1480,7 +1480,7 @@ fn named_associated_type_shorthand_candidates<R>(
|
||||||
// Handle `Self::Type` referring to own associated type in trait definitions
|
// Handle `Self::Type` referring to own associated type in trait definitions
|
||||||
if let GenericDefId::TraitId(trait_id) = param_id.parent() {
|
if let GenericDefId::TraitId(trait_id) = param_id.parent() {
|
||||||
let trait_generics = generics(db.upcast(), trait_id.into());
|
let trait_generics = generics(db.upcast(), trait_id.into());
|
||||||
if trait_generics.params[param_id.local_id()].is_trait_self() {
|
if trait_generics[param_id.local_id()].is_trait_self() {
|
||||||
let def_generics = generics(db.upcast(), def);
|
let def_generics = generics(db.upcast(), def);
|
||||||
let starting_idx = match def {
|
let starting_idx = match def {
|
||||||
GenericDefId::TraitId(_) => 0,
|
GenericDefId::TraitId(_) => 0,
|
||||||
|
|
Loading…
Reference in a new issue