Skip to content

Commit 41b0315

Browse files
committed
More documentation
1 parent 0f05b4b commit 41b0315

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

src/librustc/infer/anon_types/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,23 @@ impl<'a, 'gcx, 'tcx> Instantiator<'a, 'gcx, 'tcx> {
808808
}
809809

810810
/// Whether `anon_node_id` is a sibling or a child of a sibling of `def_id`
811+
///
812+
/// ```rust
813+
/// pub mod foo {
814+
/// pub mod bar {
815+
/// pub existential type Baz;
816+
///
817+
/// fn f1() -> Baz { .. }
818+
/// }
819+
///
820+
/// fn f2() -> bar::Baz { .. }
821+
/// }
822+
/// ```
823+
///
824+
/// Here, `def_id` will be the `DefId` of the existential type `Baz`.
825+
/// `anon_node_id` is the `NodeId` of the reference to Baz -- so either the return type of f1 or f2.
826+
/// We will return true if the reference is within the same module as the existential type
827+
/// So true for f1, false for f2.
811828
pub fn may_define_existential_type(
812829
tcx: TyCtxt,
813830
def_id: DefId,

src/librustc/ty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2861,7 +2861,7 @@ fn param_env<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
28612861
def_id: DefId)
28622862
-> ParamEnv<'tcx> {
28632863

2864-
// The param_env of an existential type is its parent's param_env
2864+
// The param_env of an impl Trait type is its defining function's param_env
28652865
if let Some(Def::Existential(_)) = tcx.describe_def(def_id) {
28662866
if let Some(node_id) = tcx.hir.as_local_node_id(def_id) {
28672867
if let hir::map::NodeItem(item) = tcx.hir.get(node_id) {

src/librustc_typeck/check/wfcheck.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,26 @@ fn check_fn_or_method<'a, 'fcx, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'gcx>,
535535
check_where_clauses(tcx, fcx, span, def_id, Some(sig.output()));
536536
}
537537

538+
/// Checks "defining uses" of existential types to ensure that they meet the restrictions laid for
539+
/// "higher-order pattern unification".
540+
/// This ensures that inference is tractable.
541+
/// In particular, definitions of existential types can only use other generics as arguments,
542+
/// and they cannot repeat an argument. Example:
543+
///
544+
/// ```rust
545+
/// existential type Foo<A, B>;
546+
///
547+
/// // ok -- `Foo` is applied to two distinct, generic types.
548+
/// fn a<T, U>() -> Foo<T, U> { .. }
549+
///
550+
/// // not ok -- `Foo` is applied to `T` twice.
551+
/// fn b<T>() -> Foo<T, T> { .. }
552+
///
553+
///
554+
/// // not ok -- `Foo` is applied to a non-generic type.
555+
/// fn b<T>() -> Foo<T, u32> { .. }
556+
/// ```
557+
///
538558
fn check_existential_types<'a, 'fcx, 'gcx, 'tcx>(
539559
tcx: TyCtxt<'a, 'gcx, 'gcx>,
540560
fcx: &FnCtxt<'fcx, 'gcx, 'tcx>,

0 commit comments

Comments
 (0)