From 804c0476575e891aa47131d67cbb3b6c21682abf Mon Sep 17 00:00:00 2001
From: Oli Scherer <git-spam-no-reply9815368754983@oli-obk.de>
Date: Wed, 27 Mar 2024 10:56:11 +0000
Subject: [PATCH 1/4] Load missing type of impl associated constant from trait
 definition

---
 compiler/rustc_hir_typeck/src/lib.rs          | 18 +++++++++---
 tests/ui/consts/missing_assoc_const_type.rs   | 28 +++++++++++++++++++
 .../ui/consts/missing_assoc_const_type.stderr |  8 ++++++
 tests/ui/consts/missing_assoc_const_type2.rs  | 21 ++++++++++++++
 .../consts/missing_assoc_const_type2.stderr   |  8 ++++++
 .../typeck_type_placeholder_item.stderr       | 23 +++++++--------
 6 files changed, 89 insertions(+), 17 deletions(-)
 create mode 100644 tests/ui/consts/missing_assoc_const_type.rs
 create mode 100644 tests/ui/consts/missing_assoc_const_type.stderr
 create mode 100644 tests/ui/consts/missing_assoc_const_type2.rs
 create mode 100644 tests/ui/consts/missing_assoc_const_type2.stderr

diff --git a/compiler/rustc_hir_typeck/src/lib.rs b/compiler/rustc_hir_typeck/src/lib.rs
index 0b67b37df29d8..f201bfddf4bb1 100644
--- a/compiler/rustc_hir_typeck/src/lib.rs
+++ b/compiler/rustc_hir_typeck/src/lib.rs
@@ -192,10 +192,20 @@ fn typeck_with_fallback<'tcx>(
         check_fn(&mut fcx, fn_sig, None, decl, def_id, body, tcx.features().unsized_fn_params);
     } else {
         let expected_type = if let Some(&hir::Ty { kind: hir::TyKind::Infer, span, .. }) = body_ty {
-            Some(fcx.next_ty_var(TypeVariableOrigin {
-                kind: TypeVariableOriginKind::TypeInference,
-                span,
-            }))
+            if let Some(item) = tcx.opt_associated_item(def_id.into())
+                && let ty::AssocKind::Const = item.kind
+                && let ty::ImplContainer = item.container
+                && let Some(trait_item) = item.trait_item_def_id
+            {
+                let args =
+                    tcx.impl_trait_ref(item.container_id(tcx)).unwrap().instantiate_identity().args;
+                Some(tcx.type_of(trait_item).instantiate(tcx, args))
+            } else {
+                Some(fcx.next_ty_var(TypeVariableOrigin {
+                    kind: TypeVariableOriginKind::TypeInference,
+                    span,
+                }))
+            }
         } else if let Node::AnonConst(_) = node {
             match tcx.parent_hir_node(id) {
                 Node::Ty(&hir::Ty { kind: hir::TyKind::Typeof(ref anon_const), .. })
diff --git a/tests/ui/consts/missing_assoc_const_type.rs b/tests/ui/consts/missing_assoc_const_type.rs
new file mode 100644
index 0000000000000..8d95e3dca633c
--- /dev/null
+++ b/tests/ui/consts/missing_assoc_const_type.rs
@@ -0,0 +1,28 @@
+//! Test that we compute the right type for associated constants
+//! of impls, even if the type is missing. We know it from the trait
+//! declaration after all.
+
+trait Range {
+    const FIRST: u8;
+    const LAST: u8;
+}
+
+struct TwoDigits;
+impl Range for TwoDigits {
+    const FIRST:  = 10;
+    //~^ ERROR: missing type for `const` item
+    const LAST: u8 = 99;
+}
+
+const fn digits(x: u8) -> usize {
+    match x {
+        TwoDigits::FIRST..=TwoDigits::LAST => 0,
+        0..=9 | 100..=255 => panic!(),
+    }
+}
+
+const FOOMP: [(); {
+    digits(42)
+}] = [];
+
+fn main() {}
diff --git a/tests/ui/consts/missing_assoc_const_type.stderr b/tests/ui/consts/missing_assoc_const_type.stderr
new file mode 100644
index 0000000000000..28af1f0f321e6
--- /dev/null
+++ b/tests/ui/consts/missing_assoc_const_type.stderr
@@ -0,0 +1,8 @@
+error: missing type for `const` item
+  --> $DIR/missing_assoc_const_type.rs:12:17
+   |
+LL |     const FIRST:  = 10;
+   |                 ^ help: provide a type for the associated constant: `u8`
+
+error: aborting due to 1 previous error
+
diff --git a/tests/ui/consts/missing_assoc_const_type2.rs b/tests/ui/consts/missing_assoc_const_type2.rs
new file mode 100644
index 0000000000000..baf236700a396
--- /dev/null
+++ b/tests/ui/consts/missing_assoc_const_type2.rs
@@ -0,0 +1,21 @@
+//! Test that we compute the right type for associated constants
+//! of impls, even if the type is missing. We know it from the trait
+//! declaration after all.
+
+trait Range {
+    const FIRST: u8;
+    const LAST: u8;
+}
+
+struct TwoDigits;
+impl Range for TwoDigits {
+    const FIRST:  = 10;
+    //~^ ERROR: missing type
+    const LAST: u8 = 99;
+}
+
+const FOOMP: [(); {
+    TwoDigits::FIRST as usize
+}] = [(); 10];
+
+fn main() {}
diff --git a/tests/ui/consts/missing_assoc_const_type2.stderr b/tests/ui/consts/missing_assoc_const_type2.stderr
new file mode 100644
index 0000000000000..1255ca2d102b5
--- /dev/null
+++ b/tests/ui/consts/missing_assoc_const_type2.stderr
@@ -0,0 +1,8 @@
+error: missing type for `const` item
+  --> $DIR/missing_assoc_const_type2.rs:12:17
+   |
+LL |     const FIRST:  = 10;
+   |                 ^ help: provide a type for the associated constant: `u8`
+
+error: aborting due to 1 previous error
+
diff --git a/tests/ui/typeck/typeck_type_placeholder_item.stderr b/tests/ui/typeck/typeck_type_placeholder_item.stderr
index 8bcad56916aa9..7977504dae1d6 100644
--- a/tests/ui/typeck/typeck_type_placeholder_item.stderr
+++ b/tests/ui/typeck/typeck_type_placeholder_item.stderr
@@ -546,6 +546,15 @@ help: use type parameters instead
 LL |         fn fn_test10<T>(&self, _x : T) { }
    |                     +++             ~
 
+error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated constants
+  --> $DIR/typeck_type_placeholder_item.rs:194:14
+   |
+LL |     const D: _ = 42;
+   |              ^
+   |              |
+   |              not allowed in type signatures
+   |              help: replace with the correct type: `i32`
+
 error[E0121]: the placeholder `_` is not allowed within types on item signatures for return types
   --> $DIR/typeck_type_placeholder_item.rs:217:31
    |
@@ -574,19 +583,7 @@ error[E0121]: the placeholder `_` is not allowed within types on item signatures
   --> $DIR/typeck_type_placeholder_item.rs:209:14
    |
 LL |     const D: _ = 42;
-   |              ^
-   |              |
-   |              not allowed in type signatures
-   |              help: replace with the correct type: `i32`
-
-error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated constants
-  --> $DIR/typeck_type_placeholder_item.rs:194:14
-   |
-LL |     const D: _ = 42;
-   |              ^
-   |              |
-   |              not allowed in type signatures
-   |              help: replace with the correct type: `i32`
+   |              ^ not allowed in type signatures
 
 error[E0046]: not all trait items implemented, missing: `F`
   --> $DIR/typeck_type_placeholder_item.rs:200:1

From d0eb9c86f1ec1d2f6c2c5c09abb31d2b3d3e7069 Mon Sep 17 00:00:00 2001
From: Oli Scherer <git-spam-no-reply9815368754983@oli-obk.de>
Date: Wed, 27 Mar 2024 11:12:21 +0000
Subject: [PATCH 2/4] move type inference for missing types on constants into
 its own method

---
 compiler/rustc_hir_typeck/src/lib.rs | 104 +++++++++++++++------------
 1 file changed, 58 insertions(+), 46 deletions(-)

diff --git a/compiler/rustc_hir_typeck/src/lib.rs b/compiler/rustc_hir_typeck/src/lib.rs
index f201bfddf4bb1..55e86bbec1c48 100644
--- a/compiler/rustc_hir_typeck/src/lib.rs
+++ b/compiler/rustc_hir_typeck/src/lib.rs
@@ -191,52 +191,7 @@ fn typeck_with_fallback<'tcx>(
 
         check_fn(&mut fcx, fn_sig, None, decl, def_id, body, tcx.features().unsized_fn_params);
     } else {
-        let expected_type = if let Some(&hir::Ty { kind: hir::TyKind::Infer, span, .. }) = body_ty {
-            if let Some(item) = tcx.opt_associated_item(def_id.into())
-                && let ty::AssocKind::Const = item.kind
-                && let ty::ImplContainer = item.container
-                && let Some(trait_item) = item.trait_item_def_id
-            {
-                let args =
-                    tcx.impl_trait_ref(item.container_id(tcx)).unwrap().instantiate_identity().args;
-                Some(tcx.type_of(trait_item).instantiate(tcx, args))
-            } else {
-                Some(fcx.next_ty_var(TypeVariableOrigin {
-                    kind: TypeVariableOriginKind::TypeInference,
-                    span,
-                }))
-            }
-        } else if let Node::AnonConst(_) = node {
-            match tcx.parent_hir_node(id) {
-                Node::Ty(&hir::Ty { kind: hir::TyKind::Typeof(ref anon_const), .. })
-                    if anon_const.hir_id == id =>
-                {
-                    Some(fcx.next_ty_var(TypeVariableOrigin {
-                        kind: TypeVariableOriginKind::TypeInference,
-                        span,
-                    }))
-                }
-                Node::Expr(&hir::Expr { kind: hir::ExprKind::InlineAsm(asm), .. })
-                | Node::Item(&hir::Item { kind: hir::ItemKind::GlobalAsm(asm), .. }) => {
-                    asm.operands.iter().find_map(|(op, _op_sp)| match op {
-                        hir::InlineAsmOperand::Const { anon_const } if anon_const.hir_id == id => {
-                            // Inline assembly constants must be integers.
-                            Some(fcx.next_int_var())
-                        }
-                        hir::InlineAsmOperand::SymFn { anon_const } if anon_const.hir_id == id => {
-                            Some(fcx.next_ty_var(TypeVariableOrigin {
-                                kind: TypeVariableOriginKind::MiscVariable,
-                                span,
-                            }))
-                        }
-                        _ => None,
-                    })
-                }
-                _ => None,
-            }
-        } else {
-            None
-        };
+        let expected_type = infer_type_if_missing(body_ty, &fcx, node);
         let expected_type = expected_type.unwrap_or_else(fallback);
 
         let expected_type = fcx.normalize(body.value.span, expected_type);
@@ -306,6 +261,63 @@ fn typeck_with_fallback<'tcx>(
     typeck_results
 }
 
+fn infer_type_if_missing<'tcx>(
+    body_ty: Option<&hir::Ty<'tcx>>,
+    fcx: &FnCtxt<'_, 'tcx>,
+    node: Node<'tcx>,
+) -> Option<Ty<'tcx>> {
+    let tcx = fcx.tcx;
+    let def_id = fcx.body_id;
+    let expected_type = if let Some(&hir::Ty { kind: hir::TyKind::Infer, span, .. }) = body_ty {
+        if let Some(item) = tcx.opt_associated_item(def_id.into())
+            && let ty::AssocKind::Const = item.kind
+            && let ty::ImplContainer = item.container
+            && let Some(trait_item) = item.trait_item_def_id
+        {
+            let args =
+                tcx.impl_trait_ref(item.container_id(tcx)).unwrap().instantiate_identity().args;
+            Some(tcx.type_of(trait_item).instantiate(tcx, args))
+        } else {
+            Some(fcx.next_ty_var(TypeVariableOrigin {
+                kind: TypeVariableOriginKind::TypeInference,
+                span,
+            }))
+        }
+    } else if let Node::AnonConst(_) = node {
+        let id = tcx.local_def_id_to_hir_id(def_id);
+        match tcx.parent_hir_node(id) {
+            Node::Ty(&hir::Ty { kind: hir::TyKind::Typeof(ref anon_const), span, .. })
+                if anon_const.hir_id == id =>
+            {
+                Some(fcx.next_ty_var(TypeVariableOrigin {
+                    kind: TypeVariableOriginKind::TypeInference,
+                    span,
+                }))
+            }
+            Node::Expr(&hir::Expr { kind: hir::ExprKind::InlineAsm(asm), span, .. })
+            | Node::Item(&hir::Item { kind: hir::ItemKind::GlobalAsm(asm), span, .. }) => {
+                asm.operands.iter().find_map(|(op, _op_sp)| match op {
+                    hir::InlineAsmOperand::Const { anon_const } if anon_const.hir_id == id => {
+                        // Inline assembly constants must be integers.
+                        Some(fcx.next_int_var())
+                    }
+                    hir::InlineAsmOperand::SymFn { anon_const } if anon_const.hir_id == id => {
+                        Some(fcx.next_ty_var(TypeVariableOrigin {
+                            kind: TypeVariableOriginKind::MiscVariable,
+                            span,
+                        }))
+                    }
+                    _ => None,
+                })
+            }
+            _ => None,
+        }
+    } else {
+        None
+    };
+    expected_type
+}
+
 /// When `check_fn` is invoked on a coroutine (i.e., a body that
 /// includes yield), it returns back some information about the yield
 /// points.

