Make the Resolution variants tuple variants

This commit is contained in:
Florian Diebold 2019-01-30 22:41:44 +01:00
parent d3df80dfe4
commit d571d26955
7 changed files with 41 additions and 53 deletions

View File

@ -12,6 +12,7 @@
/// Data about a generic parameter (to a function, struct, impl, ...).
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct GenericParam {
// TODO: give generic params proper IDs
pub(crate) idx: u32,
pub(crate) name: Name,
}

View File

@ -90,9 +90,8 @@ pub fn target_ty(&self, db: &impl HirDatabase) -> Ty {
pub fn target_trait(&self, db: &impl HirDatabase) -> Option<Trait> {
if let Some(TypeRef::Path(path)) = self.target_trait_ref() {
let resolver = self.resolver(db);
if let Some(Resolution::Def {
def: ModuleDef::Trait(tr),
}) = resolver.resolve_path(db, path).take_types()
if let Some(Resolution::Def(ModuleDef::Trait(tr))) =
resolver.resolve_path(db, path).take_types()
{
return Some(tr);
}
@ -106,7 +105,7 @@ pub fn items(&self) -> &[ImplItem] {
pub fn resolver(&self, db: &impl HirDatabase) -> Resolver {
let r = self.module().resolver(db);
// FIXME: add generics
// TODO: add generics
let r = r.push_impl_block_scope(self.clone());
r
}

View File

@ -48,17 +48,11 @@ pub(crate) enum Scope {
pub enum Resolution {
// FIXME make these tuple variants
/// An item
Def {
def: ModuleDef,
},
Def(ModuleDef),
/// A local binding (only value namespace)
LocalBinding {
pat: PatId,
},
LocalBinding(PatId),
/// A generic parameter
GenericParam {
idx: u32,
},
GenericParam(u32),
SelfType(ImplBlock),
}
@ -85,7 +79,7 @@ pub fn resolve_path(&self, db: &impl HirDatabase, path: &Path) -> PerNs<Resoluti
_ => return PerNs::none(),
};
let module_res = item_map.resolve_path(db, module, path);
module_res.map(|def| Resolution::Def { def })
module_res.map(|def| Resolution::Def(def))
}
}
@ -157,18 +151,16 @@ fn resolve_name(&self, name: &Name) -> PerNs<Resolution> {
match self {
Scope::ModuleScope(m) => {
if let Some(KnownName::SelfParam) = name.as_known_name() {
PerNs::types(Resolution::Def {
def: m.module.into(),
})
PerNs::types(Resolution::Def(m.module.into()))
} else {
match m.item_map[m.module.module_id].get(name) {
Some(res) => res.def.map(|def| Resolution::Def { def }),
Some(res) => res.def.map(Resolution::Def),
None => PerNs::none(),
}
}
}
Scope::GenericParams(gp) => match gp.find_by_name(name) {
Some(gp) => PerNs::types(Resolution::GenericParam { idx: gp.idx }),
Some(gp) => PerNs::types(Resolution::GenericParam(gp.idx)),
None => PerNs::none(),
},
Scope::ImplBlockScope(i) => {
@ -185,7 +177,7 @@ fn resolve_name(&self, name: &Name) -> PerNs<Resolution> {
.iter()
.find(|entry| entry.name() == name);
match entry {
Some(e) => PerNs::values(Resolution::LocalBinding { pat: e.pat() }),
Some(e) => PerNs::values(Resolution::LocalBinding(e.pat())),
None => PerNs::none(),
}
}
@ -205,14 +197,14 @@ fn collect_names(&self, f: &mut FnMut(Name, PerNs<Resolution>)) {
m.item_map[m.module.module_id]
.entries()
.for_each(|(name, res)| {
f(name.clone(), res.def.map(|def| Resolution::Def { def }));
f(name.clone(), res.def.map(Resolution::Def));
})
}
Scope::GenericParams(gp) => {
for param in &gp.params {
f(
param.name.clone(),
PerNs::types(Resolution::GenericParam { idx: param.idx }),
PerNs::types(Resolution::GenericParam(param.idx)),
)
}
}
@ -226,7 +218,7 @@ fn collect_names(&self, f: &mut FnMut(Name, PerNs<Resolution>)) {
e.expr_scopes.entries(e.scope_id).iter().for_each(|e| {
f(
e.name().clone(),
PerNs::values(Resolution::LocalBinding { pat: e.pat() }),
PerNs::values(Resolution::LocalBinding(e.pat())),
);
});
}

View File

@ -368,12 +368,12 @@ pub(crate) fn from_hir_path(db: &impl HirDatabase, resolver: &Resolver, path: &P
let resolution = resolver.resolve_path(db, path).take_types();
let def = match resolution {
Some(Resolution::Def { def, .. }) => def,
Some(Resolution::LocalBinding { .. }) => {
Some(Resolution::Def(def)) => def,
Some(Resolution::LocalBinding(..)) => {
// this should never happen
panic!("path resolved to local binding in type ns");
}
Some(Resolution::GenericParam { idx }) => {
Some(Resolution::GenericParam(idx)) => {
return Ty::Param {
idx,
// TODO: maybe return name in resolution?
@ -1107,7 +1107,7 @@ fn resolve_ty_completely(&mut self, tv_stack: &mut Vec<TypeVarId>, ty: Ty) -> Ty
fn infer_path_expr(&mut self, resolver: &Resolver, path: &Path) -> Option<Ty> {
let resolved = resolver.resolve_path(self.db, &path).take_values()?;
match resolved {
Resolution::Def { def, .. } => {
Resolution::Def(def) => {
let typable: Option<TypableDef> = def.into();
let typable = typable?;
let substs = Ty::substs_from_path(self.db, &self.resolver, path, typable);
@ -1115,12 +1115,12 @@ fn infer_path_expr(&mut self, resolver: &Resolver, path: &Path) -> Option<Ty> {
let ty = self.insert_type_vars(ty);
Some(ty)
}
Resolution::LocalBinding { pat } => {
Resolution::LocalBinding(pat) => {
let ty = self.type_of_pat.get(pat)?;
let ty = self.resolve_ty_as_possible(&mut vec![], ty.clone());
Some(ty)
}
Resolution::GenericParam { .. } => {
Resolution::GenericParam(..) => {
// generic params can't refer to values... yet
None
}
@ -1138,13 +1138,13 @@ fn resolve_variant(&mut self, path: Option<&Path>) -> (Ty, Option<VariantDef>) {
};
let resolver = &self.resolver;
let typable: Option<TypableDef> = match resolver.resolve_path(self.db, &path).take_types() {
Some(Resolution::Def { def, .. }) => def.into(),
Some(Resolution::LocalBinding { .. }) => {
Some(Resolution::Def(def)) => def.into(),
Some(Resolution::LocalBinding(..)) => {
// this cannot happen
log::error!("path resolved to local binding in type ns");
return (Ty::Unknown, None);
}
Some(Resolution::GenericParam { .. }) => {
Some(Resolution::GenericParam(..)) => {
// generic params can't be used in struct literals
return (Ty::Unknown, None);
}

View File

@ -12,7 +12,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
_ => return,
};
let def = match ctx.resolver.resolve_path(ctx.db, &path).take_types() {
Some(Resolution::Def { def }) => def,
Some(Resolution::Def(def)) => def,
_ => return,
};
match def {
@ -24,7 +24,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
ctx.source_range(),
name.to_string(),
)
.from_resolution(ctx, &res.def.map(|def| hir::Resolution::Def { def }))
.from_resolution(ctx, &res.def.map(hir::Resolution::Def))
.add_to(acc);
}
}

View File

@ -223,22 +223,18 @@ pub(super) fn from_resolution(
Some(it) => it,
};
let (kind, docs) = match def {
Resolution::Def { def: Module(it) } => (CompletionItemKind::Module, it.docs(ctx.db)),
Resolution::Def {
def: Function(func),
} => return self.from_function(ctx, *func),
Resolution::Def { def: Struct(it) } => (CompletionItemKind::Struct, it.docs(ctx.db)),
Resolution::Def { def: Enum(it) } => (CompletionItemKind::Enum, it.docs(ctx.db)),
Resolution::Def {
def: EnumVariant(it),
} => (CompletionItemKind::EnumVariant, it.docs(ctx.db)),
Resolution::Def { def: Const(it) } => (CompletionItemKind::Const, it.docs(ctx.db)),
Resolution::Def { def: Static(it) } => (CompletionItemKind::Static, it.docs(ctx.db)),
Resolution::Def { def: Trait(it) } => (CompletionItemKind::Trait, it.docs(ctx.db)),
Resolution::Def { def: Type(it) } => (CompletionItemKind::TypeAlias, it.docs(ctx.db)),
Resolution::GenericParam { .. } => (CompletionItemKind::TypeParam, None),
Resolution::LocalBinding { .. } => (CompletionItemKind::Binding, None),
Resolution::SelfType { .. } => (
Resolution::Def(Module(it)) => (CompletionItemKind::Module, it.docs(ctx.db)),
Resolution::Def(Function(func)) => return self.from_function(ctx, *func),
Resolution::Def(Struct(it)) => (CompletionItemKind::Struct, it.docs(ctx.db)),
Resolution::Def(Enum(it)) => (CompletionItemKind::Enum, it.docs(ctx.db)),
Resolution::Def(EnumVariant(it)) => (CompletionItemKind::EnumVariant, it.docs(ctx.db)),
Resolution::Def(Const(it)) => (CompletionItemKind::Const, it.docs(ctx.db)),
Resolution::Def(Static(it)) => (CompletionItemKind::Static, it.docs(ctx.db)),
Resolution::Def(Trait(it)) => (CompletionItemKind::Trait, it.docs(ctx.db)),
Resolution::Def(Type(it)) => (CompletionItemKind::TypeAlias, it.docs(ctx.db)),
Resolution::GenericParam(..) => (CompletionItemKind::TypeParam, None),
Resolution::LocalBinding(..) => (CompletionItemKind::Binding, None),
Resolution::SelfType(..) => (
CompletionItemKind::TypeParam, // (does this need its own kind?)
None,
),

View File

@ -90,8 +90,8 @@ pub(crate) fn reference_definition(
{
let resolved = resolver.resolve_path(db, &path);
match resolved.clone().take_types().or(resolved.take_values()) {
Some(Resolution::Def { def }) => return Exact(NavigationTarget::from_def(db, def)),
Some(Resolution::LocalBinding { pat }) => {
Some(Resolution::Def(def)) => return Exact(NavigationTarget::from_def(db, def)),
Some(Resolution::LocalBinding(pat)) => {
let body = resolver.body().expect("no body for local binding");
let syntax_mapping = body.syntax_mapping(db);
let ptr = syntax_mapping
@ -104,7 +104,7 @@ pub(crate) fn reference_definition(
let nav = NavigationTarget::from_scope_entry(file_id, name, ptr);
return Exact(nav);
}
Some(Resolution::GenericParam { .. }) => {
Some(Resolution::GenericParam(..)) => {
// TODO go to the generic param def
}
Some(Resolution::SelfType(_impl_block)) => {