Skip to content

Commit

Permalink
Auto merge of rust-lang#17530 - Veykril:lifetime-order, r=Veykril
Browse files Browse the repository at this point in the history
Move lifetimes in front of type and const params but after self
  • Loading branch information
bors committed Jul 2, 2024
2 parents 73c97e3 + baa959f commit 4981f00
Show file tree
Hide file tree
Showing 12 changed files with 333 additions and 329 deletions.
4 changes: 2 additions & 2 deletions crates/hir-def/src/child_by_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ impl ChildBySource for GenericDefId {
}

let generic_params = db.generic_params(*self);
let mut toc_idx_iter = generic_params.type_or_consts.iter().map(|(idx, _)| idx);
let lts_idx_iter = generic_params.lifetimes.iter().map(|(idx, _)| idx);
let mut toc_idx_iter = generic_params.iter_type_or_consts().map(|(idx, _)| idx);
let lts_idx_iter = generic_params.iter_lt().map(|(idx, _)| idx);

// For traits the first type index is `Self`, skip it.
if let GenericDefId::TraitId(_) = *self {
Expand Down
417 changes: 217 additions & 200 deletions crates/hir-def/src/generics.rs

Large diffs are not rendered by default.

18 changes: 8 additions & 10 deletions crates/hir-def/src/item_tree/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,15 +532,15 @@ impl Printer<'_> {

w!(self, "<");
let mut first = true;
for (idx, lt) in params.lifetimes.iter() {
for (idx, lt) in params.iter_lt() {
if !first {
w!(self, ", ");
}
first = false;
self.print_attrs_of(AttrOwner::LifetimeParamData(parent, idx), " ");
w!(self, "{}", lt.name.display(self.db.upcast()));
}
for (idx, x) in params.type_or_consts.iter() {
for (idx, x) in params.iter_type_or_consts() {
if !first {
w!(self, ", ");
}
Expand Down Expand Up @@ -570,13 +570,13 @@ impl Printer<'_> {
}

fn print_where_clause(&mut self, params: &GenericParams) -> bool {
if params.where_predicates.is_empty() {
if params.where_predicates().next().is_none() {
return false;
}

w!(self, "\nwhere");
self.indented(|this| {
for (i, pred) in params.where_predicates.iter().enumerate() {
for (i, pred) in params.where_predicates().enumerate() {
if i != 0 {
wln!(this, ",");
}
Expand Down Expand Up @@ -607,12 +607,10 @@ impl Printer<'_> {

match target {
WherePredicateTypeTarget::TypeRef(ty) => this.print_type_ref(ty),
WherePredicateTypeTarget::TypeOrConstParam(id) => {
match &params.type_or_consts[*id].name() {
Some(name) => w!(this, "{}", name.display(self.db.upcast())),
None => w!(this, "_anon_{}", id.into_raw()),
}
}
WherePredicateTypeTarget::TypeOrConstParam(id) => match params[*id].name() {
Some(name) => w!(this, "{}", name.display(self.db.upcast())),
None => w!(this, "_anon_{}", id.into_raw()),
},
}
w!(this, ": ");
this.print_type_bounds(std::slice::from_ref(bound));
Expand Down
8 changes: 4 additions & 4 deletions crates/hir-def/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ impl Resolver {
Scope::GenericParams { params, def } => Some((params, def)),
_ => None,
})
.flat_map(|(params, def)| params.where_predicates.iter().zip(iter::repeat(def)))
.flat_map(|(params, def)| params.where_predicates().zip(iter::repeat(def)))
}

pub fn generic_def(&self) -> Option<GenericDefId> {
Expand Down Expand Up @@ -758,10 +758,10 @@ impl Scope {
}
Scope::GenericParams { params, def: parent } => {
let parent = *parent;
for (local_id, param) in params.type_or_consts.iter() {
for (local_id, param) in params.iter_type_or_consts() {
if let Some(name) = &param.name() {
let id = TypeOrConstParamId { parent, local_id };
let data = &db.generic_params(parent).type_or_consts[local_id];
let data = &db.generic_params(parent)[local_id];
acc.add(
name,
ScopeDef::GenericParam(match data {
Expand All @@ -775,7 +775,7 @@ impl Scope {
);
}
}
for (local_id, param) in params.lifetimes.iter() {
for (local_id, param) in params.iter_lt() {
let id = LifetimeParamId { parent, local_id };
acc.add(&param.name, ScopeDef::GenericParam(id.into()))
}
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-def/src/src.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl HasChildSource<LocalTypeOrConstParamId> for GenericDefId {
db: &dyn DefDatabase,
) -> InFile<ArenaMap<LocalTypeOrConstParamId, Self::Value>> {
let generic_params = db.generic_params(*self);
let mut idx_iter = generic_params.type_or_consts.iter().map(|(idx, _)| idx);
let mut idx_iter = generic_params.iter_type_or_consts().map(|(idx, _)| idx);

let (file_id, generic_params_list) = self.file_id_and_params_of(db);

Expand Down Expand Up @@ -103,7 +103,7 @@ impl HasChildSource<LocalLifetimeParamId> for GenericDefId {
db: &dyn DefDatabase,
) -> InFile<ArenaMap<LocalLifetimeParamId, Self::Value>> {
let generic_params = db.generic_params(*self);
let idx_iter = generic_params.lifetimes.iter().map(|(idx, _)| idx);
let idx_iter = generic_params.iter_lt().map(|(idx, _)| idx);

let (file_id, generic_params_list) = self.file_id_and_params_of(db);

Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/chalk_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ impl TyExt for Ty {
TyKind::Placeholder(idx) => {
let id = from_placeholder_idx(db, *idx);
let generic_params = db.generic_params(id.parent);
let param_data = &generic_params.type_or_consts[id.local_id];
let param_data = &generic_params[id.local_id];
match param_data {
TypeOrConstParamData::TypeParamData(p) => match p.provenance {
hir_def::generics::TypeParamProvenance::ArgumentImplTrait => {
Expand Down
35 changes: 19 additions & 16 deletions crates/hir-ty/src/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
//!
//! The layout for generics as expected by chalk are as follows:
//! - Optional Self parameter
//! - Type or Const parameters
//! - Lifetime parameters
//! - Type or Const parameters
//! - Parent parameters
//!
//! where parent follows the same scheme.
Expand All @@ -20,19 +20,23 @@ use hir_def::{
LocalLifetimeParamId, LocalTypeOrConstParamId, Lookup, TypeOrConstParamId, TypeParamId,
};
use intern::Interned;
use itertools::chain;
use stdx::TupleExt;

use crate::{db::HirDatabase, lt_to_placeholder_idx, to_placeholder_idx, Interner, Substitution};

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)));
Generics { def, params: db.generic_params(def), parent_generics }
let params = db.generic_params(def);
let has_trait_self_param = params.trait_self_param().is_some();
Generics { def, params, parent_generics, has_trait_self_param }
}
#[derive(Clone, Debug)]
pub(crate) struct Generics {
def: GenericDefId,
params: Interned<GenericParams>,
parent_generics: Option<Box<Generics>>,
has_trait_self_param: bool,
}

impl<T> ops::Index<T> for Generics
Expand Down Expand Up @@ -74,10 +78,6 @@ impl Generics {
self.params.iter_type_or_consts().map(from_toc_id(self)).map(TupleExt::head)
}

pub(crate) fn iter_self_lt_id(&self) -> impl DoubleEndedIterator<Item = GenericParamId> + '_ {
self.params.iter_lt().map(from_lt_id(self)).map(TupleExt::head)
}

/// Iterate over the params followed by the parent params.
pub(crate) fn iter(
&self,
Expand All @@ -89,19 +89,19 @@ impl Generics {
pub(crate) fn iter_self(
&self,
) -> impl DoubleEndedIterator<Item = (GenericParamId, GenericParamDataRef<'_>)> + '_ {
self.params
.iter_type_or_consts()
.map(from_toc_id(self))
.chain(self.params.iter_lt().map(from_lt_id(self)))
let mut toc = self.params.iter_type_or_consts().map(from_toc_id(self));
let trait_self_param = self.has_trait_self_param.then(|| toc.next()).flatten();
chain!(trait_self_param, self.params.iter_lt().map(from_lt_id(self)), toc)
}

/// Iterator over types and const params of parent.
fn iter_parent(
&self,
) -> impl DoubleEndedIterator<Item = (GenericParamId, GenericParamDataRef<'_>)> + '_ {
self.parent_generics().into_iter().flat_map(|it| {
let lt_iter = it.params.iter_lt().map(from_lt_id(it));
it.params.iter_type_or_consts().map(from_toc_id(it)).chain(lt_iter)
let mut toc = it.params.iter_type_or_consts().map(from_toc_id(it));
let trait_self_param = it.has_trait_self_param.then(|| toc.next()).flatten();
chain!(trait_self_param, it.params.iter_lt().map(from_lt_id(it)), toc)
})
}

Expand Down Expand Up @@ -145,8 +145,11 @@ impl Generics {
fn find_type_or_const_param(&self, param: TypeOrConstParamId) -> Option<usize> {
if param.parent == self.def {
let idx = param.local_id.into_raw().into_u32() as usize;
debug_assert!(idx <= self.params.type_or_consts.len());
Some(idx)
debug_assert!(idx <= self.params.len_type_or_consts());
if self.params.trait_self_param() == Some(param.local_id) {
return Some(idx);
}
Some(self.params.len_lifetimes() + idx)
} else {
debug_assert_eq!(self.parent_generics().map(|it| it.def), Some(param.parent));
self.parent_generics()
Expand All @@ -163,8 +166,8 @@ impl Generics {
fn find_lifetime(&self, lifetime: LifetimeParamId) -> Option<usize> {
if lifetime.parent == self.def {
let idx = lifetime.local_id.into_raw().into_u32() as usize;
debug_assert!(idx <= self.params.lifetimes.len());
Some(self.params.type_or_consts.len() + idx)
debug_assert!(idx <= self.params.len_lifetimes());
Some(self.params.trait_self_param().is_some() as usize + idx)
} else {
debug_assert_eq!(self.parent_generics().map(|it| it.def), Some(lifetime.parent));
self.parent_generics()
Expand Down
Loading

0 comments on commit 4981f00

Please sign in to comment.