Skip to content

Commit

Permalink
Replace abstract type with type alias impl Trait
Browse files Browse the repository at this point in the history
  • Loading branch information
varkor committed Aug 2, 2019
1 parent 70c8839 commit 2777386
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/librustc/hir/lowering.rs
Expand Up @@ -1894,7 +1894,7 @@ impl<'a> LoweringContext<'a> {
hir::LifetimeName::Implicit | hir::LifetimeName::Underscore => {
if self.collect_elided_lifetimes {
// Use `'_` for both implicit and underscore lifetimes in
// `abstract type Foo<'_>: SomeTrait<'_>;`.
// `type Foo<'_> = impl SomeTrait<'_>;`.
hir::LifetimeName::Underscore
} else {
return;
Expand Down
38 changes: 19 additions & 19 deletions src/librustc/infer/opaque_types/mod.rs
Expand Up @@ -18,19 +18,19 @@ use syntax_pos::Span;

pub type OpaqueTypeMap<'tcx> = DefIdMap<OpaqueTypeDecl<'tcx>>;

/// Information about the opaque, abstract types whose values we
/// Information about the opaque types whose values we
/// are inferring in this function (these are the `impl Trait` that
/// appear in the return type).
#[derive(Copy, Clone, Debug)]
pub struct OpaqueTypeDecl<'tcx> {
/// The substitutions that we apply to the abstract that this
/// The substitutions that we apply to the opaque type that this
/// `impl Trait` desugars to. e.g., if:
///
/// fn foo<'a, 'b, T>() -> impl Trait<'a>
///
/// winds up desugared to:
///
/// abstract type Foo<'x, X>: Trait<'x>
/// type Foo<'x, X> = impl Trait<'x>
/// fn foo<'a, 'b, T>() -> Foo<'a, T>
///
/// then `substs` would be `['a, T]`.
Expand All @@ -50,7 +50,7 @@ pub struct OpaqueTypeDecl<'tcx> {
/// over-approximated, but better than nothing.
pub definition_span: Span,

/// The type variable that represents the value of the abstract type
/// The type variable that represents the value of the opaque type
/// that we require. In other words, after we compile this function,
/// we will be created a constraint like:
///
Expand Down Expand Up @@ -164,12 +164,12 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
/// Here, we have two `impl Trait` types whose values are being
/// inferred (the `impl Bar<'a>` and the `impl
/// Bar<'b>`). Conceptually, this is sugar for a setup where we
/// define underlying abstract types (`Foo1`, `Foo2`) and then, in
/// define underlying opaque types (`Foo1`, `Foo2`) and then, in
/// the return type of `foo`, we *reference* those definitions:
///
/// ```text
/// abstract type Foo1<'x>: Bar<'x>;
/// abstract type Foo2<'x>: Bar<'x>;
/// type Foo1<'x> = impl Bar<'x>;
/// type Foo2<'x> = impl Bar<'x>;
/// fn foo<'a, 'b>(..) -> (Foo1<'a>, Foo2<'b>) { .. }
/// // ^^^^ ^^
/// // | |
Expand Down Expand Up @@ -228,7 +228,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
///
/// This is actually a bit of a tricky constraint in general. We
/// want to say that each variable (e.g., `'0`) can only take on
/// values that were supplied as arguments to the abstract type
/// values that were supplied as arguments to the opaque type
/// (e.g., `'a` for `Foo1<'a>`) or `'static`, which is always in
/// scope. We don't have a constraint quite of this kind in the current
/// region checker.
Expand Down Expand Up @@ -279,10 +279,10 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
/// # The `free_region_relations` parameter
///
/// The `free_region_relations` argument is used to find the
/// "minimum" of the regions supplied to a given abstract type.
/// "minimum" of the regions supplied to a given opaque type.
/// It must be a relation that can answer whether `'a <= 'b`,
/// where `'a` and `'b` are regions that appear in the "substs"
/// for the abstract type references (the `<'a>` in `Foo1<'a>`).
/// for the opaque type references (the `<'a>` in `Foo1<'a>`).
///
/// Note that we do not impose the constraints based on the
/// generic regions from the `Foo1` definition (e.g., `'x`). This
Expand All @@ -298,7 +298,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
///
/// Here, the fact that `'b: 'a` is known only because of the
/// implied bounds from the `&'a &'b u32` parameter, and is not
/// "inherent" to the abstract type definition.
/// "inherent" to the opaque type definition.
///
/// # Parameters
///
Expand Down Expand Up @@ -361,7 +361,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
// There were no `required_region_bounds`,
// so we have to search for a `least_region`.
// Go through all the regions used as arguments to the
// abstract type. These are the parameters to the abstract
// opaque type. These are the parameters to the opaque
// type; so in our example above, `substs` would contain
// `['a]` for the first impl trait and `'b` for the
// second.
Expand Down Expand Up @@ -528,12 +528,12 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {

/// Given the fully resolved, instantiated type for an opaque
/// type, i.e., the value of an inference variable like C1 or C2
/// (*), computes the "definition type" for an abstract type
/// (*), computes the "definition type" for an opaque type
/// definition -- that is, the inferred value of `Foo1<'x>` or
/// `Foo2<'x>` that we would conceptually use in its definition:
///
/// abstract type Foo1<'x>: Bar<'x> = AAA; <-- this type AAA
/// abstract type Foo2<'x>: Bar<'x> = BBB; <-- or this type BBB
/// type Foo1<'x> = impl Bar<'x> = AAA; <-- this type AAA
/// type Foo2<'x> = impl Bar<'x> = BBB; <-- or this type BBB
/// fn foo<'a, 'b>(..) -> (Foo1<'a>, Foo2<'b>) { .. }
///
/// Note that these values are defined in terms of a distinct set of
Expand Down Expand Up @@ -994,15 +994,15 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> {
// value we are inferring. At present, this is
// always true during the first phase of
// type-check, but not always true later on during
// NLL. Once we support named abstract types more fully,
// NLL. Once we support named opaque types more fully,
// this same scenario will be able to arise during all phases.
//
// Here is an example using `abstract type` that indicates
// the distinction we are checking for:
// Here is an example using type alias `impl Trait`
// that indicates the distinction we are checking for:
//
// ```rust
// mod a {
// pub abstract type Foo: Iterator;
// pub type Foo = impl Iterator;
// pub fn make_foo() -> Foo { .. }
// }
//
Expand Down
48 changes: 24 additions & 24 deletions src/librustc/middle/resolve_lifetime.rs
Expand Up @@ -268,17 +268,17 @@ enum Scope<'a> {
track_lifetime_uses: bool,

/// Whether or not this binder would serve as the parent
/// binder for abstract types introduced within. For example:
/// binder for opaque types introduced within. For example:
///
/// fn foo<'a>() -> impl for<'b> Trait<Item = impl Trait2<'a>>
///
/// Here, the abstract types we create for the `impl Trait`
/// Here, the opaque types we create for the `impl Trait`
/// and `impl Trait2` references will both have the `foo` item
/// as their parent. When we get to `impl Trait2`, we find
/// that it is nested within the `for<>` binder -- this flag
/// allows us to skip that when looking for the parent binder
/// of the resulting abstract type.
abstract_type_parent: bool,
/// of the resulting opaque type.
opaque_type_parent: bool,

s: ScopeRef<'a>,
},
Expand Down Expand Up @@ -526,7 +526,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
let scope = Scope::Binder {
lifetimes,
next_early_index: index + non_lifetime_count,
abstract_type_parent: true,
opaque_type_parent: true,
track_lifetime_uses,
s: ROOT_SCOPE,
};
Expand Down Expand Up @@ -574,7 +574,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
s: self.scope,
next_early_index,
track_lifetime_uses: true,
abstract_type_parent: false,
opaque_type_parent: false,
};
self.with(scope, |old_scope, this| {
// a bare fn has no bounds, so everything
Expand Down Expand Up @@ -622,9 +622,9 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
hir::TyKind::Def(item_id, ref lifetimes) => {
// Resolve the lifetimes in the bounds to the lifetime defs in the generics.
// `fn foo<'a>() -> impl MyTrait<'a> { ... }` desugars to
// `abstract type MyAnonTy<'b>: MyTrait<'b>;`
// ^ ^ this gets resolved in the scope of
// the exist_ty generics
// `type MyAnonTy<'b> = impl MyTrait<'b>;`
// ^ ^ this gets resolved in the scope of
// the opaque_ty generics
let (generics, bounds) = match self.tcx.hir().expect_item(item_id.id).node
{
// Named opaque `impl Trait` types are reached via `TyKind::Path`.
Expand Down Expand Up @@ -687,7 +687,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {

// We want to start our early-bound indices at the end of the parent scope,
// not including any parent `impl Trait`s.
let mut index = self.next_early_index_for_abstract_type();
let mut index = self.next_early_index_for_opaque_type();
debug!("visit_ty: index = {}", index);

let mut elision = None;
Expand Down Expand Up @@ -728,7 +728,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
next_early_index,
s: this.scope,
track_lifetime_uses: true,
abstract_type_parent: false,
opaque_type_parent: false,
};
this.with(scope, |_old_scope, this| {
this.visit_generics(generics);
Expand All @@ -743,7 +743,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
next_early_index,
s: self.scope,
track_lifetime_uses: true,
abstract_type_parent: false,
opaque_type_parent: false,
};
self.with(scope, |_old_scope, this| {
this.visit_generics(generics);
Expand Down Expand Up @@ -796,7 +796,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
next_early_index: index + non_lifetime_count,
s: self.scope,
track_lifetime_uses: true,
abstract_type_parent: true,
opaque_type_parent: true,
};
self.with(scope, |_old_scope, this| {
this.visit_generics(generics);
Expand Down Expand Up @@ -848,7 +848,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
next_early_index: index + non_lifetime_count,
s: self.scope,
track_lifetime_uses: true,
abstract_type_parent: true,
opaque_type_parent: true,
};
self.with(scope, |_old_scope, this| {
this.visit_generics(generics);
Expand Down Expand Up @@ -879,7 +879,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
next_early_index,
s: self.scope,
track_lifetime_uses: true,
abstract_type_parent: true,
opaque_type_parent: true,
};
self.with(scope, |_old_scope, this| {
this.visit_generics(generics);
Expand Down Expand Up @@ -967,7 +967,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
s: self.scope,
next_early_index,
track_lifetime_uses: true,
abstract_type_parent: false,
opaque_type_parent: false,
};
let result = self.with(scope, |old_scope, this| {
this.check_lifetime_params(old_scope, &bound_generic_params);
Expand Down Expand Up @@ -1037,7 +1037,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
s: self.scope,
next_early_index,
track_lifetime_uses: true,
abstract_type_parent: false,
opaque_type_parent: false,
};
self.with(scope, |old_scope, this| {
this.check_lifetime_params(old_scope, &trait_ref.bound_generic_params);
Expand Down Expand Up @@ -1753,7 +1753,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
lifetimes,
next_early_index,
s: self.scope,
abstract_type_parent: true,
opaque_type_parent: true,
track_lifetime_uses: false,
};
self.with(scope, move |old_scope, this| {
Expand All @@ -1762,17 +1762,17 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
});
}

fn next_early_index_helper(&self, only_abstract_type_parent: bool) -> u32 {
fn next_early_index_helper(&self, only_opaque_type_parent: bool) -> u32 {
let mut scope = self.scope;
loop {
match *scope {
Scope::Root => return 0,

Scope::Binder {
next_early_index,
abstract_type_parent,
opaque_type_parent,
..
} if (!only_abstract_type_parent || abstract_type_parent) =>
} if (!only_opaque_type_parent || opaque_type_parent) =>
{
return next_early_index
}
Expand All @@ -1792,10 +1792,10 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
}

/// Returns the next index one would use for an `impl Trait` that
/// is being converted into an `abstract type`. This will be the
/// is being converted into an opaque type alias `impl Trait`. This will be the
/// next early index from the enclosing item, for the most
/// part. See the `abstract_type_parent` field for more info.
fn next_early_index_for_abstract_type(&self) -> u32 {
/// part. See the `opaque_type_parent` field for more info.
fn next_early_index_for_opaque_type(&self) -> u32 {
self.next_early_index_helper(false)
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/rustdoc-ui/coverage/traits.rs
Expand Up @@ -31,7 +31,7 @@ pub trait MyAlias = ThisTrait + Send + Sync;

// FIXME(58624): once rustdoc can process opaque `impl Trait` types,
// we need to make sure they're counted
// /// woah, getting all abstract in here
// /// woah, getting all opaque in here
// pub type ThisExists = impl ThisTrait;
//
// /// why don't we get a little more concrete
Expand Down

0 comments on commit 2777386

Please sign in to comment.