Replace rustc_typeck::Namespace with rustc_hir::def::Namespace

This commit is contained in:
Dylan MacKenzie 2020-02-11 10:41:28 -08:00
parent ea3c9d27cf
commit 3bb7da2e4f
10 changed files with 32 additions and 48 deletions

View file

@ -33,7 +33,7 @@ use rustc_data_structures::fx::FxIndexMap;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_data_structures::sync::{self, par_iter, Lrc, ParallelIterator};
use rustc_hir as hir;
use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
use rustc_hir::def::{CtorKind, CtorOf, DefKind, Namespace, Res};
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc_hir::{Constness, GlobMap, Node, TraitMap};
use rustc_index::vec::{Idx, IndexVec};
@ -216,6 +216,13 @@ impl AssocKind {
ty::AssocKind::Const => "associated constant",
}
}
pub fn namespace(&self) -> Namespace {
match *self {
ty::AssocKind::OpaqueTy | ty::AssocKind::Type => Namespace::TypeNS,
ty::AssocKind::Const | ty::AssocKind::Method => Namespace::ValueNS,
}
}
}
impl AssocItem {

View file

@ -1,4 +1,4 @@
use crate::def::{DefKind, Res};
use crate::def::{DefKind, Namespace, Res};
use crate::def_id::DefId;
crate use crate::hir_id::HirId;
use crate::itemlikevisit;
@ -1897,6 +1897,15 @@ pub enum ImplItemKind<'hir> {
OpaqueTy(GenericBounds<'hir>),
}
impl ImplItemKind<'_> {
pub fn namespace(&self) -> Namespace {
match self {
ImplItemKind::OpaqueTy(..) | ImplItemKind::TyAlias(..) => Namespace::TypeNS,
ImplItemKind::Const(..) | ImplItemKind::Method(..) => Namespace::ValueNS,
}
}
}
// The name of the associated type for `Fn` return types.
pub const FN_OUTPUT_NAME: Symbol = sym::Output;

View file

@ -9,7 +9,6 @@ use crate::collect::PlaceholderHirTyCollector;
use crate::lint;
use crate::middle::lang_items::SizedTraitLangItem;
use crate::middle::resolve_lifetime as rl;
use crate::namespace::Namespace;
use crate::require_c_abi_if_c_variadic;
use crate::util::common::ErrorReported;
use rustc::lint::builtin::AMBIGUOUS_ASSOCIATED_ITEMS;
@ -20,7 +19,7 @@ use rustc::ty::{GenericParamDef, GenericParamDefKind};
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticId};
use rustc_hir as hir;
use rustc_hir::def::{CtorOf, DefKind, Res};
use rustc_hir::def::{CtorOf, DefKind, Namespace, Res};
use rustc_hir::def_id::DefId;
use rustc_hir::intravisit::Visitor;
use rustc_hir::print;
@ -2202,7 +2201,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
let item = tcx
.associated_items(trait_did)
.iter()
.find(|i| Namespace::from(i.kind) == Namespace::Type && i.ident.modern() == assoc_ident)
.find(|i| i.kind.namespace() == Namespace::TypeNS && i.ident.modern() == assoc_ident)
.expect("missing associated type");
let ty = self.projected_ty_from_poly_trait_ref(span, item.def_id, assoc_segment, bound);

View file

