about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBoxy <rust@boxyuwu.dev>2024-12-03 00:00:48 +0000
committerBoxy <rust@boxyuwu.dev>2024-12-03 00:21:51 +0000
commit2807ba77a0b8c22d1326a6bfef87b28942fe9ab7 (patch)
tree968056dd82d193215d63a1dcba8306583b8f9ef6
parentd49be02cf6d2e2a01264fcdef1e20c826710c0f5 (diff)
downloadrust-2807ba77a0b8c22d1326a6bfef87b28942fe9ab7.tar.gz
rust-2807ba77a0b8c22d1326a6bfef87b28942fe9ab7.zip
Use correct `hir_id` for array const arg infers
-rw-r--r--compiler/rustc_ast_lowering/src/lib.rs3
-rw-r--r--compiler/rustc_metadata/src/rmeta/encoder.rs12
-rw-r--r--tests/ui/const-generics/generic_arg_infer/array-repeat-expr-lib.rs12
3 files changed, 22 insertions, 5 deletions
diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs
index 7ffe4db6e45..3a04a66cd14 100644
--- a/compiler/rustc_ast_lowering/src/lib.rs
+++ b/compiler/rustc_ast_lowering/src/lib.rs
@@ -2013,7 +2013,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
             ExprKind::Underscore => {
                 if self.tcx.features().generic_arg_infer() {
                     let ct_kind = hir::ConstArgKind::Infer(self.lower_span(c.value.span));
-                    self.arena.alloc(hir::ConstArg { hir_id: self.next_id(), kind: ct_kind })
+                    self.arena
+                        .alloc(hir::ConstArg { hir_id: self.lower_node_id(c.id), kind: ct_kind })
                 } else {
                     feature_err(
                         &self.tcx.sess,
diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs
index 068a5d31a8e..a34ea18f716 100644
--- a/compiler/rustc_metadata/src/rmeta/encoder.rs
+++ b/compiler/rustc_metadata/src/rmeta/encoder.rs
@@ -1389,10 +1389,14 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
             // `ConstArgKind::Path`. We never actually access this `DefId`
             // anywhere so we don't need to encode it for other crates.
             if def_kind == DefKind::AnonConst
-                && matches!(
-                    tcx.hir_node_by_def_id(local_id),
-                    hir::Node::ConstArg(hir::ConstArg { kind: hir::ConstArgKind::Path(..), .. })
-                )
+                && match tcx.hir_node_by_def_id(local_id) {
+                    hir::Node::ConstArg(hir::ConstArg { kind, .. }) => match kind {
+                        // Skip encoding defs for these as they should not have had a `DefId` created
+                        hir::ConstArgKind::Path(..) | hir::ConstArgKind::Infer(..) => true,
+                        hir::ConstArgKind::Anon(..) => false,
+                    },
+                    _ => false,
+                }
             {
                 continue;
             }
diff --git a/tests/ui/const-generics/generic_arg_infer/array-repeat-expr-lib.rs b/tests/ui/const-generics/generic_arg_infer/array-repeat-expr-lib.rs
new file mode 100644
index 00000000000..c1f725db126
--- /dev/null
+++ b/tests/ui/const-generics/generic_arg_infer/array-repeat-expr-lib.rs
@@ -0,0 +1,12 @@
+//@ check-pass
+
+#![feature(generic_arg_infer)]
+#![crate_type = "lib"]
+
+// Test that encoding the hallucinated `DefId` for the `_` const argument doesn't
+// ICE (see #133468). This requires this to be a library crate.
+
+pub fn foo() {
+    let s: [u8; 10];
+    s = [0; _];
+}