https://gcc.gnu.org/g:9e383861e9a1619b9263253fa2fefceb70b98def
commit r17-5136-g9e383861e9a1619b9263253fa2fefceb70b98def Author: Philip Herron <[email protected]> Date: Mon Sep 14 22:07:59 2026 +0100 gccrs: Fix ICE in constexpr for unit-structs 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]> Diff: --- 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 +-- gcc/testsuite/rust/compile/issues/issue-4668.rs | 18 ++++++++++++++++++ 6 files changed, 32 insertions(+), 5 deletions(-) diff --git a/gcc/rust/backend/rust-compile-resolve-path.cc b/gcc/rust/backend/rust-compile-resolve-path.cc index db984bee3393..12b200a11d9b 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 44fd235b925b..65a7c8c1773d 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 4d02e3b5c52d..63f970dfe9e7 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 128278111563..4c95d8fee91b 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 a30e3ff1b310..673c44702e2d 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 000000000000..4f64bbbb0220 --- /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() +}];
