Skip to content

Commit 2495bd6

Browse files
committed
Add a better expansion that is cheaper to compile
1 parent ab9eed7 commit 2495bd6

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

text/0000-associated-const-underscore.md

+48-1
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,54 @@ of which are adequate to this use case.
437437
};
438438
```
439439

440-
As far as I know, this expansion is able to accomplish all technical
440+
For a library containing `pub struct Thing { field: i32 }` and the above
441+
`const _`, this produces an rlib that is 7.2 KB, containing a symbol for that
442+
`assert_fields_are_total_eq` function.
443+
444+
```console
445+
$ llvm-dwarfdump target/debug/librepro.rlib
446+
447+
DW_TAG_namespace
448+
DW_AT_name ("repro")
449+
450+
DW_TAG_namespace
451+
DW_AT_name ("_")
452+
453+
DW_TAG_namespace
454+
DW_AT_name ("{impl#0}")
455+
456+
DW_TAG_subprogram
457+
DW_AT_low_pc (0x0000000000000000)
458+
DW_AT_high_pc (0x0000000000000001)
459+
DW_AT_frame_base (DW_OP_reg7 RSP)
460+
DW_AT_linkage_name ("_ZN67_$LT$repro..Thing$u20$as$u20$repro.._..__AssertFieldsAreTotalEq$GT$26assert_fields_are_total_eq17hc74c403364f7baa6E")
461+
DW_AT_name ("assert_fields_are_total_eq")
462+
DW_AT_decl_file ("src/lib.rs")
463+
DW_AT_decl_line (12)
464+
DW_AT_external (true)
465+
```
466+
467+
We can make an approach that is cheaper to compile by changing the
468+
`__AssertFieldsAreTotalEq` trait's contents from a fn to a const. This way
469+
there is no longer a need to compile the function's body to machine code;
470+
just type-check it. This reduces the size of librepro.rlib by 35% to 4.7 KB.
471+
472+
```rust
473+
impl ::core::cmp::Eq for Thing {}
474+
475+
const _: () = {
476+
trait __AssertFieldsAreTotalEq {
477+
const ASSERT_FIELDS_ARE_TOTAL_EQ: ();
478+
}
479+
impl __AssertFieldsAreTotalEq for Thing {
480+
const ASSERT_FIELDS_ARE_TOTAL_EQ: () = {
481+
let _: ::core::cmp::AssertParamIsEq<Field>;
482+
};
483+
}
484+
};
485+
```
486+
487+
As far as I know, this final expansion is able to accomplish all technical
441488
objectives. I considered making a PR to make `derive(Eq)` take this
442489
approach, but if possible, going straight to the associated const underscore
443490
proposed by this RFC would be preferable.

0 commit comments

Comments
 (0)