From ffad636048734fd3a6d38ab003406664476ca158 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Le=C3=B3n=20Orell=20Valerian=20Liehr?= Date: Wed, 22 Feb 2023 16:43:52 +0100 Subject: [PATCH] Resolve assoc fns & consts defined on fn ptr tys --- compiler/rustc_hir_typeck/src/method/probe.rs | 1 + .../associated-item-on-function-pointer.rs | 34 +++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 tests/ui/associated-item/associated-item-on-function-pointer.rs diff --git a/compiler/rustc_hir_typeck/src/method/probe.rs b/compiler/rustc_hir_typeck/src/method/probe.rs index 8b0473b7454c0..54ff6aeb2f35c 100644 --- a/compiler/rustc_hir_typeck/src/method/probe.rs +++ b/compiler/rustc_hir_typeck/src/method/probe.rs @@ -692,6 +692,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { | ty::Slice(_) | ty::RawPtr(_) | ty::Ref(..) + | ty::FnPtr(_) | ty::Never | ty::Tuple(..) => self.assemble_inherent_candidates_for_incoherent_ty(raw_self_ty), _ => {} diff --git a/tests/ui/associated-item/associated-item-on-function-pointer.rs b/tests/ui/associated-item/associated-item-on-function-pointer.rs new file mode 100644 index 0000000000000..30ef580df4c0b --- /dev/null +++ b/tests/ui/associated-item/associated-item-on-function-pointer.rs @@ -0,0 +1,34 @@ +// Check that we successfully resolve associated functions and constants defined on +// function pointer types. Regression test for issue #108270. + +// check-pass + +#![feature(rustc_attrs)] +#![rustc_coherence_is_core] + +impl fn() { + const ARITY: usize = 0; + + fn handle() {} + + fn apply(self) { + self() + } +} + +impl for<'src> fn(&'src str) -> bool { + fn execute(self, source: &'static str) -> bool { + self(source) + } +} + +fn main() { + let _: usize = ::ARITY; + ::handle(); + + let f: fn() = main; + f.apply(); + + let predicate: fn(&str) -> bool = |source| !source.is_empty(); + let _ = predicate.execute("..."); +}