about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2022-12-03 17:37:45 +0100
committerGitHub <noreply@github.com>2022-12-03 17:37:45 +0100
commitf91fa512d1beb080fa9a15026d6acb3f4d4729b7 (patch)
tree53a6f69556ca618bc1cccabe8e678d267330e2d2
parentb1e680650e93f6ac93bde358bbbc5e5e4122b342 (diff)
parente973240d18f131d4fe48c4920f92a044c7503299 (diff)
downloadrust-f91fa512d1beb080fa9a15026d6acb3f4d4729b7.tar.gz
rust-f91fa512d1beb080fa9a15026d6acb3f4d4729b7.zip
Rollup merge of #105201 - cjgillot:issue-105040, r=compiler-errors
Do not call fn_sig on non-functions.

Fixes https://github.com/rust-lang/rust/issues/105040
Fixes https://github.com/rust-lang/rust/issues/89271
-rw-r--r--compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs8
-rw-r--r--src/test/ui/suggestions/assoc-const-as-fn.rs18
-rw-r--r--src/test/ui/suggestions/assoc-const-as-fn.stderr14
3 files changed, 40 insertions, 0 deletions
diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
index 86384c7b93e..3078e0cbeda 100644
--- a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
+++ b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
@@ -1918,6 +1918,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
         receiver: Option<&'tcx hir::Expr<'tcx>>,
         args: &'tcx [hir::Expr<'tcx>],
     ) -> bool {
+        // Do not call `fn_sig` on non-functions.
+        if !matches!(
+            self.tcx.def_kind(def_id),
+            DefKind::Fn | DefKind::AssocFn | DefKind::Variant | DefKind::Ctor(..)
+        ) {
+            return false;
+        }
+
         let sig = self.tcx.fn_sig(def_id).skip_binder();
         let args_referencing_param: Vec<_> = sig
             .inputs()
diff --git a/src/test/ui/suggestions/assoc-const-as-fn.rs b/src/test/ui/suggestions/assoc-const-as-fn.rs
new file mode 100644
index 00000000000..4b4595dd5e6
--- /dev/null
+++ b/src/test/ui/suggestions/assoc-const-as-fn.rs
@@ -0,0 +1,18 @@
+unsafe fn pointer(v: usize, w: u32) {}
+
+pub trait UniformScalar {}
+impl UniformScalar for u32 {}
+
+pub trait GlUniformScalar: UniformScalar {
+    const FACTORY: unsafe fn(usize, Self) -> ();
+}
+impl GlUniformScalar for u32 {
+    const FACTORY: unsafe fn(usize, Self) -> () = pointer;
+}
+
+pub fn foo<T: UniformScalar>(value: T) {
+    <T as GlUniformScalar>::FACTORY(1, value);
+    //~^ ERROR the trait bound `T: GlUniformScalar` is not satisfied
+}
+
+fn main() {}
diff --git a/src/test/ui/suggestions/assoc-const-as-fn.stderr b/src/test/ui/suggestions/assoc-const-as-fn.stderr
new file mode 100644
index 00000000000..fa740687858
--- /dev/null
+++ b/src/test/ui/suggestions/assoc-const-as-fn.stderr
@@ -0,0 +1,14 @@
+error[E0277]: the trait bound `T: GlUniformScalar` is not satisfied
+  --> $DIR/assoc-const-as-fn.rs:14:5
+   |
+LL |     <T as GlUniformScalar>::FACTORY(1, value);
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `GlUniformScalar` is not implemented for `T`
+   |
+help: consider further restricting this bound
+   |
+LL | pub fn foo<T: UniformScalar + GlUniformScalar>(value: T) {
+   |                             +++++++++++++++++
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0277`.