]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Support const generic inference variables
authorPhilip Herron <herron.philip@googlemail.com>
Thu, 31 Jul 2025 20:18:56 +0000 (21:18 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 5 Aug 2025 14:37:01 +0000 (16:37 +0200)
We already support const infer so this just creates a fresh tyty::infer_var
for the const element type and then a ConstKind::Infer type for the const
type wrapper and the existing plumbing handles this.

Fixes Rust-GCC#3885

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): create infer variable

gcc/testsuite/ChangeLog:

* rust/compile/issue-3885.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gcc/rust/typecheck/rust-hir-type-check-expr.cc
gcc/testsuite/rust/compile/issue-3885.rs [new file with mode: 0644]

index 5bf8cc3bc1f826c38fccc55d547e0095ee0dd3de..2dc0e296e79baefe5f5ebb496b5abb248c69007a 100644 (file)
@@ -33,6 +33,7 @@
 #include "rust-type-util.h"
 #include "rust-immutable-name-resolution-context.h"
 #include "rust-compile-base.h"
+#include "rust-tyty-util.h"
 #include "tree.h"
 
 namespace Rust {
@@ -662,11 +663,22 @@ TypeCheckExpr::visit (HIR::BlockExpr &expr)
 void
 TypeCheckExpr::visit (HIR::AnonConst &expr)
 {
-  // FIXME: How do we typecheck a deferred inference const?
+  if (!expr.is_deferred ())
+    {
+      infered = TypeCheckExpr::Resolve (expr.get_inner_expr ());
+      return;
+    }
+
+  auto locus = expr.get_locus ();
+  auto infer_ty_var = TyTy::TyVar::get_implicit_infer_var (locus);
 
-  rust_assert (!expr.is_deferred ());
+  HirId next = mappings.get_next_hir_id ();
+  infered = new TyTy::ConstType (TyTy::ConstType::ConstKind::Infer, "",
+                                infer_ty_var.get_tyty (), error_mark_node, {},
+                                locus, next, next, {});
 
-  infered = TypeCheckExpr::Resolve (expr.get_inner_expr ());
+  context->insert_implicit_type (infered->get_ref (), infered);
+  mappings.insert_location (infered->get_ref (), locus);
 }
 
 void
diff --git a/gcc/testsuite/rust/compile/issue-3885.rs b/gcc/testsuite/rust/compile/issue-3885.rs
new file mode 100644 (file)
index 0000000..050a59c
--- /dev/null
@@ -0,0 +1,7 @@
+pub fn test() {
+    let _u: [_; _] = [15u32];
+    let _v: [u8; _] = [1, 2, 3];
+    let _w: [_; 2] = [1.0, 2.0];
+    let _x = [42; 5];
+    let _y: [_; _] = _x;
+}