make PerNs non-generic

This commit is contained in:
Aleksey Kladov 2019-09-13 16:38:59 +03:00
parent 51e2d76b98
commit 2fbe79ed9a
4 changed files with 30 additions and 34 deletions

View File

@ -200,7 +200,7 @@ fn get_legacy_macro(&self, name: &Name) -> Option<MacroDef> {
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Resolution {
/// None for unresolved
pub def: PerNs<ModuleDef>,
pub def: PerNs,
/// ident by which this is imported into local scope.
pub import: Option<ImportId>,
}
@ -213,7 +213,7 @@ pub(crate) fn from_macro(macro_: MacroDef) -> Self {
#[derive(Debug, Clone)]
struct ResolvePathResult {
resolved_def: PerNs<ModuleDef>,
resolved_def: PerNs,
segment_index: Option<usize>,
reached_fixedpoint: ReachedFixedPoint,
}
@ -224,7 +224,7 @@ fn empty(reached_fixedpoint: ReachedFixedPoint) -> ResolvePathResult {
}
fn with(
resolved_def: PerNs<ModuleDef>,
resolved_def: PerNs,
reached_fixedpoint: ReachedFixedPoint,
segment_index: Option<usize>,
) -> ResolvePathResult {
@ -316,7 +316,7 @@ pub(crate) fn resolve_path(
db: &impl DefDatabase,
original_module: CrateModuleId,
path: &Path,
) -> (PerNs<ModuleDef>, Option<usize>) {
) -> (PerNs, Option<usize>) {
let res = self.resolve_path_fp_with_macro(db, ResolveMode::Other, original_module, path);
(res.resolved_def, res.segment_index)
}
@ -331,7 +331,7 @@ fn resolve_path_fp_with_macro(
path: &Path,
) -> ResolvePathResult {
let mut segments = path.segments.iter().enumerate();
let mut curr_per_ns: PerNs<ModuleDef> = match path.kind {
let mut curr_per_ns: PerNs = match path.kind {
PathKind::Crate => {
PerNs::types(Module { krate: self.krate, module_id: self.root }.into())
}
@ -456,7 +456,7 @@ fn resolve_path_fp_with_macro(
ResolvePathResult::with(curr_per_ns, ReachedFixedPoint::Yes, None)
}
fn resolve_name_in_crate_root_or_extern_prelude(&self, name: &Name) -> PerNs<ModuleDef> {
fn resolve_name_in_crate_root_or_extern_prelude(&self, name: &Name) -> PerNs {
let from_crate_root =
self[self.root].scope.get(name).map_or_else(PerNs::none, |res| res.def);
let from_extern_prelude = self.resolve_name_in_extern_prelude(name);
@ -469,7 +469,7 @@ pub(crate) fn resolve_name_in_module(
db: &impl DefDatabase,
module: CrateModuleId,
name: &Name,
) -> PerNs<ModuleDef> {
) -> PerNs {
// Resolve in:
// - legacy scope of macro
// - current module / scope
@ -485,11 +485,11 @@ pub(crate) fn resolve_name_in_module(
from_legacy_macro.or(from_scope).or(from_extern_prelude).or(from_prelude)
}
fn resolve_name_in_extern_prelude(&self, name: &Name) -> PerNs<ModuleDef> {
fn resolve_name_in_extern_prelude(&self, name: &Name) -> PerNs {
self.extern_prelude.get(name).map_or(PerNs::none(), |&it| PerNs::types(it))
}
fn resolve_in_prelude(&self, db: &impl DefDatabase, name: &Name) -> PerNs<ModuleDef> {
fn resolve_in_prelude(&self, db: &impl DefDatabase, name: &Name) -> PerNs {
if let Some(prelude) = self.prelude {
let keep;
let def_map = if prelude.krate == self.krate {

View File

@ -243,7 +243,7 @@ fn resolve_import(
&self,
module_id: CrateModuleId,
import: &raw::ImportData,
) -> (PerNs<ModuleDef>, ReachedFixedPoint) {
) -> (PerNs, ReachedFixedPoint) {
log::debug!("resolving import: {:?} ({:?})", import, self.def_map.edition);
if import.is_extern_crate {
let res = self.def_map.resolve_name_in_extern_prelude(
@ -268,7 +268,7 @@ fn resolve_import(
fn record_resolved_import(
&mut self,
module_id: CrateModuleId,
def: PerNs<ModuleDef>,
def: PerNs,
import_id: raw::ImportId,
import: &raw::ImportData,
) {
@ -662,7 +662,7 @@ macro_rules! def {
};
}
let name = def.name.clone();
let def: PerNs<ModuleDef> = match def.kind {
let def: PerNs = match def.kind {
raw::DefKind::Function(ast_id) => PerNs::values(def!(Function, ast_id)),
raw::DefKind::Struct(ast_id) => {
let s = def!(Struct, ast_id);

View File

@ -1,4 +1,4 @@
use crate::MacroDef;
use crate::{MacroDef, ModuleDef};
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Namespace {
@ -9,38 +9,38 @@ pub enum Namespace {
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct PerNs<T> {
pub types: Option<T>,
pub values: Option<T>,
pub struct PerNs {
pub types: Option<ModuleDef>,
pub values: Option<ModuleDef>,
/// Since macros has different type, many methods simply ignore it.
/// We can only use special method like `get_macros` to access it.
pub macros: Option<MacroDef>,
}
impl<T> Default for PerNs<T> {
impl Default for PerNs {
fn default() -> Self {
PerNs { types: None, values: None, macros: None }
}
}
impl<T> PerNs<T> {
pub fn none() -> PerNs<T> {
impl PerNs {
pub fn none() -> PerNs {
PerNs { types: None, values: None, macros: None }
}
pub fn values(t: T) -> PerNs<T> {
pub fn values(t: ModuleDef) -> PerNs {
PerNs { types: None, values: Some(t), macros: None }
}
pub fn types(t: T) -> PerNs<T> {
pub fn types(t: ModuleDef) -> PerNs {
PerNs { types: Some(t), values: None, macros: None }
}
pub fn both(types: T, values: T) -> PerNs<T> {
pub fn both(types: ModuleDef, values: ModuleDef) -> PerNs {
PerNs { types: Some(types), values: Some(values), macros: None }
}
pub fn macros(macro_: MacroDef) -> PerNs<T> {
pub fn macros(macro_: MacroDef) -> PerNs {
PerNs { types: None, values: None, macros: Some(macro_) }
}
@ -52,11 +52,11 @@ pub fn is_all(&self) -> bool {
self.types.is_some() && self.values.is_some() && self.macros.is_some()
}
pub fn take_types(self) -> Option<T> {
pub fn take_types(self) -> Option<ModuleDef> {
self.types
}
pub fn take_values(self) -> Option<T> {
pub fn take_values(self) -> Option<ModuleDef> {
self.values
}
@ -64,11 +64,11 @@ pub fn get_macros(&self) -> Option<MacroDef> {
self.macros
}
pub fn only_macros(&self) -> PerNs<T> {
pub fn only_macros(&self) -> PerNs {
PerNs { types: None, values: None, macros: self.macros }
}
pub fn or(self, other: PerNs<T>) -> PerNs<T> {
pub fn or(self, other: PerNs) -> PerNs {
PerNs {
types: self.types.or(other.types),
values: self.values.or(other.values),

View File

@ -112,11 +112,7 @@ pub(crate) fn resolve_known_enum(&self, db: &impl HirDatabase, path: &Path) -> O
}
/// pub only for source-binder
pub(crate) fn resolve_module_path(
&self,
db: &impl HirDatabase,
path: &Path,
) -> PerNs<ModuleDef> {
pub(crate) fn resolve_module_path(&self, db: &impl HirDatabase, path: &Path) -> PerNs {
let (item_map, module) = match self.module() {
Some(it) => it,
None => return PerNs::none(),
@ -385,8 +381,8 @@ pub enum ScopeDef {
Unknown,
}
impl From<PerNs<ModuleDef>> for ScopeDef {
fn from(def: PerNs<ModuleDef>) -> Self {
impl From<PerNs> for ScopeDef {
fn from(def: PerNs) -> Self {
def.take_types()
.or_else(|| def.take_values())
.map(ScopeDef::ModuleDef)