From: Philip Herron <[email protected]>

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 <[email protected]>

Tested on x86_64-pc-linux-gnu.

---
 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 db984bee339..12b200a11d9 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 44fd235b925..65a7c8c1773 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 4d02e3b5c52..63f970dfe9e 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 12827811156..4c95d8fee91 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 a30e3ff1b31..673c44702e2 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 00000000000..4f64bbbb022
--- /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()
+}];
-- 
2.55.0

Reply via email to