Skip to content

Commit

Permalink
Replace exist_ty and ExistTy with opaque_ty and OpaqueTy
Browse files Browse the repository at this point in the history
  • Loading branch information
varkor committed Aug 2, 2019
1 parent 2777386 commit b4fe555
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 79 deletions.
2 changes: 1 addition & 1 deletion src/librustc/hir/intravisit.rs
Expand Up @@ -505,7 +505,7 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
visitor.visit_ty(ty);
visitor.visit_generics(generics)
}
ItemKind::OpaqueTy(ExistTy {
ItemKind::OpaqueTy(OpaqueTy {
ref generics,
ref bounds,
..
Expand Down
106 changes: 53 additions & 53 deletions src/librustc/hir/lowering.rs
Expand Up @@ -1721,38 +1721,38 @@ impl<'a> LoweringContext<'a> {
&mut self,
span: Span,
fn_def_id: Option<DefId>,
exist_ty_node_id: NodeId,
opaque_ty_node_id: NodeId,
lower_bounds: impl FnOnce(&mut LoweringContext<'_>) -> hir::GenericBounds,
) -> hir::TyKind {
// Make sure we know that some funky desugaring has been going on here.
// This is a first: there is code in other places like for loop
// desugaring that explicitly states that we don't want to track that.
// Not tracking it makes lints in rustc and clippy very fragile, as
// frequently opened issues show.
let exist_ty_span = self.mark_span_with_reason(
let opaque_ty_span = self.mark_span_with_reason(
DesugaringKind::OpaqueTy,
span,
None,
);

let exist_ty_def_index = self
let opaque_ty_def_index = self
.resolver
.definitions()
.opt_def_index(exist_ty_node_id)
.opt_def_index(opaque_ty_node_id)
.unwrap();

self.allocate_hir_id_counter(exist_ty_node_id);
self.allocate_hir_id_counter(opaque_ty_node_id);

let hir_bounds = self.with_hir_id_owner(exist_ty_node_id, lower_bounds);
let hir_bounds = self.with_hir_id_owner(opaque_ty_node_id, lower_bounds);

let (lifetimes, lifetime_defs) = self.lifetimes_from_impl_trait_bounds(
exist_ty_node_id,
exist_ty_def_index,
opaque_ty_node_id,
opaque_ty_def_index,
&hir_bounds,
);

self.with_hir_id_owner(exist_ty_node_id, |lctx| {
let exist_ty_item = hir::ExistTy {
self.with_hir_id_owner(opaque_ty_node_id, |lctx| {
let opaque_ty_item = hir::OpaqueTy {
generics: hir::Generics {
params: lifetime_defs,
where_clause: hir::WhereClause {
Expand All @@ -1766,51 +1766,51 @@ impl<'a> LoweringContext<'a> {
origin: hir::OpaqueTyOrigin::ReturnImplTrait,
};

trace!("exist ty from impl trait def-index: {:#?}", exist_ty_def_index);
let exist_ty_id = lctx.generate_opaque_type(
exist_ty_node_id,
exist_ty_item,
trace!("exist ty from impl trait def-index: {:#?}", opaque_ty_def_index);
let opaque_ty_id = lctx.generate_opaque_type(
opaque_ty_node_id,
opaque_ty_item,
span,
exist_ty_span,
opaque_ty_span,
);

// `impl Trait` now just becomes `Foo<'a, 'b, ..>`.
hir::TyKind::Def(hir::ItemId { id: exist_ty_id }, lifetimes)
hir::TyKind::Def(hir::ItemId { id: opaque_ty_id }, lifetimes)
})
}

/// Registers a new opaque type with the proper `NodeId`s and
/// returns the lowered node-ID for the opaque type.
fn generate_opaque_type(
&mut self,
exist_ty_node_id: NodeId,
exist_ty_item: hir::ExistTy,
opaque_ty_node_id: NodeId,
opaque_ty_item: hir::OpaqueTy,
span: Span,
exist_ty_span: Span,
opaque_ty_span: Span,
) -> hir::HirId {
let exist_ty_item_kind = hir::ItemKind::OpaqueTy(exist_ty_item);
let exist_ty_id = self.lower_node_id(exist_ty_node_id);
let opaque_ty_item_kind = hir::ItemKind::OpaqueTy(opaque_ty_item);
let opaque_ty_id = self.lower_node_id(opaque_ty_node_id);
// Generate an `type Foo = impl Trait;` declaration.
trace!("registering opaque type with id {:#?}", exist_ty_id);
let exist_ty_item = hir::Item {
hir_id: exist_ty_id,
trace!("registering opaque type with id {:#?}", opaque_ty_id);
let opaque_ty_item = hir::Item {
hir_id: opaque_ty_id,
ident: Ident::invalid(),
attrs: Default::default(),
node: exist_ty_item_kind,
node: opaque_ty_item_kind,
vis: respan(span.shrink_to_lo(), hir::VisibilityKind::Inherited),
span: exist_ty_span,
span: opaque_ty_span,
};

// Insert the item into the global item list. This usually happens
// automatically for all AST items. But this opaque type item
// does not actually exist in the AST.
self.insert_item(exist_ty_item);
exist_ty_id
self.insert_item(opaque_ty_item);
opaque_ty_id
}

fn lifetimes_from_impl_trait_bounds(
&mut self,
exist_ty_id: NodeId,
opaque_ty_id: NodeId,
parent_index: DefIndex,
bounds: &hir::GenericBounds,
) -> (HirVec<hir::GenericArg>, HirVec<hir::GenericParam>) {
Expand All @@ -1820,7 +1820,7 @@ impl<'a> LoweringContext<'a> {
struct ImplTraitLifetimeCollector<'r, 'a> {
context: &'r mut LoweringContext<'a>,
parent: DefIndex,
exist_ty_id: NodeId,
opaque_ty_id: NodeId,
collect_elided_lifetimes: bool,
currently_bound_lifetimes: Vec<hir::LifetimeName>,
already_defined_lifetimes: FxHashSet<hir::LifetimeName>,
Expand Down Expand Up @@ -1916,7 +1916,7 @@ impl<'a> LoweringContext<'a> {

let def_node_id = self.context.sess.next_node_id();
let hir_id =
self.context.lower_node_id_with_owner(def_node_id, self.exist_ty_id);
self.context.lower_node_id_with_owner(def_node_id, self.opaque_ty_id);
self.context.resolver.definitions().create_def_with_parent(
self.parent,
def_node_id,
Expand Down Expand Up @@ -1952,7 +1952,7 @@ impl<'a> LoweringContext<'a> {
let mut lifetime_collector = ImplTraitLifetimeCollector {
context: self,
parent: parent_index,
exist_ty_id,
opaque_ty_id,
collect_elided_lifetimes: true,
currently_bound_lifetimes: Vec::new(),
already_defined_lifetimes: FxHashSet::default(),
Expand Down Expand Up @@ -2582,40 +2582,40 @@ impl<'a> LoweringContext<'a> {
})
}

// Transforms `-> T` for `async fn` into `-> ExistTy { .. }`
// combined with the following definition of `ExistTy`:
// Transforms `-> T` for `async fn` into `-> OpaqueTy { .. }`
// combined with the following definition of `OpaqueTy`:
//
// type ExistTy<generics_from_parent_fn> = impl Future<Output = T>;
// type OpaqueTy<generics_from_parent_fn> = impl Future<Output = T>;
//
// `inputs`: lowered types of arguments to the function (used to collect lifetimes)
// `output`: unlowered output type (`T` in `-> T`)
// `fn_def_id`: `DefId` of the parent function (used to create child impl trait definition)
// `exist_ty_node_id`: `NodeId` of the opaque `impl Trait` type that should be created
// `opaque_ty_node_id`: `NodeId` of the opaque `impl Trait` type that should be created
// `elided_lt_replacement`: replacement for elided lifetimes in the return type
fn lower_async_fn_ret_ty(
&mut self,
output: &FunctionRetTy,
fn_def_id: DefId,
exist_ty_node_id: NodeId,
opaque_ty_node_id: NodeId,
elided_lt_replacement: LtReplacement,
) -> hir::FunctionRetTy {
let span = output.span();

let exist_ty_span = self.mark_span_with_reason(
let opaque_ty_span = self.mark_span_with_reason(
DesugaringKind::Async,
span,
None,
);

let exist_ty_def_index = self
let opaque_ty_def_index = self
.resolver
.definitions()
.opt_def_index(exist_ty_node_id)
.opt_def_index(opaque_ty_node_id)
.unwrap();

self.allocate_hir_id_counter(exist_ty_node_id);
self.allocate_hir_id_counter(opaque_ty_node_id);

let (exist_ty_id, lifetime_params) = self.with_hir_id_owner(exist_ty_node_id, |this| {
let (opaque_ty_id, lifetime_params) = self.with_hir_id_owner(opaque_ty_node_id, |this| {
let future_bound = this.with_anonymous_lifetime_mode(
AnonymousLifetimeMode::Replace(elided_lt_replacement),
|this| this.lower_async_fn_output_type_to_future_bound(
Expand All @@ -2642,11 +2642,11 @@ impl<'a> LoweringContext<'a> {
lifetime_params
.iter().cloned()
.map(|(span, hir_name)| {
this.lifetime_to_generic_param(span, hir_name, exist_ty_def_index)
this.lifetime_to_generic_param(span, hir_name, opaque_ty_def_index)
})
.collect();

let exist_ty_item = hir::ExistTy {
let opaque_ty_item = hir::OpaqueTy {
generics: hir::Generics {
params: generic_params,
where_clause: hir::WhereClause {
Expand All @@ -2660,15 +2660,15 @@ impl<'a> LoweringContext<'a> {
origin: hir::OpaqueTyOrigin::AsyncFn,
};

trace!("exist ty from async fn def index: {:#?}", exist_ty_def_index);
let exist_ty_id = this.generate_opaque_type(
exist_ty_node_id,
exist_ty_item,
trace!("exist ty from async fn def index: {:#?}", opaque_ty_def_index);
let opaque_ty_id = this.generate_opaque_type(
opaque_ty_node_id,
opaque_ty_item,
span,
exist_ty_span,
opaque_ty_span,
);

(exist_ty_id, lifetime_params)
(opaque_ty_id, lifetime_params)
});

let generic_args =
Expand All @@ -2683,10 +2683,10 @@ impl<'a> LoweringContext<'a> {
})
.collect();

let exist_ty_ref = hir::TyKind::Def(hir::ItemId { id: exist_ty_id }, generic_args);
let opaque_ty_ref = hir::TyKind::Def(hir::ItemId { id: opaque_ty_id }, generic_args);

hir::FunctionRetTy::Return(P(hir::Ty {
node: exist_ty_ref,
node: opaque_ty_ref,
span,
hir_id: self.next_id(),
}))
Expand Down Expand Up @@ -3445,7 +3445,7 @@ impl<'a> LoweringContext<'a> {
self.lower_generics(generics, ImplTraitContext::disallowed()),
),
ItemKind::OpaqueTy(ref b, ref generics) => hir::ItemKind::OpaqueTy(
hir::ExistTy {
hir::OpaqueTy {
generics: self.lower_generics(generics,
ImplTraitContext::OpaqueTy(None)),
bounds: self.lower_param_bounds(b,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/hir/map/mod.rs
Expand Up @@ -827,7 +827,7 @@ impl<'hir> Map<'hir> {
match self.get(scope) {
Node::Item(i) => {
match i.node {
ItemKind::OpaqueTy(ExistTy { impl_trait_fn: None, .. }) => {}
ItemKind::OpaqueTy(OpaqueTy { impl_trait_fn: None, .. }) => {}
_ => break,
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/hir/mod.rs
Expand Up @@ -1922,7 +1922,7 @@ pub struct BareFnTy {
}

#[derive(RustcEncodable, RustcDecodable, Debug, HashStable)]
pub struct ExistTy {
pub struct OpaqueTy {
pub generics: Generics,
pub bounds: GenericBounds,
pub impl_trait_fn: Option<DefId>,
Expand Down Expand Up @@ -2422,7 +2422,7 @@ pub enum ItemKind {
/// A type alias, e.g., `type Foo = Bar<u8>`
Ty(P<Ty>, Generics),
/// An opaque `impl Trait` type alias, e.g., `type Foo = impl Bar;`
OpaqueTy(ExistTy),
OpaqueTy(OpaqueTy),
/// An enum definition, e.g., `enum Foo<A, B> {C<A>, D<B>}`
Enum(EnumDef, Generics),
/// A struct definition, e.g., `struct Foo<A> {x: A}`
Expand Down Expand Up @@ -2479,7 +2479,7 @@ impl ItemKind {
Some(match *self {
ItemKind::Fn(_, _, ref generics, _) |
ItemKind::Ty(_, ref generics) |
ItemKind::OpaqueTy(ExistTy { ref generics, impl_trait_fn: None, .. }) |
ItemKind::OpaqueTy(OpaqueTy { ref generics, impl_trait_fn: None, .. }) |
ItemKind::Enum(_, ref generics) |
ItemKind::Struct(_, ref generics) |
ItemKind::Union(_, ref generics) |
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/infer/opaque_types/mod.rs
Expand Up @@ -1033,13 +1033,13 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> {
let (in_definition_scope, origin) = match tcx.hir().find(opaque_hir_id) {
Some(Node::Item(item)) => match item.node {
// Anonymous `impl Trait`
hir::ItemKind::OpaqueTy(hir::ExistTy {
hir::ItemKind::OpaqueTy(hir::OpaqueTy {
impl_trait_fn: Some(parent),
origin,
..
}) => (parent == self.parent_def_id, origin),
// Named `type Foo = impl Bar;`
hir::ItemKind::OpaqueTy(hir::ExistTy {
hir::ItemKind::OpaqueTy(hir::OpaqueTy {
impl_trait_fn: None,
origin,
..
Expand Down
10 changes: 5 additions & 5 deletions src/librustc/middle/resolve_lifetime.rs
Expand Up @@ -480,7 +480,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
};
self.with(scope, |_, this| intravisit::walk_item(this, item));
}
hir::ItemKind::OpaqueTy(hir::ExistTy {
hir::ItemKind::OpaqueTy(hir::OpaqueTy {
impl_trait_fn: Some(_),
..
}) => {
Expand All @@ -489,7 +489,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
// it.
}
hir::ItemKind::Ty(_, ref generics)
| hir::ItemKind::OpaqueTy(hir::ExistTy {
| hir::ItemKind::OpaqueTy(hir::OpaqueTy {
impl_trait_fn: None,
ref generics,
..
Expand Down Expand Up @@ -629,15 +629,15 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
{
// Named opaque `impl Trait` types are reached via `TyKind::Path`.
// This arm is for `impl Trait` in the types of statics, constants and locals.
hir::ItemKind::OpaqueTy(hir::ExistTy {
hir::ItemKind::OpaqueTy(hir::OpaqueTy {
impl_trait_fn: None,
..
}) => {
intravisit::walk_ty(self, ty);
return;
}
// RPIT (return position impl trait)
hir::ItemKind::OpaqueTy(hir::ExistTy {
hir::ItemKind::OpaqueTy(hir::OpaqueTy {
ref generics,
ref bounds,
..
Expand Down Expand Up @@ -1254,7 +1254,7 @@ fn compute_object_lifetime_defaults(tcx: TyCtxt<'_>) -> HirIdMap<Vec<ObjectLifet
hir::ItemKind::Struct(_, ref generics)
| hir::ItemKind::Union(_, ref generics)
| hir::ItemKind::Enum(_, ref generics)
| hir::ItemKind::OpaqueTy(hir::ExistTy {
| hir::ItemKind::OpaqueTy(hir::OpaqueTy {
ref generics,
impl_trait_fn: None,
..
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/ty/mod.rs
Expand Up @@ -3214,8 +3214,8 @@ fn trait_of_item(tcx: TyCtxt<'_>, def_id: DefId) -> Option<DefId> {
pub fn is_impl_trait_defn(tcx: TyCtxt<'_>, def_id: DefId) -> Option<DefId> {
if let Some(hir_id) = tcx.hir().as_local_hir_id(def_id) {
if let Node::Item(item) = tcx.hir().get(hir_id) {
if let hir::ItemKind::OpaqueTy(ref exist_ty) = item.node {
return exist_ty.impl_trait_fn;
if let hir::ItemKind::OpaqueTy(ref opaque_ty) = item.node {
return opaque_ty.impl_trait_fn;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_typeck/check/mod.rs
Expand Up @@ -1403,7 +1403,7 @@ pub fn check_item_type<'tcx>(tcx: TyCtxt<'tcx>, it: &'tcx hir::Item) {
hir::ItemKind::Union(..) => {
check_union(tcx, it.hir_id, it.span);
}
hir::ItemKind::OpaqueTy(hir::ExistTy{origin, ..}) => {
hir::ItemKind::OpaqueTy(hir::OpaqueTy{origin, ..}) => {
let def_id = tcx.hir().local_def_id(it.hir_id);

let substs = InternalSubsts::identity_for_item(tcx, def_id);
Expand Down

0 comments on commit b4fe555

Please sign in to comment.