From 7786ee3c9240084ddb2b62d3f78cffce61462199 Mon Sep 17 00:00:00 2001
From: Oli Scherer <git-spam-no-reply9815368754983@oli-obk.de>
Date: Wed, 27 Mar 2024 11:13:34 +0000
Subject: [PATCH 3/4] Remove a call-site to `primary_body_of` as it is only
 interested in the body id

---
 compiler/rustc_hir_typeck/src/lib.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/compiler/rustc_hir_typeck/src/lib.rs b/compiler/rustc_hir_typeck/src/lib.rs
index 55e86bbec1c48..c135389ec9861 100644
--- a/compiler/rustc_hir_typeck/src/lib.rs
+++ b/compiler/rustc_hir_typeck/src/lib.rs
@@ -106,7 +106,7 @@ fn has_typeck_results(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
     }
 
     if let Some(def_id) = def_id.as_local() {
-        primary_body_of(tcx.hir_node_by_def_id(def_id)).is_some()
+        tcx.hir_node_by_def_id(def_id).body_id().is_some()
     } else {
         false
     }

From 86e750f0f777d78a108fa224731ef163b35f2309 Mon Sep 17 00:00:00 2001
From: Oli Scherer <git-spam-no-reply9815368754983@oli-obk.de>
Date: Wed, 27 Mar 2024 11:16:41 +0000
Subject: [PATCH 4/4] Inline `primary_body_of` into its sole call site

