Skip to content

Commit

Permalink
Merge #11140
Browse files Browse the repository at this point in the history
11140: Preserve order of generic args r=HKalbasi a=HKalbasi

rust-lang/rust#90207 removed order restriction of generic args, i.e. const generics can now become before of type generics. We need to preserve this order to analyze correctly, and this PR does that.

It also simplifies implementation of const generics a bit IMO.

Implementing default generics the same problem of #7434, we need lower them to body and then evaluate them.


Co-authored-by: hkalbasi <[email protected]>
  • Loading branch information
bors[bot] and HKalbasi authored Mar 4, 2022
2 parents 9d473a0 + 660fd4a commit f8329ba
Show file tree
Hide file tree
Showing 38 changed files with 712 additions and 424 deletions.
4 changes: 2 additions & 2 deletions crates/hir/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ fn resolve_doc_path(
AttrDefId::ImplId(it) => it.resolver(db.upcast()),
AttrDefId::ExternBlockId(it) => it.resolver(db.upcast()),
AttrDefId::GenericParamId(it) => match it {
GenericParamId::TypeParamId(it) => it.parent,
GenericParamId::TypeParamId(it) => it.parent(),
GenericParamId::ConstParamId(it) => it.parent(),
GenericParamId::LifetimeParamId(it) => it.parent,
GenericParamId::ConstParamId(it) => it.parent,
}
.resolver(db.upcast()),
// FIXME
Expand Down
65 changes: 40 additions & 25 deletions crates/hir/src/display.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! HirDisplay implementations for various hir types.
use hir_def::{
adt::VariantData,
generics::{TypeParamProvenance, WherePredicate, WherePredicateTypeTarget},
generics::{
TypeOrConstParamData, TypeParamProvenance, WherePredicate, WherePredicateTypeTarget,
},
type_ref::{TypeBound, TypeRef},
AdtId, GenericDefId,
};
Expand All @@ -16,8 +18,8 @@ use syntax::SmolStr;

use crate::{
Adt, Const, ConstParam, Enum, Field, Function, GenericParam, HasCrate, HasVisibility,
LifetimeParam, Module, Static, Struct, Trait, TyBuilder, Type, TypeAlias, TypeParam, Union,
Variant,
LifetimeParam, Module, Static, Struct, Trait, TyBuilder, Type, TypeAlias, TypeOrConstParam,
TypeParam, Union, Variant,
};

impl HirDisplay for Function {
Expand Down Expand Up @@ -226,8 +228,17 @@ impl HirDisplay for GenericParam {
fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
match self {
GenericParam::TypeParam(it) => it.hir_fmt(f),
GenericParam::LifetimeParam(it) => it.hir_fmt(f),
GenericParam::ConstParam(it) => it.hir_fmt(f),
GenericParam::LifetimeParam(it) => it.hir_fmt(f),
}
}
}

impl HirDisplay for TypeOrConstParam {
fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
match self.split(f.db) {
either::Either::Left(x) => x.hir_fmt(f),
either::Either::Right(x) => x.hir_fmt(f),
}
}
}
Expand All @@ -239,11 +250,11 @@ impl HirDisplay for TypeParam {
return Ok(());
}

