Skip to content

Fix evaluation order in struct field expressions#4448

Open
elshorbagyx wants to merge 4 commits intoRust-GCC:masterfrom
elshorbagyx:fix-4445
Open

Fix evaluation order in struct field expressions#4448
elshorbagyx wants to merge 4 commits intoRust-GCC:masterfrom
elshorbagyx:fix-4445

Conversation

@elshorbagyx
Copy link

@elshorbagyx elshorbagyx commented Feb 25, 2026

Fixes #4445

gcc/rust/ChangeLog:

* backend/rust-compile-expr.cc (CompileExpr::visit): Get struct field type with field name
* typecheck/rust-hir-type-check-struct.cc (TypeCheckStructExpr::resolve): Cancel reordering of struct field exprs

gccrs evaluates struct field expressions in the order they are declared inside the struct definition. The current logic reorders the struct field expressions to match the struct field types, which results in incorrect behavior compared to the Rust reference (as mentioned in #4445). Instead, we could fetch the struct field type by field name through lookup_field.

@elshorbagyx elshorbagyx force-pushed the fix-4445 branch 4 times, most recently from 188de4d to d1fd377 Compare February 25, 2026 04:00
@philberty
Copy link
Member

the tests are failing:

FAIL: rust/compile/torture/struct_init_7.rs   -O0  (internal compiler error: in fold_convert_loc, at fold-const.cc:2672)
FAIL: rust/compile/torture/struct_init_7.rs   -O0   at line 7 (test for warnings, line 6)
FAIL: rust/compile/torture/struct_init_7.rs   -O0   at line 13 (test for warnings, line 12)
FAIL: rust/compile/torture/struct_init_7.rs   -O0  (test for excess errors)
FAIL: rust/compile/torture/struct_init_7.rs   -O1  (internal compiler error: in fold_convert_loc, at fold-const.cc:2672)
FAIL: rust/compile/torture/struct_init_7.rs   -O1   at line 7 (test for warnings, line 6)
FAIL: rust/compile/torture/struct_init_7.rs   -O1   at line 13 (test for warnings, line 12)
FAIL: rust/compile/torture/struct_init_7.rs   -O1  (test for excess errors)
FAIL: rust/compile/torture/struct_init_7.rs   -O2  (internal compiler error: in fold_convert_loc, at fold-const.cc:2672)
FAIL: rust/compile/torture/struct_init_7.rs   -O2   at line 7 (test for warnings, line 6)
FAIL: rust/compile/torture/struct_init_7.rs   -O2   at line 13 (test for warnings, line 12)
FAIL: rust/compile/torture/struct_init_7.rs   -O2  (test for excess errors)
FAIL: rust/compile/torture/struct_init_7.rs   -O3 -g  (internal compiler error: in fold_convert_loc, at fold-const.cc:2672)
FAIL: rust/compile/torture/struct_init_7.rs   -O3 -g   at line 7 (test for warnings, line 6)
FAIL: rust/compile/torture/struct_init_7.rs   -O3 -g   at line 13 (test for warnings, line 12)
FAIL: rust/compile/torture/struct_init_7.rs   -O3 -g  (test for excess errors)
FAIL: rust/compile/torture/struct_init_7.rs   -Os  (internal compiler error: in fold_convert_loc, at fold-const.cc:2672)
FAIL: rust/compile/torture/struct_init_7.rs   -Os   at line 7 (test for warnings, line 6)
FAIL: rust/compile/torture/struct_init_7.rs   -Os   at line 13 (test for warnings, line 12)
FAIL: rust/compile/torture/struct_init_7.rs   -Os  (test for excess errors)
FAIL: rust/compile/torture/struct_init_7.rs   -O2 -flto -fno-use-linker-plugin -flto-partition=none  (internal compiler error: in fold_convert_loc, at fold-const.cc:2672)
FAIL: rust/compile/torture/struct_init_7.rs   -O2 -flto -fno-use-linker-plugin -flto-partition=none   at line 7 (test for warnings, line 6)
FAIL: rust/compile/torture/struct_init_7.rs   -O2 -flto -fno-use-linker-plugin -flto-partition=none   at line 13 (test for warnings, line 12)
FAIL: rust/compile/torture/struct_init_7.rs   -O2 -flto -fno-use-linker-plugin -flto-partition=none  (test for excess errors)
FAIL: rust/compile/torture/struct_init_7.rs   -O2 -flto -fuse-linker-plugin -fno-fat-lto-objects  (internal compiler error: in fold_convert_loc, at fold-const.cc:2672)
FAIL: rust/compile/torture/struct_init_7.rs   -O2 -flto -fuse-linker-plugin -fno-fat-lto-objects   at line 7 (test for warnings, line 6)
FAIL: rust/compile/torture/struct_init_7.rs   -O2 -flto -fuse-linker-plugin -fno-fat-lto-objects   at line 13 (test for warnings, line 12)
FAIL: rust/compile/torture/struct_init_7.rs   -O2 -flto -fuse-linker-plugin -fno-fat-lto-objects  (test for excess errors)
FAIL: rust/compile/torture/struct_init_8.rs   -O0  (internal compiler error: in fold_convert_loc, at fold-const.cc:2672)
FAIL: rust/compile/torture/struct_init_8.rs   -O0   at line 9 (test for warnings, line 8)
FAIL: rust/compile/torture/struct_init_8.rs   -O0  (test for excess errors)
FAIL: rust/compile/torture/struct_init_8.rs   -O1  (internal compiler error: in fold_convert_loc, at fold-const.cc:2672)
FAIL: rust/compile/torture/struct_init_8.rs   -O1   at line 9 (test for warnings, line 8)
FAIL: rust/compile/torture/struct_init_8.rs   -O1  (test for excess errors)
FAIL: rust/compile/torture/struct_init_8.rs   -O2  (internal compiler error: in fold_convert_loc, at fold-const.cc:2672)
FAIL: rust/compile/torture/struct_init_8.rs   -O2   at line 9 (test for warnings, line 8)
FAIL: rust/compile/torture/struct_init_8.rs   -O2  (test for excess errors)
FAIL: rust/compile/torture/struct_init_8.rs   -O3 -g  (internal compiler error: in fold_convert_loc, at fold-const.cc:2672)
FAIL: rust/compile/torture/struct_init_8.rs   -O3 -g   at line 9 (test for warnings, line 8)
FAIL: rust/compile/torture/struct_init_8.rs   -O3 -g  (test for excess errors)
FAIL: rust/compile/torture/struct_init_8.rs   -Os  (internal compiler error: in fold_convert_loc, at fold-const.cc:2672)
FAIL: rust/compile/torture/struct_init_8.rs   -Os   at line 9 (test for warnings, line 8)
FAIL: rust/compile/torture/struct_init_8.rs   -Os  (test for excess errors)
FAIL: rust/compile/torture/struct_init_8.rs   -O2 -flto -fno-use-linker-plugin -flto-partition=none  (internal compiler error: in fold_convert_loc, at fold-const.cc:2672)
FAIL: rust/compile/torture/struct_init_8.rs   -O2 -flto -fno-use-linker-plugin -flto-partition=none   at line 9 (test for warnings, line 8)
FAIL: rust/compile/torture/struct_init_8.rs   -O2 -flto -fno-use-linker-plugin -flto-partition=none  (test for excess errors)
FAIL: rust/compile/torture/struct_init_8.rs   -O2 -flto -fuse-linker-plugin -fno-fat-lto-objects  (internal compiler error: in fold_convert_loc, at fold-const.cc:2672)
FAIL: rust/compile/torture/struct_init_8.rs   -O2 -flto -fuse-linker-plugin -fno-fat-lto-objects   at line 9 (test for warnings, line 8)
FAIL: rust/compile/torture/struct_init_8.rs   -O2 -flto -fuse-linker-plugin -fno-fat-lto-objects  (test for excess errors)

but also i am pretty sure we update the ordering within the typecheck pass to sort this out.

@elshorbagyx
Copy link
Author

the tests are failing:

I am working on that.

@powerboat9
Copy link
Collaborator

Make sure to double-check that the generated GIMPLE doesn't have unspecified evaluation order (I'm not sure about TREE_CONSTRUCTOR)

Fixes Rust-GCC#4445

gcc/rust/ChangeLog:

	* backend/rust-compile-expr.cc (CompileExpr::visit): Get struct field
	type with field name
	* typecheck/rust-hir-type-check-struct.cc (TypeCheckStructExpr::resolve): Cancel
	reordering of struct field exprs

Signed-off-by: Mohamed El Shorbagy <mohrizq895@gmail.com>
elshorbagyx and others added 3 commits February 27, 2026 09:00
gcc/rust/ChangeLog:

	* backend/rust-compile-expr.cc (CompileExpr::visit):

Signed-off-by: Mohamed El Shorbagy <mohrizq895@gmail.com>
@elshorbagyx
Copy link
Author

elshorbagyx commented Feb 27, 2026

@philberty @powerboat9
Tests are passing now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Evaluation order in struct expressions can be incorrect

3 participants