---
 compiler/rustc_hir_typeck/src/lib.rs | 28 +++++-----------------------
 1 file changed, 5 insertions(+), 23 deletions(-)

diff --git a/compiler/rustc_hir_typeck/src/lib.rs b/compiler/rustc_hir_typeck/src/lib.rs
index c135389ec9861..00d3e262cc891 100644
--- a/compiler/rustc_hir_typeck/src/lib.rs
+++ b/compiler/rustc_hir_typeck/src/lib.rs
@@ -83,20 +83,6 @@ macro_rules! type_error_struct {
     })
 }
 
-/// If this `DefId` is a "primary tables entry", returns
-/// `Some((body_id, body_ty, fn_sig))`. Otherwise, returns `None`.
-///
-/// If this function returns `Some`, then `typeck_results(def_id)` will
-/// succeed; if it returns `None`, then `typeck_results(def_id)` may or
-/// may not succeed. In some cases where this function returns `None`
-/// (notably closures), `typeck_results(def_id)` would wind up
-/// redirecting to the owning function.
-fn primary_body_of(
-    node: Node<'_>,
-) -> Option<(hir::BodyId, Option<&hir::Ty<'_>>, Option<&hir::FnSig<'_>>)> {
-    Some((node.body_id()?, node.ty(), node.fn_sig()))
-}
-
 fn has_typeck_results(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
     // Closures' typeck results come from their outermost function,
     // as they are part of the same "inference environment".
@@ -163,7 +149,7 @@ fn typeck_with_fallback<'tcx>(
     let span = tcx.hir().span(id);
 
     // Figure out what primary body this item has.
-    let (body_id, body_ty, fn_sig) = primary_body_of(node).unwrap_or_else(|| {
+    let body_id = node.body_id().unwrap_or_else(|| {
         span_bug!(span, "can't type-check body of {:?}", def_id);
     });
     let body = tcx.hir().body(body_id);
@@ -176,7 +162,7 @@ fn typeck_with_fallback<'tcx>(
     }
     let mut fcx = FnCtxt::new(&inh, param_env, def_id);
 
-    if let Some(hir::FnSig { header, decl, .. }) = fn_sig {
+    if let Some(hir::FnSig { header, decl, .. }) = node.fn_sig() {
         let fn_sig = if decl.output.get_infer_ret_ty().is_some() {
             fcx.lowerer().lower_fn_ty(id, header.unsafety, header.abi, decl, None, None)
         } else {
@@ -191,7 +177,7 @@ fn typeck_with_fallback<'tcx>(
 
         check_fn(&mut fcx, fn_sig, None, decl, def_id, body, tcx.features().unsized_fn_params);
     } else {
-        let expected_type = infer_type_if_missing(body_ty, &fcx, node);
+        let expected_type = infer_type_if_missing(&fcx, node);
         let expected_type = expected_type.unwrap_or_else(fallback);
 
         let expected_type = fcx.normalize(body.value.span, expected_type);
@@ -261,14 +247,10 @@ fn typeck_with_fallback<'tcx>(
     typeck_results
 }
 
-fn infer_type_if_missing<'tcx>(
-    body_ty: Option<&hir::Ty<'tcx>>,
-    fcx: &FnCtxt<'_, 'tcx>,
-    node: Node<'tcx>,
-) -> Option<Ty<'tcx>> {
+fn infer_type_if_missing<'tcx>(fcx: &FnCtxt<'_, 'tcx>, node: Node<'tcx>) -> Option<Ty<'tcx>> {
     let tcx = fcx.tcx;
     let def_id = fcx.body_id;
-    let expected_type = if let Some(&hir::Ty { kind: hir::TyKind::Infer, span, .. }) = body_ty {
+    let expected_type = if let Some(&hir::Ty { kind: hir::TyKind::Infer, span, .. }) = node.ty() {
         if let Some(item) = tcx.opt_associated_item(def_id.into())
             && let ty::AssocKind::Const = item.kind
             && let ty::ImplContainer = item.container