Add Lifetimes to the HIR

This commit is contained in:
Lukas Wirth 2020-12-11 13:49:32 +01:00
parent 41321d9678
commit 11f8664182
16 changed files with 249 additions and 96 deletions

View file

@ -89,7 +89,7 @@ impl<'a> SubstituteTypeParams<'a> {
let substs = get_syntactic_substs(impl_def).unwrap_or_default(); let substs = get_syntactic_substs(impl_def).unwrap_or_default();
let generic_def: hir::GenericDef = trait_.into(); let generic_def: hir::GenericDef = trait_.into();
let substs_by_param: FxHashMap<_, _> = generic_def let substs_by_param: FxHashMap<_, _> = generic_def
.params(source_scope.db) .type_params(source_scope.db)
.into_iter() .into_iter()
// this is a trait impl, so we need to skip the first type parameter -- this is a bit hacky // this is a trait impl, so we need to skip the first type parameter -- this is a bit hacky
.skip(1) .skip(1)

View file

@ -19,8 +19,9 @@ use hir_def::{
src::HasSource as _, src::HasSource as _,
type_ref::{Mutability, TypeRef}, type_ref::{Mutability, TypeRef},
AdtId, AssocContainerId, AssocItemId, AssocItemLoc, AttrDefId, ConstId, DefWithBodyId, EnumId, AdtId, AssocContainerId, AssocItemId, AssocItemLoc, AttrDefId, ConstId, DefWithBodyId, EnumId,
FunctionId, GenericDefId, HasModule, ImplId, LocalEnumVariantId, LocalFieldId, LocalModuleId, FunctionId, GenericDefId, HasModule, ImplId, LifetimeParamId, LocalEnumVariantId, LocalFieldId,
Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId, TypeParamId, UnionId, LocalModuleId, Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId, TypeParamId,
UnionId,
}; };
use hir_def::{find_path::PrefixKind, item_scope::ItemInNs, visibility::Visibility}; use hir_def::{find_path::PrefixKind, item_scope::ItemInNs, visibility::Visibility};
use hir_expand::{ use hir_expand::{
@ -831,7 +832,7 @@ impl SelfParam {
.params .params
.first() .first()
.map(|param| match *param { .map(|param| match *param {
TypeRef::Reference(_, mutability) => mutability.into(), TypeRef::Reference(.., mutability) => mutability.into(),
_ => Access::Owned, _ => Access::Owned,
}) })
.unwrap_or(Access::Owned) .unwrap_or(Access::Owned)
@ -1098,8 +1099,25 @@ impl_from!(
); );
impl GenericDef { impl GenericDef {
pub fn params(self, db: &dyn HirDatabase) -> Vec<TypeParam> { pub fn params(self, db: &dyn HirDatabase) -> Vec<GenericParam> {
let generics: Arc<hir_def::generics::GenericParams> = db.generic_params(self.into()); let generics = db.generic_params(self.into());
let ty_params = generics
.types
.iter()
.map(|(local_id, _)| TypeParam { id: TypeParamId { parent: self.into(), local_id } })
.map(GenericParam::TypeParam);
let lt_params = generics
.lifetimes
.iter()
.map(|(local_id, _)| LifetimeParam {
id: LifetimeParamId { parent: self.into(), local_id },
})
.map(GenericParam::LifetimeParam);
ty_params.chain(lt_params).collect()
}
pub fn type_params(self, db: &dyn HirDatabase) -> Vec<TypeParam> {
let generics = db.generic_params(self.into());
generics generics
.types .types
.iter() .iter()
@ -1175,6 +1193,13 @@ impl Local {
} }
} }
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum GenericParam {
TypeParam(TypeParam),
LifetimeParam(LifetimeParam),
}
impl_from!(TypeParam, LifetimeParam for GenericParam);
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct TypeParam { pub struct TypeParam {
pub(crate) id: TypeParamId, pub(crate) id: TypeParamId,
@ -1215,6 +1240,18 @@ impl TypeParam {
} }
} }
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct LifetimeParam {
pub(crate) id: LifetimeParamId,
}
impl LifetimeParam {
pub fn name(self, db: &dyn HirDatabase) -> Name {
let params = db.generic_params(self.id.parent);
params.lifetimes[self.id.local_id].name.clone()
}
}
// FIXME: rename from `ImplDef` to `Impl` // FIXME: rename from `ImplDef` to `Impl`
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ImplDef { pub struct ImplDef {

View file

@ -35,8 +35,8 @@ pub use crate::{
code_model::{ code_model::{
Access, Adt, AsAssocItem, AssocItem, AssocItemContainer, Callable, CallableKind, Const, Access, Adt, AsAssocItem, AssocItem, AssocItemContainer, Callable, CallableKind, Const,
Crate, CrateDependency, DefWithBody, Enum, EnumVariant, Field, FieldSource, Function, Crate, CrateDependency, DefWithBody, Enum, EnumVariant, Field, FieldSource, Function,
GenericDef, HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, ScopeDef, Static, GenericDef, HasVisibility, ImplDef, LifetimeParam, Local, MacroDef, Module, ModuleDef,
Struct, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, ScopeDef, Static, Struct, Trait, Type, TypeAlias, TypeParam, Union, VariantDef,
}, },
has_source::HasSource, has_source::HasSource,
semantics::{PathResolution, Semantics, SemanticsScope}, semantics::{PathResolution, Semantics, SemanticsScope},
@ -56,8 +56,9 @@ pub use hir_def::{
visibility::Visibility, visibility::Visibility,
}; };
pub use hir_expand::{ pub use hir_expand::{
name::known, name::AsName, name::Name, ExpandResult, HirFileId, InFile, MacroCallId, name::{known, AsName, Name},
MacroCallLoc, /* FIXME */ MacroDefId, MacroFile, Origin, ExpandResult, HirFileId, InFile, MacroCallId, MacroCallLoc, /* FIXME */ MacroDefId,
MacroFile, Origin,
}; };
pub use hir_ty::display::HirDisplay; pub use hir_ty::display::HirDisplay;

View file

@ -21,7 +21,7 @@ use crate::{
keys, keys,
src::HasChildSource, src::HasChildSource,
src::HasSource, src::HasSource,
type_ref::{TypeBound, TypeRef}, type_ref::{LifetimeRef, TypeBound, TypeRef},
AdtId, GenericDefId, LocalTypeParamId, Lookup, TypeParamId, AdtId, GenericDefId, LocalTypeParamId, Lookup, TypeParamId,
}; };
@ -33,6 +33,12 @@ pub struct TypeParamData {
pub provenance: TypeParamProvenance, pub provenance: TypeParamProvenance,
} }
/// Data about a generic parameter (to a function, struct, impl, ...).
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct LifetimeParamData {
pub name: Name,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)] #[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum TypeParamProvenance { pub enum TypeParamProvenance {
TypeParamList, TypeParamList,
@ -44,7 +50,7 @@ pub enum TypeParamProvenance {
#[derive(Clone, PartialEq, Eq, Debug, Default)] #[derive(Clone, PartialEq, Eq, Debug, Default)]
pub struct GenericParams { pub struct GenericParams {
pub types: Arena<TypeParamData>, pub types: Arena<TypeParamData>,
// lifetimes: Arena<LocalLifetimeParamId, LifetimeParamData>, pub lifetimes: Arena<LifetimeParamData>,
pub where_predicates: Vec<WherePredicate>, pub where_predicates: Vec<WherePredicate>,
} }
@ -53,16 +59,17 @@ pub struct GenericParams {
/// It might still result in multiple actual predicates though, because of /// It might still result in multiple actual predicates though, because of
/// associated type bindings like `Iterator<Item = u32>`. /// associated type bindings like `Iterator<Item = u32>`.
#[derive(Clone, PartialEq, Eq, Debug)] #[derive(Clone, PartialEq, Eq, Debug)]
pub struct WherePredicate { pub enum WherePredicate {
pub target: WherePredicateTarget, TypeBound { target: WherePredicateTypeTarget, bound: TypeBound },
pub bound: TypeBound, Lifetime { target: LifetimeRef, bound: LifetimeRef },
} }
#[derive(Clone, PartialEq, Eq, Debug)] #[derive(Clone, PartialEq, Eq, Debug)]
pub enum WherePredicateTarget { pub enum WherePredicateTypeTarget {
TypeRef(TypeRef), TypeRef(TypeRef),
/// For desugared where predicates that can directly refer to a type param. /// For desugared where predicates that can directly refer to a type param.
TypeParam(LocalTypeParamId), TypeParam(LocalTypeParamId),
// FIXME: ForLifetime(Vec<LifetimeParamId>, TypeRef)
} }
type SourceMap = ArenaMap<LocalTypeParamId, Either<ast::Trait, ast::TypeParam>>; type SourceMap = ArenaMap<LocalTypeParamId, Either<ast::Trait, ast::TypeParam>>;
@ -123,7 +130,7 @@ impl GenericParams {
} }
fn new(db: &dyn DefDatabase, def: GenericDefId) -> (GenericParams, InFile<SourceMap>) { fn new(db: &dyn DefDatabase, def: GenericDefId) -> (GenericParams, InFile<SourceMap>) {
let mut generics = GenericParams { types: Arena::default(), where_predicates: Vec::new() }; let mut generics = GenericParams::default();
let mut sm = ArenaMap::default(); let mut sm = ArenaMap::default();
// FIXME: add `: Sized` bound for everything except for `Self` in traits // FIXME: add `: Sized` bound for everything except for `Self` in traits
@ -171,7 +178,7 @@ impl GenericParams {
// add super traits as bounds on Self // add super traits as bounds on Self
// i.e., trait Foo: Bar is equivalent to trait Foo where Self: Bar // i.e., trait Foo: Bar is equivalent to trait Foo where Self: Bar
let self_param = TypeRef::Path(name![Self].into()); let self_param = TypeRef::Path(name![Self].into());
generics.fill_bounds(&lower_ctx, &src.value, self_param); generics.fill_bounds(&lower_ctx, &src.value, Either::Left(self_param));
generics.fill(&lower_ctx, &mut sm, &src.value); generics.fill(&lower_ctx, &mut sm, &src.value);
src.file_id src.file_id
@ -218,12 +225,12 @@ impl GenericParams {
&mut self, &mut self,
lower_ctx: &LowerCtx, lower_ctx: &LowerCtx,
node: &dyn ast::TypeBoundsOwner, node: &dyn ast::TypeBoundsOwner,
type_ref: TypeRef, target: Either<TypeRef, LifetimeRef>,
) { ) {
for bound in for bound in
node.type_bound_list().iter().flat_map(|type_bound_list| type_bound_list.bounds()) node.type_bound_list().iter().flat_map(|type_bound_list| type_bound_list.bounds())
{ {
self.add_where_predicate_from_bound(lower_ctx, bound, type_ref.clone()); self.add_where_predicate_from_bound(lower_ctx, bound, target.clone());
} }
} }
@ -246,19 +253,30 @@ impl GenericParams {
sm.insert(param_id, Either::Right(type_param.clone())); sm.insert(param_id, Either::Right(type_param.clone()));
let type_ref = TypeRef::Path(name.into()); let type_ref = TypeRef::Path(name.into());
self.fill_bounds(&lower_ctx, &type_param, type_ref); self.fill_bounds(&lower_ctx, &type_param, Either::Left(type_ref));
}
for lifetime_param in params.lifetime_params() {
let name = lifetime_param
.lifetime_token()
.map_or_else(Name::missing, |tok| Name::new_lifetime(&tok));
let param = LifetimeParamData { name: name.clone() };
let _param_id = self.lifetimes.alloc(param);
let lifetime_ref = LifetimeRef::new_name(name);
self.fill_bounds(&lower_ctx, &lifetime_param, Either::Right(lifetime_ref));
} }
} }
fn fill_where_predicates(&mut self, lower_ctx: &LowerCtx, where_clause: ast::WhereClause) { fn fill_where_predicates(&mut self, lower_ctx: &LowerCtx, where_clause: ast::WhereClause) {
for pred in where_clause.predicates() { for pred in where_clause.predicates() {
let type_ref = match pred.ty() { let target = if let Some(type_ref) = pred.ty() {
Some(type_ref) => type_ref, Either::Left(TypeRef::from_ast(lower_ctx, type_ref))
None => continue, } else if let Some(lifetime_tok) = pred.lifetime_token() {
Either::Right(LifetimeRef::from_token(lifetime_tok))
} else {
continue;
}; };
let type_ref = TypeRef::from_ast(lower_ctx, type_ref);
for bound in pred.type_bound_list().iter().flat_map(|l| l.bounds()) { for bound in pred.type_bound_list().iter().flat_map(|l| l.bounds()) {
self.add_where_predicate_from_bound(lower_ctx, bound, type_ref.clone()); self.add_where_predicate_from_bound(lower_ctx, bound, target.clone());
} }
} }
} }
@ -267,15 +285,24 @@ impl GenericParams {
&mut self, &mut self,
lower_ctx: &LowerCtx, lower_ctx: &LowerCtx,
bound: ast::TypeBound, bound: ast::TypeBound,
type_ref: TypeRef, target: Either<TypeRef, LifetimeRef>,
) { ) {
if bound.question_mark_token().is_some() { if bound.question_mark_token().is_some() {
// FIXME: remove this bound // FIXME: remove this bound
return; return;
} }
let bound = TypeBound::from_ast(lower_ctx, bound); let bound = TypeBound::from_ast(lower_ctx, bound);
self.where_predicates let predicate = match (target, bound) {
.push(WherePredicate { target: WherePredicateTarget::TypeRef(type_ref), bound }); (Either::Left(type_ref), bound) => WherePredicate::TypeBound {
target: WherePredicateTypeTarget::TypeRef(type_ref),
bound,
},
(Either::Right(lifetime), TypeBound::Lifetime(bound)) => {
WherePredicate::Lifetime { target: lifetime, bound }
}
_ => return,
};
self.where_predicates.push(predicate);
} }
pub(crate) fn fill_implicit_impl_trait_args(&mut self, type_ref: &TypeRef) { pub(crate) fn fill_implicit_impl_trait_args(&mut self, type_ref: &TypeRef) {
@ -288,8 +315,8 @@ impl GenericParams {
}; };
let param_id = self.types.alloc(param); let param_id = self.types.alloc(param);
for bound in bounds { for bound in bounds {
self.where_predicates.push(WherePredicate { self.where_predicates.push(WherePredicate::TypeBound {
target: WherePredicateTarget::TypeParam(param_id), target: WherePredicateTypeTarget::TypeParam(param_id),
bound: bound.clone(), bound: bound.clone(),
}); });
} }

View file

@ -255,7 +255,7 @@ impl GenericParamsStorage {
} }
static EMPTY_GENERICS: GenericParams = static EMPTY_GENERICS: GenericParams =
GenericParams { types: Arena::new(), where_predicates: Vec::new() }; GenericParams { types: Arena::new(), lifetimes: Arena::new(), where_predicates: Vec::new() };
#[derive(Default, Debug, Eq, PartialEq)] #[derive(Default, Debug, Eq, PartialEq)]
struct ItemTreeData { struct ItemTreeData {

View file

@ -13,6 +13,7 @@ use syntax::{
use crate::{ use crate::{
attr::Attrs, attr::Attrs,
generics::{GenericParams, TypeParamData, TypeParamProvenance}, generics::{GenericParams, TypeParamData, TypeParamProvenance},
type_ref::LifetimeRef,
}; };
use super::*; use super::*;
@ -292,12 +293,16 @@ impl Ctx {
let self_type = TypeRef::Path(name![Self].into()); let self_type = TypeRef::Path(name![Self].into());
match self_param.kind() { match self_param.kind() {
ast::SelfParamKind::Owned => self_type, ast::SelfParamKind::Owned => self_type,
ast::SelfParamKind::Ref => { ast::SelfParamKind::Ref => TypeRef::Reference(
TypeRef::Reference(Box::new(self_type), Mutability::Shared) Box::new(self_type),
} self_param.lifetime_token().map(LifetimeRef::from_token),
ast::SelfParamKind::MutRef => { Mutability::Shared,
TypeRef::Reference(Box::new(self_type), Mutability::Mut) ),
} ast::SelfParamKind::MutRef => TypeRef::Reference(
Box::new(self_type),
self_param.lifetime_token().map(LifetimeRef::from_token),
Mutability::Mut,
),
} }
} }
}; };
@ -629,8 +634,7 @@ impl Ctx {
// add super traits as bounds on Self // add super traits as bounds on Self
// i.e., trait Foo: Bar is equivalent to trait Foo where Self: Bar // i.e., trait Foo: Bar is equivalent to trait Foo where Self: Bar
let self_param = TypeRef::Path(name![Self].into()); let self_param = TypeRef::Path(name![Self].into());
generics.fill_bounds(&self.body_ctx, trait_def, self_param); generics.fill_bounds(&self.body_ctx, trait_def, Either::Left(self_param));
generics.fill(&self.body_ctx, &mut sm, node); generics.fill(&self.body_ctx, &mut sm, node);
} }
GenericsOwner::Impl => { GenericsOwner::Impl => {

View file

@ -223,6 +223,13 @@ pub struct TypeParamId {
pub type LocalTypeParamId = Idx<generics::TypeParamData>; pub type LocalTypeParamId = Idx<generics::TypeParamData>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct LifetimeParamId {
pub parent: GenericDefId,
pub local_id: LocalLifetimeParamId,
}
pub type LocalLifetimeParamId = Idx<generics::LifetimeParamData>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ContainerId { pub enum ContainerId {
ModuleId(ModuleId), ModuleId(ModuleId),

View file

@ -7,7 +7,7 @@ use std::{
sync::Arc, sync::Arc,
}; };
use crate::body::LowerCtx; use crate::{body::LowerCtx, type_ref::LifetimeRef};
use base_db::CrateId; use base_db::CrateId;
use hir_expand::{ use hir_expand::{
hygiene::Hygiene, hygiene::Hygiene,
@ -145,7 +145,7 @@ pub struct AssociatedTypeBinding {
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum GenericArg { pub enum GenericArg {
Type(TypeRef), Type(TypeRef),
// or lifetime... Lifetime(LifetimeRef),
} }
impl Path { impl Path {

View file

@ -15,7 +15,7 @@ use super::AssociatedTypeBinding;
use crate::{ use crate::{
body::LowerCtx, body::LowerCtx,
path::{GenericArg, GenericArgs, ModPath, Path, PathKind}, path::{GenericArg, GenericArgs, ModPath, Path, PathKind},
type_ref::{TypeBound, TypeRef}, type_ref::{LifetimeRef, TypeBound, TypeRef},
}; };
pub(super) use lower_use::lower_use_tree; pub(super) use lower_use::lower_use_tree;
@ -170,8 +170,14 @@ pub(super) fn lower_generic_args(
bindings.push(AssociatedTypeBinding { name, type_ref, bounds }); bindings.push(AssociatedTypeBinding { name, type_ref, bounds });
} }
} }
// Lifetimes and constants are ignored for now. ast::GenericArg::LifetimeArg(lifetime_arg) => {
ast::GenericArg::LifetimeArg(_) | ast::GenericArg::ConstArg(_) => (), if let Some(lifetime) = lifetime_arg.lifetime_token() {
let lifetime_ref = LifetimeRef::from_token(lifetime);
args.push(GenericArg::Lifetime(lifetime_ref))
}
}
// constants are ignored for now.
ast::GenericArg::ConstArg(_) => (),
} }
} }

View file

@ -1,6 +1,7 @@
//! HIR for references to types. Paths in these are not yet resolved. They can //! HIR for references to types. Paths in these are not yet resolved. They can
//! be directly created from an ast::TypeRef, without further queries. //! be directly created from an ast::TypeRef, without further queries.
use syntax::ast::{self}; use hir_expand::name::Name;
use syntax::{ast, SyntaxToken};
use crate::{body::LowerCtx, path::Path}; use crate::{body::LowerCtx, path::Path};
@ -58,7 +59,7 @@ pub enum TypeRef {
Tuple(Vec<TypeRef>), Tuple(Vec<TypeRef>),
Path(Path), Path(Path),
RawPtr(Box<TypeRef>, Mutability), RawPtr(Box<TypeRef>, Mutability),
Reference(Box<TypeRef>, Mutability), Reference(Box<TypeRef>, Option<LifetimeRef>, Mutability),
Array(Box<TypeRef> /*, Expr*/), Array(Box<TypeRef> /*, Expr*/),
Slice(Box<TypeRef>), Slice(Box<TypeRef>),
/// A fn pointer. Last element of the vector is the return type. /// A fn pointer. Last element of the vector is the return type.
@ -69,11 +70,30 @@ pub enum TypeRef {
Error, Error,
} }
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct LifetimeRef {
pub name: Name,
}
impl LifetimeRef {
pub(crate) fn new_name(name: Name) -> Self {
LifetimeRef { name }
}
pub(crate) fn from_token(token: SyntaxToken) -> Self {
LifetimeRef { name: Name::new_lifetime(&token) }
}
pub fn missing() -> LifetimeRef {
LifetimeRef { name: Name::missing() }
}
}
#[derive(Clone, PartialEq, Eq, Hash, Debug)] #[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub enum TypeBound { pub enum TypeBound {
Path(Path), Path(Path),
// also for<> bounds // ForLifetime(Vec<LifetimeRef>, Path), FIXME ForLifetime
// also Lifetimes Lifetime(LifetimeRef),
Error, Error,
} }
@ -107,8 +127,9 @@ impl TypeRef {
} }
ast::Type::RefType(inner) => { ast::Type::RefType(inner) => {
let inner_ty = TypeRef::from_ast_opt(&ctx, inner.ty()); let inner_ty = TypeRef::from_ast_opt(&ctx, inner.ty());
let lifetime = inner.lifetime_token().map(|t| LifetimeRef::from_token(t));
let mutability = Mutability::from_mutable(inner.mut_token().is_some()); let mutability = Mutability::from_mutable(inner.mut_token().is_some());
TypeRef::Reference(Box::new(inner_ty), mutability) TypeRef::Reference(Box::new(inner_ty), lifetime, mutability)
} }
ast::Type::InferType(_inner) => TypeRef::Placeholder, ast::Type::InferType(_inner) => TypeRef::Placeholder,
ast::Type::FnPtrType(inner) => { ast::Type::FnPtrType(inner) => {
@ -163,14 +184,14 @@ impl TypeRef {
types.iter().for_each(|t| go(t, f)) types.iter().for_each(|t| go(t, f))
} }
TypeRef::RawPtr(type_ref, _) TypeRef::RawPtr(type_ref, _)
| TypeRef::Reference(type_ref, _) | TypeRef::Reference(type_ref, ..)
| TypeRef::Array(type_ref) | TypeRef::Array(type_ref)
| TypeRef::Slice(type_ref) => go(&type_ref, f), | TypeRef::Slice(type_ref) => go(&type_ref, f),
TypeRef::ImplTrait(bounds) | TypeRef::DynTrait(bounds) => { TypeRef::ImplTrait(bounds) | TypeRef::DynTrait(bounds) => {
for bound in bounds { for bound in bounds {
match bound { match bound {
TypeBound::Path(path) => go_path(path, f), TypeBound::Path(path) => go_path(path, f),
TypeBound::Error => (), TypeBound::Lifetime(_) | TypeBound::Error => (),
} }
} }
} }
@ -186,8 +207,12 @@ impl TypeRef {
for segment in path.segments().iter() { for segment in path.segments().iter() {
if let Some(args_and_bindings) = segment.args_and_bindings { if let Some(args_and_bindings) = segment.args_and_bindings {
for arg in &args_and_bindings.args { for arg in &args_and_bindings.args {
let crate::path::GenericArg::Type(type_ref) = arg; match arg {
go(type_ref, f); crate::path::GenericArg::Type(type_ref) => {
go(type_ref, f);
}
crate::path::GenericArg::Lifetime(_) => {}
}
} }
for binding in &args_and_bindings.bindings { for binding in &args_and_bindings.bindings {
if let Some(type_ref) = &binding.type_ref { if let Some(type_ref) = &binding.type_ref {
@ -196,7 +221,7 @@ impl TypeRef {
for bound in &binding.bounds { for bound in &binding.bounds {
match bound { match bound {
TypeBound::Path(path) => go_path(path, f), TypeBound::Path(path) => go_path(path, f),
TypeBound::Error => (), TypeBound::Lifetime(_) | TypeBound::Error => (),
} }
} }
} }
@ -232,7 +257,10 @@ impl TypeBound {
}; };
TypeBound::Path(path) TypeBound::Path(path)
} }
ast::TypeBoundKind::ForType(_) | ast::TypeBoundKind::Lifetime(_) => TypeBound::Error, ast::TypeBoundKind::ForType(_) => TypeBound::Error, // FIXME ForType
ast::TypeBoundKind::Lifetime(lifetime) => {
TypeBound::Lifetime(LifetimeRef::from_token(lifetime))
}
} }
} }

View file

@ -38,7 +38,7 @@ impl Name {
} }
pub fn new_lifetime(lt: &syntax::SyntaxToken) -> Name { pub fn new_lifetime(lt: &syntax::SyntaxToken) -> Name {
assert!(lt.kind() == syntax::SyntaxKind::LIFETIME); assert_eq!(lt.kind(), syntax::SyntaxKind::LIFETIME);
Name(Repr::Text(lt.text().clone())) Name(Repr::Text(lt.text().clone()))
} }
@ -250,6 +250,8 @@ pub mod known {
pub const SELF_PARAM: super::Name = super::Name::new_inline("self"); pub const SELF_PARAM: super::Name = super::Name::new_inline("self");
pub const SELF_TYPE: super::Name = super::Name::new_inline("Self"); pub const SELF_TYPE: super::Name = super::Name::new_inline("Self");
pub const STATIC_LIFETIME: super::Name = super::Name::new_inline("'static");
#[macro_export] #[macro_export]
macro_rules! name { macro_rules! name {
(self) => { (self) => {
@ -258,6 +260,9 @@ pub mod known {
(Self) => { (Self) => {
$crate::name::known::SELF_TYPE $crate::name::known::SELF_TYPE
}; };
('static) => {
$crate::name::known::STATIC_LIFETIME
};
($ident:ident) => { ($ident:ident) => {
$crate::name::known::$ident $crate::name::known::$ident
}; };

View file

@ -4,7 +4,7 @@ use std::fmt;
use crate::{ use crate::{
db::HirDatabase, utils::generics, ApplicationTy, CallableDefId, FnSig, GenericPredicate, db::HirDatabase, utils::generics, ApplicationTy, CallableDefId, FnSig, GenericPredicate,
Obligation, OpaqueTyId, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, Lifetime, Obligation, OpaqueTyId, ProjectionTy, Substs, TraitRef, Ty, TypeCtor,
}; };
use hir_def::{ use hir_def::{
find_path, generics::TypeParamProvenance, item_scope::ItemInNs, AdtId, AssocContainerId, find_path, generics::TypeParamProvenance, item_scope::ItemInNs, AdtId, AssocContainerId,
@ -710,6 +710,19 @@ impl HirDisplay for GenericPredicate {
} }
} }
impl HirDisplay for Lifetime {
fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
match self {
Lifetime::Parameter(id) => {
let generics = generics(f.db.upcast(), id.parent);
let param_data = &generics.params.lifetimes[id.local_id];
write!(f, "{}", &param_data.name)
}
Lifetime::Static => write!(f, "'static"),
}
}
}
impl HirDisplay for Obligation { impl HirDisplay for Obligation {
fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
match self { match self {

View file

@ -848,6 +848,7 @@ impl<'a> InferenceContext<'a> {
let ty = self.make_ty(type_ref); let ty = self.make_ty(type_ref);
substs.push(ty); substs.push(ty);
} }
GenericArg::Lifetime(_) => {}
} }
} }
}; };

View file

@ -29,8 +29,8 @@ use base_db::{salsa, CrateId};
use hir_def::{ use hir_def::{
expr::ExprId, expr::ExprId,
type_ref::{Mutability, Rawness}, type_ref::{Mutability, Rawness},
AdtId, AssocContainerId, DefWithBodyId, GenericDefId, HasModule, Lookup, TraitId, TypeAliasId, AdtId, AssocContainerId, DefWithBodyId, GenericDefId, HasModule, LifetimeParamId, Lookup,
TypeParamId, TraitId, TypeAliasId, TypeParamId,
}; };
use itertools::Itertools; use itertools::Itertools;
@ -52,6 +52,12 @@ pub use traits::{InEnvironment, Obligation, ProjectionPredicate, TraitEnvironmen
pub use chalk_ir::{BoundVar, DebruijnIndex}; pub use chalk_ir::{BoundVar, DebruijnIndex};
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub enum Lifetime {
Parameter(LifetimeParamId),
Static,
}
/// A type constructor or type name: this might be something like the primitive /// A type constructor or type name: this might be something like the primitive
/// type `bool`, a struct like `Vec`, or things like function pointers or /// type `bool`, a struct like `Vec`, or things like function pointers or
/// tuples. /// tuples.

View file

@ -12,7 +12,7 @@ use base_db::CrateId;
use hir_def::{ use hir_def::{
adt::StructKind, adt::StructKind,
builtin_type::BuiltinType, builtin_type::BuiltinType,
generics::{TypeParamProvenance, WherePredicate, WherePredicateTarget}, generics::{TypeParamProvenance, WherePredicate, WherePredicateTypeTarget},
path::{GenericArg, Path, PathSegment, PathSegments}, path::{GenericArg, Path, PathSegment, PathSegments},
resolver::{HasResolver, Resolver, TypeNs}, resolver::{HasResolver, Resolver, TypeNs},
type_ref::{TypeBound, TypeRef}, type_ref::{TypeBound, TypeRef},
@ -171,7 +171,7 @@ impl Ty {
let inner_ty = Ty::from_hir(ctx, inner); let inner_ty = Ty::from_hir(ctx, inner);
Ty::apply_one(TypeCtor::Slice, inner_ty) Ty::apply_one(TypeCtor::Slice, inner_ty)
} }
TypeRef::Reference(inner, mutability) => { TypeRef::Reference(inner, _, mutability) => {
let inner_ty = Ty::from_hir(ctx, inner); let inner_ty = Ty::from_hir(ctx, inner);
Ty::apply_one(TypeCtor::Ref(*mutability), inner_ty) Ty::apply_one(TypeCtor::Ref(*mutability), inner_ty)
} }
@ -555,7 +555,7 @@ fn substs_from_path_segment(
substs.extend(iter::repeat(Ty::Unknown).take(parent_params)); substs.extend(iter::repeat(Ty::Unknown).take(parent_params));
let mut had_explicit_args = false; let mut had_explicit_type_args = false;
if let Some(generic_args) = &segment.args_and_bindings { if let Some(generic_args) = &segment.args_and_bindings {
if !generic_args.has_self_type { if !generic_args.has_self_type {
@ -568,10 +568,11 @@ fn substs_from_path_segment(
for arg in generic_args.args.iter().skip(skip).take(expected_num) { for arg in generic_args.args.iter().skip(skip).take(expected_num) {
match arg { match arg {
GenericArg::Type(type_ref) => { GenericArg::Type(type_ref) => {
had_explicit_args = true; had_explicit_type_args = true;
let ty = Ty::from_hir(ctx, type_ref); let ty = Ty::from_hir(ctx, type_ref);
substs.push(ty); substs.push(ty);
} }
GenericArg::Lifetime(_) => {}
} }
} }
} }
@ -579,7 +580,7 @@ fn substs_from_path_segment(
// handle defaults. In expression or pattern path segments without // handle defaults. In expression or pattern path segments without
// explicitly specified type arguments, missing type arguments are inferred // explicitly specified type arguments, missing type arguments are inferred
// (i.e. defaults aren't used). // (i.e. defaults aren't used).
if !infer_args || had_explicit_args { if !infer_args || had_explicit_type_args {
if let Some(def_generic) = def_generic { if let Some(def_generic) = def_generic {
let defaults = ctx.db.generic_defaults(def_generic); let defaults = ctx.db.generic_defaults(def_generic);
assert_eq!(total_len, defaults.len()); assert_eq!(total_len, defaults.len());
@ -657,7 +658,7 @@ impl TraitRef {
) -> Option<TraitRef> { ) -> Option<TraitRef> {
match bound { match bound {
TypeBound::Path(path) => TraitRef::from_path(ctx, path, Some(self_ty)), TypeBound::Path(path) => TraitRef::from_path(ctx, path, Some(self_ty)),
TypeBound::Error => None, TypeBound::Lifetime(_) | TypeBound::Error => None,
} }
} }
} }
@ -667,22 +668,30 @@ impl GenericPredicate {
ctx: &'a TyLoweringContext<'a>, ctx: &'a TyLoweringContext<'a>,
where_predicate: &'a WherePredicate, where_predicate: &'a WherePredicate,
) -> impl Iterator<Item = GenericPredicate> + 'a { ) -> impl Iterator<Item = GenericPredicate> + 'a {
let self_ty = match &where_predicate.target { match where_predicate {
WherePredicateTarget::TypeRef(type_ref) => Ty::from_hir(ctx, type_ref), WherePredicate::TypeBound { target, bound } => {
WherePredicateTarget::TypeParam(param_id) => { let self_ty = match target {
let generic_def = ctx.resolver.generic_def().expect("generics in scope"); WherePredicateTypeTarget::TypeRef(type_ref) => Ty::from_hir(ctx, type_ref),
let generics = generics(ctx.db.upcast(), generic_def); WherePredicateTypeTarget::TypeParam(param_id) => {
let param_id = hir_def::TypeParamId { parent: generic_def, local_id: *param_id }; let generic_def = ctx.resolver.generic_def().expect("generics in scope");
match ctx.type_param_mode { let generics = generics(ctx.db.upcast(), generic_def);
TypeParamLoweringMode::Placeholder => Ty::Placeholder(param_id), let param_id =
TypeParamLoweringMode::Variable => { hir_def::TypeParamId { parent: generic_def, local_id: *param_id };
let idx = generics.param_idx(param_id).expect("matching generics"); match ctx.type_param_mode {
Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, idx)) TypeParamLoweringMode::Placeholder => Ty::Placeholder(param_id),
TypeParamLoweringMode::Variable => {
let idx = generics.param_idx(param_id).expect("matching generics");
Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, idx))
}
}
} }
} };
GenericPredicate::from_type_bound(ctx, bound, self_ty)
.collect::<Vec<_>>()
.into_iter()
} }
}; WherePredicate::Lifetime { .. } => vec![].into_iter(),
GenericPredicate::from_type_bound(ctx, &where_predicate.bound, self_ty) }
} }
pub(crate) fn from_type_bound<'a>( pub(crate) fn from_type_bound<'a>(
@ -707,7 +716,7 @@ fn assoc_type_bindings_from_type_bound<'a>(
) -> impl Iterator<Item = GenericPredicate> + 'a { ) -> impl Iterator<Item = GenericPredicate> + 'a {
let last_segment = match bound { let last_segment = match bound {
TypeBound::Path(path) => path.segments().last(), TypeBound::Path(path) => path.segments().last(),
TypeBound::Error => None, TypeBound::Error | TypeBound::Lifetime(_) => None,
}; };
last_segment last_segment
.into_iter() .into_iter()
@ -872,11 +881,16 @@ pub(crate) fn generic_predicates_for_param_query(
resolver resolver
.where_predicates_in_scope() .where_predicates_in_scope()
// we have to filter out all other predicates *first*, before attempting to lower them // we have to filter out all other predicates *first*, before attempting to lower them
.filter(|pred| match &pred.target { .filter(|pred| match pred {
WherePredicateTarget::TypeRef(type_ref) => { WherePredicate::TypeBound {
Ty::from_hir_only_param(&ctx, type_ref) == Some(param_id) target: WherePredicateTypeTarget::TypeRef(type_ref),
} ..
WherePredicateTarget::TypeParam(local_id) => *local_id == param_id.local_id, } => Ty::from_hir_only_param(&ctx, type_ref) == Some(param_id),
WherePredicate::TypeBound {
target: WherePredicateTypeTarget::TypeParam(local_id),
..
} => *local_id == param_id.local_id,
WherePredicate::Lifetime { .. } => false,
}) })
.flat_map(|pred| { .flat_map(|pred| {
GenericPredicate::from_where_predicate(&ctx, pred) GenericPredicate::from_where_predicate(&ctx, pred)

View file

@ -2,11 +2,10 @@
//! query, but can't be computed directly from `*Data` (ie, which need a `db`). //! query, but can't be computed directly from `*Data` (ie, which need a `db`).
use std::sync::Arc; use std::sync::Arc;
use hir_def::generics::WherePredicateTarget;
use hir_def::{ use hir_def::{
adt::VariantData, adt::VariantData,
db::DefDatabase, db::DefDatabase,
generics::{GenericParams, TypeParamData, TypeParamProvenance}, generics::{GenericParams, TypeParamData, TypeParamProvenance, WherePredicateTypeTarget},
path::Path, path::Path,
resolver::{HasResolver, TypeNs}, resolver::{HasResolver, TypeNs},
type_ref::TypeRef, type_ref::TypeRef,
@ -27,14 +26,19 @@ fn direct_super_traits(db: &dyn DefDatabase, trait_: TraitId) -> Vec<TraitId> {
generic_params generic_params
.where_predicates .where_predicates
.iter() .iter()
.filter_map(|pred| match &pred.target { .filter_map(|pred| match pred {
WherePredicateTarget::TypeRef(TypeRef::Path(p)) if p == &Path::from(name![Self]) => { hir_def::generics::WherePredicate::TypeBound { target, bound } => match target {
pred.bound.as_path() WherePredicateTypeTarget::TypeRef(TypeRef::Path(p))
} if p == &Path::from(name![Self]) =>
WherePredicateTarget::TypeParam(local_id) if Some(*local_id) == trait_self => { {
pred.bound.as_path() bound.as_path()
} }
_ => None, WherePredicateTypeTarget::TypeParam(local_id) if Some(*local_id) == trait_self => {
bound.as_path()
}
_ => None,
},
hir_def::generics::WherePredicate::Lifetime { .. } => None,
}) })
.filter_map(|path| match resolver.resolve_path_in_type_ns_fully(db, path.mod_path()) { .filter_map(|path| match resolver.resolve_path_in_type_ns_fully(db, path.mod_path()) {
Some(TypeNs::TraitId(t)) => Some(t), Some(TypeNs::TraitId(t)) => Some(t),