[gccrs COMMIT] gccrs: Fix ICE in constexpr for unit-structs

gerris.rs@gmail.com gerris.rs@gmail.com
Tue Sep 15 13:29:05 GMT 2026


From: Philip Herron <herron.philip@googlemail.com>

HIR Items were using undify but Stmt level items used coercions so
this was inconsistent.

Fixes Rust-GCC/gccrs#4668

gcc/rust/ChangeLog:

	* backend/rust-compile-resolve-path.cc: return a constructor for this ADT
	* backend/rust-constexpr.cc (adjust_temp_type): check for constructor
	* typecheck/rust-hir-type-check-item.cc (TypeCheckItem::visit): this is a coercion site

gcc/testsuite/ChangeLog:

	* rust/compile/issues/2477.rs: remove old bad diag
	* rust/compile/issues/4165.rs: likewise
	* rust/compile/issues/issue-4668.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.


Commit on github: https://github.com/Rust-GCC/gccrs/commit/931c22cea397b5272ba09542781274421f6dbe62

The commit has been mentioned in the following issue(s):
 - Rust-GCC/gccrs#4668: https://github.com/Rust-GCC/gccrs/issues/4668

The commit has been mentioned in the following pull-request(s):
 - https://github.com/Rust-GCC/gccrs/pull/4899

 gcc/rust/backend/rust-compile-resolve-path.cc  |  6 +++++-
 gcc/rust/backend/rust-constexpr.cc             |  7 +++++++
 gcc/rust/typecheck/rust-hir-type-check-item.cc |  2 +-
 gcc/testsuite/rust/compile/issues/2477.rs      |  1 -
 gcc/testsuite/rust/compile/issues/4165.rs      |  3 +--
 .../rust/compile/issues/issue-4668.rs          | 18 ++++++++++++++++++
 6 files changed, 32 insertions(+), 5 deletions(-)
 create mode 100644 gcc/testsuite/rust/compile/issues/issue-4668.rs

diff --git a/gcc/rust/backend/rust-compile-resolve-path.cc b/gcc/rust/backend/rust-compile-resolve-path.cc
index db984bee3..12b200a11 100644
--- a/gcc/rust/backend/rust-compile-resolve-path.cc
+++ b/gcc/rust/backend/rust-compile-resolve-path.cc
@@ -81,7 +81,11 @@ ResolvePathRef::attempt_constructor_expression_lookup (
 
   TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (lookup);
   if (adt->is_unit ())
-    return unit_expression (expr_locus);
+    {
+      tree compiled_adt_type = TyTyResolveCompile::compile (ctx, adt);
+      return Backend::constructor_expression (compiled_adt_type, false, {}, -1,
+					      expr_locus);
+    }
 
   if (!adt->is_enum ())
     return error_mark_node;
diff --git a/gcc/rust/backend/rust-constexpr.cc b/gcc/rust/backend/rust-constexpr.cc
index 44fd235b9..65a7c8c17 100644
--- a/gcc/rust/backend/rust-constexpr.cc
+++ b/gcc/rust/backend/rust-constexpr.cc
@@ -1584,6 +1584,13 @@ adjust_temp_type (tree type, tree temp)
   if (same_type_p (TREE_TYPE (temp), type))
     return temp;
 
+  if (TREE_CODE (temp) == CONSTRUCTOR)
+    {
+      tree t = copy_node (temp);
+      TREE_TYPE (t) = type;
+      return t;
+    }
+
   gcc_assert (scalarish_type_p (type));
   /* Now we know we're dealing with a scalar, and a prvalue of non-class
      type is cv-unqualified.  */
diff --git a/gcc/rust/typecheck/rust-hir-type-check-item.cc b/gcc/rust/typecheck/rust-hir-type-check-item.cc
index 4d02e3b5c..63f970dfe 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-item.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-item.cc
@@ -644,7 +644,7 @@ TypeCheckItem::visit (HIR::ConstantItem &constant)
   TyTy::BaseType *expr_type = TypeCheckExpr::Resolve (constant.get_expr ());
   context->pop_const_context ();
 
-  TyTy::BaseType *unified = unify_site (
+  TyTy::BaseType *unified = coercion_site (
     constant.get_mappings ().get_hirid (),
     TyTy::TyWithLocation (type, constant.get_type ().get_locus ()),
     TyTy::TyWithLocation (expr_type, constant.get_expr ().get_locus ()),
diff --git a/gcc/testsuite/rust/compile/issues/2477.rs b/gcc/testsuite/rust/compile/issues/2477.rs
index 128278111..4c95d8fee 100644
--- a/gcc/testsuite/rust/compile/issues/2477.rs
+++ b/gcc/testsuite/rust/compile/issues/2477.rs
@@ -3,4 +3,3 @@
 
 const FOO: u32 = return 0;
 // { dg-error "return statement outside of function body" "" { target *-*-* } .-1 }
-// { dg-error "mismatched types, expected .u32. but got" "" { target *-*-* } .-2 }
diff --git a/gcc/testsuite/rust/compile/issues/4165.rs b/gcc/testsuite/rust/compile/issues/4165.rs
index a30e3ff1b..673c44702 100644
--- a/gcc/testsuite/rust/compile/issues/4165.rs
+++ b/gcc/testsuite/rust/compile/issues/4165.rs
@@ -4,8 +4,7 @@
 const N: usize = 2;
 const ARR: [i32; N] = [42; X];
 // { dg-error {cannot find value .X. in this scope \[E0425\]} "" { target *-*-* } .-1 }
-// { dg-error {mismatched types, expected .\[i32; 2]. but got .<tyty::error>. \[E0308\]} "" { target *-*-* } .-2 }
-// { dg-error {mismatched types, expected .usize. but got .bool. \[E0308\]} "" { target *-*-* } .-3 }
+// { dg-error {mismatched types, expected .usize. but got .bool. \[E0308\]} "" { target *-*-* } .-2 }
 const X: bool = (N[0] == 99) && (ARR[0] == 0);
 // { dg-error {the type .usize. cannot be indexed by .<integer>. \[E0277\]} "" { target *-*-* } .-1 }
 // { dg-error {mismatched types, expected .<tyty::error>. but got .<integer>. \[E0308\]} "" { target *-*-* } .-2 }
diff --git a/gcc/testsuite/rust/compile/issues/issue-4668.rs b/gcc/testsuite/rust/compile/issues/issue-4668.rs
new file mode 100644
index 000000000..4f64bbbb0
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issues/issue-4668.rs
@@ -0,0 +1,18 @@
+#![feature(no_core, lang_items)]
+#![no_core]
+
+#[lang = "sized"]
+pub trait Sized {}
+
+struct Dummy; // { dg-warning "struct is never constructed" }
+
+impl Dummy {
+    const fn func(self) -> usize { // { dg-warning "associated function is never used" }
+        42
+    }
+}
+
+const _: &[usize] = &[0; {
+    const DUMMY: &Dummy = &Dummy;
+    DUMMY.func()
+}];

base-commit: 3b0b57e0033f4198e492abcdbd65c00d83c734ae
-- 
2.55.0



More information about the Gcc-rust mailing list