about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-03-27 23:27:23 +0100
committerGitHub <noreply@github.com>2024-03-27 23:27:23 +0100
commit15fb2f214c407255225f5efc648460b64b4adcc1 (patch)
tree8726c1ff8b8716f53dd5856eeba15f90965013bd
parentae33b9045b05780c50db1da3c898bea52883ce8e (diff)
parent86e750f0f777d78a108fa224731ef163b35f2309 (diff)
downloadrust-15fb2f214c407255225f5efc648460b64b4adcc1.tar.gz
rust-15fb2f214c407255225f5efc648460b64b4adcc1.zip
Rollup merge of #123130 - oli-obk:missing_type_taint, r=compiler-errors
Load missing type of impl associated constant from trait definition

fixes #123092

Also does some cleanups I discovered while analyzing this issue
-rw-r--r--compiler/rustc_hir_typeck/src/lib.rs110
-rw-r--r--tests/ui/consts/missing_assoc_const_type.rs28
-rw-r--r--tests/ui/consts/missing_assoc_const_type.stderr8
-rw-r--r--tests/ui/consts/missing_assoc_const_type2.rs21
-rw-r--r--tests/ui/consts/missing_assoc_const_type2.stderr8
-rw-r--r--tests/ui/typeck/typeck_type_placeholder_item.stderr23
6 files changed, 132 insertions, 66 deletions
diff --git a/compiler/rustc_hir_typeck/src/lib.rs b/compiler/rustc_hir_typeck/src/lib.rs
index 3a16884d5c7..80fd4be53e1 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".
@@ -106,7 +92,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
     }
@@ -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(&root_ctxt, 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,42 +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 = if let Some(&hir::Ty { kind: hir::TyKind::Infer, span, .. }) = body_ty {
-            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(&fcx, node);
         let expected_type = expected_type.unwrap_or_else(fallback);
 
         let expected_type = fcx.normalize(body.value.span, expected_type);
@@ -296,6 +247,59 @@ fn typeck_with_fallback<'tcx>(
     typeck_results
 }
 
+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, .. }) = node.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.
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 00000000000..8d95e3dca63
--- /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 00000000000..28af1f0f321
--- /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 00000000000..baf236700a3
--- /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 00000000000..1255ca2d102
--- /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 8bcad56916a..7977504dae1 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