Use Idents for associated type bindings in HIR

This commit is contained in:
Vadim Petrochenkov 2018-06-10 15:55:48 +03:00
parent c5454c04bc
commit 1fe9b4d763
15 changed files with 38 additions and 36 deletions

View File

@ -660,7 +660,7 @@ pub fn walk_generic_args<'v, V: Visitor<'v>>(visitor: &mut V,
pub fn walk_assoc_type_binding<'v, V: Visitor<'v>>(visitor: &mut V,
type_binding: &'v TypeBinding) {
visitor.visit_id(type_binding.id);
visitor.visit_name(type_binding.span, type_binding.name);
visitor.visit_ident(type_binding.ident);
visitor.visit_ty(&type_binding.ty);
}

View File

@ -1064,7 +1064,7 @@ impl<'a> LoweringContext<'a> {
fn lower_ty_binding(&mut self, b: &TypeBinding, itctx: ImplTraitContext) -> hir::TypeBinding {
hir::TypeBinding {
id: self.lower_node_id(b.id).node_id,
name: self.lower_ident(b.ident),
ident: b.ident,
ty: self.lower_ty(&b.ty, itctx),
span: b.span,
}
@ -1820,7 +1820,7 @@ impl<'a> LoweringContext<'a> {
bindings: hir_vec![
hir::TypeBinding {
id: this.next_id().node_id,
name: Symbol::intern(FN_OUTPUT_NAME),
ident: Ident::from_str(FN_OUTPUT_NAME),
ty: output
.as_ref()
.map(|ty| this.lower_ty(&ty, DISALLOWED))

View File

@ -1607,7 +1607,7 @@ pub enum ImplItemKind {
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub struct TypeBinding {
pub id: NodeId,
pub name: Name,
pub ident: Ident,
pub ty: P<Ty>,
pub span: Span,
}

View File

@ -1762,7 +1762,7 @@ impl<'a> State<'a> {
for binding in generic_args.bindings.iter() {
start_or_comma(self)?;
self.print_name(binding.name)?;
self.print_ident(binding.ident)?;
self.s.space()?;
self.word_space("=")?;
self.print_type(&binding.ty)?;

View File

@ -278,7 +278,7 @@ impl_stable_hash_for!(struct hir::MethodSig {
impl_stable_hash_for!(struct hir::TypeBinding {
id,
name,
ident,
ty,
span
});

View File

@ -31,6 +31,7 @@ use infer::type_variable::TypeVariableOrigin;
use middle::const_val::ConstVal;
use mir::interpret::{GlobalId};
use rustc_data_structures::snapshot_map::{Snapshot, SnapshotMap};
use syntax::ast::Ident;
use syntax::symbol::Symbol;
use ty::subst::{Subst, Substs};
use ty::{self, ToPredicate, ToPolyTraitRef, Ty, TyCtxt};
@ -1452,7 +1453,7 @@ fn confirm_callable_candidate<'cx, 'gcx, 'tcx>(
projection_ty: ty::ProjectionTy::from_ref_and_name(
tcx,
trait_ref,
Symbol::intern(FN_OUTPUT_NAME),
Ident::from_str(FN_OUTPUT_NAME),
),
ty: ret_type
}
@ -1546,7 +1547,7 @@ fn assoc_ty_def<'cx, 'gcx, 'tcx>(
let impl_node = specialization_graph::Node::Impl(impl_def_id);
for item in impl_node.items(tcx) {
if item.kind == ty::AssociatedKind::Type &&
tcx.hygienic_eq(item.name, assoc_ty_name, trait_def_id) {
tcx.hygienic_eq(item.name.to_ident(), assoc_ty_name.to_ident(), trait_def_id) {
return specialization_graph::NodeItem {
node: specialization_graph::Node::Impl(impl_def_id),
item,

View File

@ -379,7 +379,7 @@ impl<'a, 'gcx, 'tcx> Ancestors {
self.flat_map(move |node| {
node.items(tcx).filter(move |impl_item| {
impl_item.kind == trait_item_kind &&
tcx.hygienic_eq(impl_item.name, trait_item_name, trait_def_id)
tcx.hygienic_eq(impl_item.name.to_ident(), trait_item_name.to_ident(), trait_def_id)
}).map(move |item| NodeItem { node: node, item: item })
})
}

View File

@ -2717,9 +2717,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
// Hygienically compare a use-site name (`use_name`) for a field or an associated item with its
// supposed definition name (`def_name`). The method also needs `DefId` of the supposed
// definition's parent/scope to perform comparison.
pub fn hygienic_eq(self, use_name: Name, def_name: Name, def_parent_def_id: DefId) -> bool {
let (use_ident, def_ident) = (use_name.to_ident(), def_name.to_ident());
self.adjust_ident(use_ident, def_parent_def_id, DUMMY_NODE_ID).0 == def_ident
pub fn hygienic_eq(self, use_name: Ident, def_name: Ident, def_parent_def_id: DefId) -> bool {
self.adjust_ident(use_name, def_parent_def_id, DUMMY_NODE_ID).0 == def_name.modern()
}
pub fn adjust_ident(self, mut ident: Ident, scope: DefId, block: NodeId) -> (Ident, DefId) {

View File

@ -25,7 +25,7 @@ use mir::interpret::{Scalar, Pointer, Value, ConstValue};
use std::iter;
use std::cmp::Ordering;
use rustc_target::spec::abi;
use syntax::ast::{self, Name};
use syntax::ast::{self, Ident};
use syntax::symbol::{keywords, InternedString};
use serialize;
@ -853,11 +853,11 @@ impl<'a, 'tcx> ProjectionTy<'tcx> {
/// Construct a ProjectionTy by searching the trait from trait_ref for the
/// associated item named item_name.
pub fn from_ref_and_name(
tcx: TyCtxt, trait_ref: ty::TraitRef<'tcx>, item_name: Name
tcx: TyCtxt, trait_ref: ty::TraitRef<'tcx>, item_name: Ident
) -> ProjectionTy<'tcx> {
let item_def_id = tcx.associated_items(trait_ref.def_id).find(|item| {
item.kind == ty::AssociatedKind::Type &&
tcx.hygienic_eq(item_name, item.name, trait_ref.def_id)
tcx.hygienic_eq(item_name, item.name.to_ident(), trait_ref.def_id)
}).unwrap().def_id;
ProjectionTy {

View File

@ -417,7 +417,7 @@ pub fn program_clauses_for_associated_type_value<'a, 'tcx>(
let hypotheses = vec![trait_implemented];
// `<A0 as Trait<A1..An>>::AssocType<Pn+1..Pm>`
let projection_ty = ty::ProjectionTy::from_ref_and_name(tcx, trait_ref, item.name);
let projection_ty = ty::ProjectionTy::from_ref_and_name(tcx, trait_ref, item.name.to_ident());
// `Normalize(<A0 as Trait<A1..An>>::AssocType<Pn+1..Pm> -> T)`
let normalize_goal = DomainGoal::Normalize(ty::ProjectionPredicate { projection_ty, ty });

View File

@ -83,7 +83,7 @@ pub trait AstConv<'gcx, 'tcx> {
}
struct ConvertedBinding<'tcx> {
item_name: ast::Name,
item_name: ast::Ident,
ty: Ty<'tcx>,
span: Span,
}
@ -342,7 +342,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
let assoc_bindings = generic_args.bindings.iter().map(|binding| {
ConvertedBinding {
item_name: binding.name,
item_name: binding.ident,
ty: self.ast_ty_to_ty(&binding.ty),
span: binding.span,
}
@ -485,12 +485,12 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
fn trait_defines_associated_type_named(&self,
trait_def_id: DefId,
assoc_name: ast::Name)
assoc_name: ast::Ident)
-> bool
{
self.tcx().associated_items(trait_def_id).any(|item| {
item.kind == ty::AssociatedKind::Type &&
self.tcx().hygienic_eq(assoc_name, item.name, trait_def_id)
self.tcx().hygienic_eq(assoc_name, item.name.to_ident(), trait_def_id)
})
}
@ -569,7 +569,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
}?;
let (assoc_ident, def_scope) =
tcx.adjust_ident(binding.item_name.to_ident(), candidate.def_id(), ref_id);
tcx.adjust_ident(binding.item_name, candidate.def_id(), ref_id);
let assoc_ty = tcx.associated_items(candidate.def_id()).find(|i| {
i.kind == ty::AssociatedKind::Type && i.name.to_ident() == assoc_ident
}).expect("missing associated type");
@ -778,7 +778,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
// any ambiguity.
fn find_bound_for_assoc_item(&self,
ty_param_def_id: DefId,
assoc_name: ast::Name,
assoc_name: ast::Ident,
span: Span)
-> Result<ty::PolyTraitRef<'tcx>, ErrorReported>
{
@ -807,7 +807,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
fn one_bound_for_assoc_type<I>(&self,
mut bounds: I,
ty_param_name: &str,
assoc_name: ast::Name,
assoc_name: ast::Ident,
span: Span)
-> Result<ty::PolyTraitRef<'tcx>, ErrorReported>
where I: Iterator<Item=ty::PolyTraitRef<'tcx>>
@ -837,7 +837,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
for bound in bounds {
let bound_span = self.tcx().associated_items(bound.def_id()).find(|item| {
item.kind == ty::AssociatedKind::Type &&
self.tcx().hygienic_eq(assoc_name, item.name, bound.def_id())
self.tcx().hygienic_eq(assoc_name, item.name.to_ident(), bound.def_id())
})
.and_then(|item| self.tcx().hir.span_if_local(item.def_id));
@ -873,7 +873,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
-> (Ty<'tcx>, Def)
{
let tcx = self.tcx();
let assoc_name = item_segment.name;
let assoc_name = item_segment.name.to_ident();
debug!("associated_path_def_to_ty: {:?}::{}", ty, assoc_name);
@ -895,8 +895,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
let candidates =
traits::supertraits(tcx, ty::Binder::bind(trait_ref))
.filter(|r| self.trait_defines_associated_type_named(r.def_id(),
assoc_name));
.filter(|r| self.trait_defines_associated_type_named(r.def_id(), assoc_name));
match self.one_bound_for_assoc_type(candidates, "Self", assoc_name, span) {
Ok(bound) => bound,
@ -923,7 +922,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
};
let trait_did = bound.def_id();
let (assoc_ident, def_scope) = tcx.adjust_ident(assoc_name.to_ident(), trait_did, ref_id);
let (assoc_ident, def_scope) = tcx.adjust_ident(assoc_name, trait_did, ref_id);
let item = tcx.associated_items(trait_did).find(|i| {
Namespace::from(i.kind) == Namespace::Type &&
i.name.to_ident() == assoc_ident

View File

@ -21,7 +21,7 @@ use rustc::ty::{ToPredicate, TypeFoldable};
use rustc::ty::adjustment::{Adjustment, Adjust, OverloadedDeref};
use syntax_pos::Span;
use syntax::symbol::Symbol;
use syntax::ast::Ident;
use std::iter;
@ -134,7 +134,7 @@ impl<'a, 'gcx, 'tcx> Autoderef<'a, 'gcx, 'tcx> {
ty::ProjectionTy::from_ref_and_name(
tcx,
trait_ref,
Symbol::intern("Target"),
Ident::from_str("Target"),
),
cause,
0,

View File

@ -387,8 +387,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
/// and return it, or `None`, if no such item was defined there.
pub fn associated_item(&self, def_id: DefId, item_name: ast::Name, ns: Namespace)
-> Option<ty::AssociatedItem> {
self.tcx.associated_items(def_id)
.find(|item| Namespace::from(item.kind) == ns &&
self.tcx.hygienic_eq(item_name, item.name, def_id))
self.tcx.associated_items(def_id).find(|item| {
Namespace::from(item.kind) == ns &&
self.tcx.hygienic_eq(item_name.to_ident(), item.name.to_ident(), def_id)
})
}
}

View File

@ -1400,11 +1400,13 @@ fn check_impl_items_against_trait<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
let ty_impl_item = tcx.associated_item(tcx.hir.local_def_id(impl_item.id));
let ty_trait_item = tcx.associated_items(impl_trait_ref.def_id)
.find(|ac| Namespace::from(&impl_item.node) == Namespace::from(ac.kind) &&
tcx.hygienic_eq(ty_impl_item.name, ac.name, impl_trait_ref.def_id))
tcx.hygienic_eq(ty_impl_item.name.to_ident(), ac.name.to_ident(),
impl_trait_ref.def_id))
.or_else(|| {
// Not compatible, but needed for the error message
tcx.associated_items(impl_trait_ref.def_id)
.find(|ac| tcx.hygienic_eq(ty_impl_item.name, ac.name, impl_trait_ref.def_id))
.find(|ac| tcx.hygienic_eq(ty_impl_item.name.to_ident(), ac.name.to_ident(),
impl_trait_ref.def_id))
});
// Check that impl definition matches trait definition

View File

@ -4293,7 +4293,7 @@ pub struct TypeBinding {
impl Clean<TypeBinding> for hir::TypeBinding {
fn clean(&self, cx: &DocContext) -> TypeBinding {
TypeBinding {
name: self.name.clean(cx),
name: self.ident.name.clean(cx),
ty: self.ty.clean(cx)
}
}