let bounds = f.db.generic_predicates_for_param(self.id.parent, self.id, None);
let substs = TyBuilder::type_params_subst(f.db, self.id.parent);
let bounds = f.db.generic_predicates_for_param(self.id.parent(), self.id.into(), None);
let substs = TyBuilder::type_params_subst(f.db, self.id.parent());
let predicates: Vec<_> =
bounds.iter().cloned().map(|b| b.substitute(Interner, &substs)).collect();
let krate = self.id.parent.krate(f.db).id;
let krate = self.id.parent().krate(f.db).id;
let sized_trait =
f.db.lang_item(krate, SmolStr::new_inline("sized"))
.and_then(|lang_item| lang_item.as_trait());
Expand Down Expand Up @@ -276,11 +287,11 @@ impl HirDisplay for ConstParam {
fn write_generic_params(def: GenericDefId, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
let params = f.db.generic_params(def);
if params.lifetimes.is_empty()
&& params.consts.is_empty()
&& params
.types
.iter()
.all(|(_, param)| !matches!(param.provenance, TypeParamProvenance::TypeParamList))
.filter_map(|x| x.1.type_param())
.all(|param| !matches!(param.provenance, TypeParamProvenance::TypeParamList))
{
return Ok(());
}
Expand All @@ -300,23 +311,27 @@ fn write_generic_params(def: GenericDefId, f: &mut HirFormatter) -> Result<(), H
write!(f, "{}", lifetime.name)?;
}
for (_, ty) in params.types.iter() {
if ty.provenance != TypeParamProvenance::TypeParamList {
continue;
}
if let Some(name) = &ty.name {
delim(f)?;
write!(f, "{}", name)?;
if let Some(default) = &ty.default {
write!(f, " = ")?;
default.hir_fmt(f)?;
if let Some(name) = &ty.name() {
match ty {
TypeOrConstParamData::TypeParamData(ty) => {
if ty.provenance != TypeParamProvenance::TypeParamList {
continue;
}
delim(f)?;
write!(f, "{}", name)?;
if let Some(default) = &ty.default {
write!(f, " = ")?;
default.hir_fmt(f)?;
}
}
TypeOrConstParamData::ConstParamData(c) => {
delim(f)?;
write!(f, "const {}: ", name)?;
c.ty.hir_fmt(f)?;
}
}
}
}
for (_, konst) in params.consts.iter() {
delim(f)?;
write!(f, "const {}: ", konst.name)?;
konst.ty.hir_fmt(f)?;
}

write!(f, ">")?;
Ok(())
Expand All @@ -328,7 +343,7 @@ fn write_where_clause(def: GenericDefId, f: &mut HirFormatter) -> Result<(), Hir
// unnamed type targets are displayed inline with the argument itself, e.g. `f: impl Y`.
let is_unnamed_type_target = |target: &WherePredicateTypeTarget| match target {
WherePredicateTypeTarget::TypeRef(_) => false,
WherePredicateTypeTarget::TypeParam(id) => params.types[*id].name.is_none(),
WherePredicateTypeTarget::TypeOrConstParam(id) => params.types[*id].name().is_none(),
};

let has_displayable_predicate = params
Expand All @@ -344,7 +359,7 @@ fn write_where_clause(def: GenericDefId, f: &mut HirFormatter) -> Result<(), Hir

let write_target = |target: &WherePredicateTypeTarget, f: &mut HirFormatter| match target {
WherePredicateTypeTarget::TypeRef(ty) => ty.hir_fmt(f),
WherePredicateTypeTarget::TypeParam(id) => match &params.types[*id].name {
WherePredicateTypeTarget::TypeOrConstParam(id) => match &params.types[*id].name() {
Some(name) => write!(f, "{}", name),
None => write!(f, "{{unnamed}}"),
},
Expand Down
9 changes: 5 additions & 4 deletions crates/hir/src/from_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ from_id![
(hir_def::ConstId, crate::Const),
(hir_def::FunctionId, crate::Function),
(hir_def::ImplId, crate::Impl),
(hir_def::TypeOrConstParamId, crate::TypeOrConstParam),
(hir_def::TypeParamId, crate::TypeParam),
(hir_def::LifetimeParamId, crate::LifetimeParam),
(hir_def::ConstParamId, crate::ConstParam),
(hir_def::LifetimeParamId, crate::LifetimeParam),
(hir_expand::MacroDefId, crate::MacroDef)
];

Expand Down Expand Up @@ -71,18 +72,18 @@ impl From<GenericParamId> for GenericParam {
fn from(id: GenericParamId) -> Self {
match id {
GenericParamId::TypeParamId(it) => GenericParam::TypeParam(it.into()),
GenericParamId::LifetimeParamId(it) => GenericParam::LifetimeParam(it.into()),
GenericParamId::ConstParamId(it) => GenericParam::ConstParam(it.into()),
GenericParamId::LifetimeParamId(it) => GenericParam::LifetimeParam(it.into()),
}
}
}

impl From<GenericParam> for GenericParamId {
fn from(id: GenericParam) -> Self {
match id {
GenericParam::TypeParam(it) => GenericParamId::TypeParamId(it.id),
GenericParam::LifetimeParam(it) => GenericParamId::LifetimeParamId(it.id),
GenericParam::ConstParam(it) => GenericParamId::ConstParamId(it.id),
GenericParam::ConstParam(it) => GenericParamId::ConstParamId(it.id.into()),
GenericParam::TypeParam(it) => GenericParamId::TypeParamId(it.id.into()),
}
}
}
Expand Down
16 changes: 4 additions & 12 deletions crates/hir/src/has_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use hir_expand::InFile;
use syntax::ast;

use crate::{
db::HirDatabase, Adt, Const, ConstParam, Enum, Field, FieldSource, Function, Impl,
LifetimeParam, MacroDef, Module, Static, Struct, Trait, TypeAlias, TypeParam, Union, Variant,
db::HirDatabase, Adt, Const, Enum, Field, FieldSource, Function, Impl, LifetimeParam, MacroDef,
Module, Static, Struct, Trait, TypeAlias, TypeOrConstParam, Union, Variant,
};

pub trait HasSource {
Expand Down Expand Up @@ -139,8 +139,8 @@ impl HasSource for Impl {
}
}

impl HasSource for TypeParam {
type Ast = Either<ast::TypeParam, ast::Trait>;
impl HasSource for TypeOrConstParam {
type Ast = Either<ast::TypeOrConstParam, ast::Trait>;
fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
let child_source = self.id.parent.child_source(db.upcast());
Some(child_source.map(|it| it[self.id.local_id].clone()))
Expand All @@ -154,11 +154,3 @@ impl HasSource for LifetimeParam {
Some(child_source.map(|it| it[self.id.local_id].clone()))
}
}

impl HasSource for ConstParam {
type Ast = ast::ConstParam;
fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
let child_source = self.id.parent.child_source(db.upcast());
Some(child_source.map(|it| it[self.id.local_id].clone()))
}
}
Loading

0 comments on commit f8329ba

Please sign in to comment.