From 2db0492e6264d22629ed75b60f8c0dcebc451966 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Mon, 12 Sep 2022 04:20:26 +0000 Subject: [PATCH] Normalize closure signature after construction --- compiler/rustc_typeck/src/check/closure.rs | 3 ++ src/test/ui/closures/issue-101696.rs | 36 ++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 src/test/ui/closures/issue-101696.rs diff --git a/compiler/rustc_typeck/src/check/closure.rs b/compiler/rustc_typeck/src/check/closure.rs index 55cbaf71e7c..893227d3e7d 100644 --- a/compiler/rustc_typeck/src/check/closure.rs +++ b/compiler/rustc_typeck/src/check/closure.rs @@ -624,6 +624,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ), bound_vars, ); + // Astconv can't normalize inputs or outputs with escaping bound vars, + // so normalize them here, after we've wrapped them in a binder. + let result = self.normalize_associated_types_in(self.tcx.hir().span(hir_id), result); let c_result = self.inh.infcx.canonicalize_response(result); self.typeck_results.borrow_mut().user_provided_sigs.insert(expr_def_id, c_result); diff --git a/src/test/ui/closures/issue-101696.rs b/src/test/ui/closures/issue-101696.rs new file mode 100644 index 00000000000..0a358bd1643 --- /dev/null +++ b/src/test/ui/closures/issue-101696.rs @@ -0,0 +1,36 @@ +// check-pass + +use std::marker::PhantomData; + +#[derive(Default)] +struct MyType<'a> { + field: usize, + _phantom: PhantomData<&'a ()>, +} + +#[derive(Default)] +struct MyTypeVariant<'a> { + field: usize, + _phantom: PhantomData<&'a ()>, +} + +trait AsVariantTrait { + type Type; +} + +impl<'a> AsVariantTrait for MyType<'a> { + type Type = MyTypeVariant<'a>; +} + +type Variant = ::Type; + +fn foo(f: F) { + let input = T::default(); + f(input); +} + +fn main() { + foo(|a: ::Type| { + a.field; + }); +}