@ -11,7 +11,6 @@ pub use self::CandidateSource::*;
pub use self::MethodError::*;
use crate::check::FnCtxt;
use crate::namespace::Namespace;
use rustc::ty::subst::Subst;
use rustc::ty::subst::{InternalSubsts, SubstsRef};
use rustc::ty::GenericParamDefKind;
@ -19,7 +18,7 @@ use rustc::ty::{self, ToPolyTraitRef, ToPredicate, TraitRef, Ty, TypeFoldable, W
use rustc_data_structures::sync::Lrc;
use rustc_errors::{Applicability, DiagnosticBuilder};
use rustc_hir as hir;
use rustc_hir::def::{CtorOf, DefKind};
use rustc_hir::def::{CtorOf, DefKind, Namespace};
use rustc_hir::def_id::DefId;
use rustc_infer::infer::{self, InferOk};
use rustc_infer::traits;
@ -342,7 +341,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Trait must have a method named `m_name` and it should not have
// type parameters or early-bound regions.
let tcx = self.tcx;
let method_item = match self.associated_item(trait_def_id, m_name, Namespace::Value) {
let method_item = match self.associated_item(trait_def_id, m_name, Namespace::ValueNS) {
Some(method_item) => method_item,
None => {
tcx.sess.delay_span_bug(

View file

@ -7,7 +7,6 @@ use crate::check::autoderef::{self, Autoderef};
use crate::check::FnCtxt;
use crate::hir::def::DefKind;
use crate::hir::def_id::DefId;
use crate::namespace::Namespace;
use rustc::lint;
use rustc::middle::stability;
@ -22,6 +21,7 @@ use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::sync::Lrc;
use rustc_errors::struct_span_err;
use rustc_hir as hir;
use rustc_hir::def::Namespace;
use rustc_infer::infer::canonical::OriginalQueryValues;
use rustc_infer::infer::canonical::{Canonical, QueryResponse};
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
@ -1699,13 +1699,13 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
.iter()
.filter(|x| {
let dist = lev_distance(&*name.as_str(), &x.ident.as_str());
Namespace::from(x.kind) == Namespace::Value && dist > 0 && dist <= max_dist
x.kind.namespace() == Namespace::ValueNS && dist > 0 && dist <= max_dist
})
.copied()
.collect()
} else {
self.fcx
.associated_item(def_id, name, Namespace::Value)
.associated_item(def_id, name, Namespace::ValueNS)
.map_or(Vec::new(), |x| vec![x])
}
} else {

View file

@ -3,7 +3,6 @@
use crate::check::FnCtxt;
use crate::middle::lang_items::FnOnceTraitLangItem;
use crate::namespace::Namespace;
use rustc::hir::map as hir_map;
use rustc::hir::map::Map;
use rustc::ty::print::with_crate_prefix;
@ -11,7 +10,7 @@ use rustc::ty::{self, ToPolyTraitRef, ToPredicate, Ty, TyCtxt, TypeFoldable, Wit
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder};
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def::{DefKind, Namespace, Res};
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc_hir::intravisit;
use rustc_hir::{ExprKind, Node, QPath};
@ -97,13 +96,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Provide the best span we can. Use the item, if local to crate, else
// the impl, if local to crate (item may be defaulted), else nothing.
let item = match self
.associated_item(impl_did, item_name, Namespace::Value)
.associated_item(impl_did, item_name, Namespace::ValueNS)
.or_else(|| {
let impl_trait_ref = self.tcx.impl_trait_ref(impl_did)?;
self.associated_item(
impl_trait_ref.def_id,
item_name,
Namespace::Value,
Namespace::ValueNS,
)
}) {
Some(item) => item,
@ -185,7 +184,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
CandidateSource::TraitSource(trait_did) => {
let item =
match self.associated_item(trait_did, item_name, Namespace::Value) {
match self.associated_item(trait_did, item_name, Namespace::ValueNS) {
Some(item) => item,
None => continue,
};
@ -264,7 +263,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// be used exists at all, and the type is an ambiguous numeric type
// ({integer}/{float}).
let mut candidates = all_traits(self.tcx).into_iter().filter_map(|info| {
self.associated_item(info.def_id, item_name, Namespace::Value)
self.associated_item(info.def_id, item_name, Namespace::ValueNS)
});
if let (true, false, SelfSource::MethodCall(expr), Some(_)) = (
actual.is_numeric(),
@ -779,7 +778,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// here).
(type_is_local || info.def_id.is_local())
&& self
.associated_item(info.def_id, item_name, Namespace::Value)
.associated_item(info.def_id, item_name, Namespace::ValueNS)
.filter(|item| {
// We only want to suggest public or local traits (#45781).
item.vis == ty::Visibility::Public || info.def_id.is_local()

View file

@ -89,7 +89,6 @@ pub mod writeback;
use crate::astconv::{AstConv, PathSeg};
use crate::middle::lang_items;
use crate::namespace::Namespace;
use rustc::hir::map::blocks::FnLikeNode;
use rustc::hir::map::Map;
use rustc::middle::region;
@ -1972,6 +1971,7 @@ fn check_impl_items_against_trait<'tcx>(
// Check existing impl methods to see if they are both present in trait
// and compatible with trait signature
for impl_item in impl_items() {
let namespace = impl_item.kind.namespace();
let ty_impl_item = tcx.associated_item(tcx.hir().local_def_id(impl_item.hir_id));
let ty_trait_item = tcx
.associated_items(impl_trait_ref.def_id)

View file

@ -1,4 +1,3 @@
use crate::namespace::Namespace;
use rustc::ty::{AssocItem, TyCtxt};
use rustc_errors::struct_span_err;
use rustc_hir as hir;

View file

@ -83,7 +83,6 @@ mod collect;
mod constrained_generic_params;
mod impl_wf_check;
mod mem_categorization;
mod namespace;
mod outlives;
mod structured_errors;
mod variance;

View file

@ -1,27 +0,0 @@
use rustc::ty;
use rustc_hir as hir;
// Whether an item exists in the type or value namespace.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Namespace {
Type,
Value,
}
impl From<ty::AssocKind> for Namespace {
fn from(a_kind: ty::AssocKind) -> Self {
match a_kind {
ty::AssocKind::OpaqueTy | ty::AssocKind::Type => Namespace::Type,
ty::AssocKind::Const | ty::AssocKind::Method => Namespace::Value,
}
}
}
impl<'a> From<&'a hir::ImplItemKind<'_>> for Namespace {
fn from(impl_kind: &'a hir::ImplItemKind<'_>) -> Self {
match *impl_kind {
hir::ImplItemKind::OpaqueTy(..) | hir::ImplItemKind::TyAlias(..) => Namespace::Type,
hir::ImplItemKind::Const(..) | hir::ImplItemKind::Method(..) => Namespace::Value,
}
}
}