Skip to content

Commit 0320ee1

Browse files
Deduce closure signature from supertrait projection bounds
1 parent 6345cb0 commit 0320ee1

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

Diff for: compiler/rustc_hir_typeck/src/closure.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_span::def_id::LocalDefId;
1818
use rustc_span::{DUMMY_SP, Span};
1919
use rustc_trait_selection::error_reporting::traits::ArgKind;
2020
use rustc_trait_selection::traits;
21-
use rustc_type_ir::ClosureKind;
21+
use rustc_type_ir::{ClosureKind, Upcast as _};
2222
use tracing::{debug, instrument, trace};
2323

2424
use super::{CoroutineTypes, Expectation, FnCtxt, check_fn};
@@ -312,16 +312,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
312312
.iter_instantiated_copied(self.tcx, args)
313313
.map(|(c, s)| (c.as_predicate(), s)),
314314
),
315-
ty::Dynamic(object_type, ..) => {
316-
let sig = object_type.projection_bounds().find_map(|pb| {
317-
let pb = pb.with_self_ty(self.tcx, self.tcx.types.trait_object_dummy_self);
318-
self.deduce_sig_from_projection(None, closure_kind, pb)
319-
});
320-
let kind = object_type
321-
.principal_def_id()
322-
.and_then(|did| self.tcx.fn_trait_kind_from_def_id(did));
323-
(sig, kind)
324-
}
315+
ty::Dynamic(data, ..) => self.deduce_closure_signature_from_predicates(
316+
expected_ty,
317+
closure_kind,
318+
data.iter().map(|bound| {
319+
(
320+
bound
321+
.with_self_ty(self.tcx, self.tcx.types.trait_object_dummy_self)
322+
.upcast(self.tcx),
323+
DUMMY_SP,
324+
)
325+
}),
326+
),
325327
ty::Infer(ty::TyVar(vid)) => self.deduce_closure_signature_from_predicates(
326328
Ty::new_var(self.tcx, self.root_var(vid)),
327329
closure_kind,

Diff for: tests/ui/closures/deduce-from-object-supertrait.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//@ check-pass
2+
3+
trait Foo: Fn(Bar) {}
4+
impl<T> Foo for T where T: Fn(Bar) {}
5+
6+
struct Bar;
7+
impl Bar {
8+
fn bar(&self) {}
9+
}
10+
11+
fn main() {
12+
let x: &dyn Foo = &|x| {
13+
x.bar();
14+
};
15+
}

0 commit comments

Comments
 (0)