Skip to content

Commit 4f549fe

Browse files
committed
improve comment about instantiating anon types
1 parent 3f490ca commit 4f549fe

File tree

1 file changed

+34
-6
lines changed
  • src/librustc/infer/anon_types

1 file changed

+34
-6
lines changed

src/librustc/infer/anon_types/mod.rs

+34-6
Original file line numberDiff line numberDiff line change
@@ -509,12 +509,40 @@ impl<'a, 'gcx, 'tcx> Instantiator<'a, 'gcx, 'tcx> {
509509
tcx,
510510
fldop: |ty| {
511511
if let ty::TyAnon(def_id, substs) = ty.sty {
512-
// Check that this is `impl Trait` type is declared by
513-
// `parent_def_id`. During the first phase of type-check, this
514-
// is true, but during NLL type-check, we sometimes encounter
515-
// `impl Trait` types in e.g. inferred closure signatures that
516-
// are not 'local' to the current function and hence which
517-
// ought not to be instantiated.
512+
// Check that this is `impl Trait` type is
513+
// declared by `parent_def_id` -- i.e., one whose
514+
// value we are inferring. At present, this is
515+
// always true during the first phase of
516+
// type-check, but not always true later on during
517+
// NLL. Once we support named abstract types more fully,
518+
// this same scenario will be able to arise during all phases.
519+
//
520+
// Here is an example using `abstract type` that indicates
521+
// the distinction we are checking for:
522+
//
523+
// ```rust
524+
// mod a {
525+
// pub abstract type Foo: Iterator;
526+
// pub fn make_foo() -> Foo { .. }
527+
// }
528+
//
529+
// mod b {
530+
// fn foo() -> a::Foo { a::make_foo() }
531+
// }
532+
// ```
533+
//
534+
// Here, the return type of `foo` references a
535+
// `TyAnon` indeed, but not one whose value is
536+
// presently being inferred. You can get into a
537+
// similar situation with closure return types
538+
// today:
539+
//
540+
// ```rust
541+
// fn foo() -> impl Iterator { .. }
542+
// fn bar() {
543+
// let x = || foo(); // returns the Anon assoc with `foo`
544+
// }
545+
// ```
518546
if let Some(anon_node_id) = tcx.hir.as_local_node_id(def_id) {
519547
let anon_parent_node_id = tcx.hir.get_parent(anon_node_id);
520548
let anon_parent_def_id = tcx.hir.local_def_id(anon_parent_node_id);

0 commit comments

Comments
 (0)