Skip to content

Make the synthesized class invariant a member - #23941

Open
TurkeyMan wants to merge 1 commit into
dlang:masterfrom
TurkeyMan:pr3_invariant_member
Open

TurkeyMan wants to merge 1 commit into
dlang:masterfrom
TurkeyMan:pr3_invariant_member

Conversation

@TurkeyMan

Copy link
Copy Markdown
Contributor

buildInv pushes the merged __invariant function into the members but never added it to the symbol table, so T.__invariant could not be named (user invariants get numbered names). It is now added, and listed by __traits(allMembers) like __xdtor, so a runtime can take its address.

This progresses towards runtime ClassInfo synthesis.

@TurkeyMan
TurkeyMan force-pushed the pr3_invariant_member branch 2 times, most recently from 886c0d9 to 01e8e40 Compare September 29, 2026 13:47

@thewilsonator thewilsonator left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, needs a spec update though

Comment thread compiler/src/dmd/traits.d
Comment thread changelog/dmd.invariant-member.dd Outdated
Comment on lines +1 to +6
The generated invariant of an aggregate is now accessible as `__invariant`

The compiler generates one function per struct or class that calls all of its
`invariant` blocks. Previously it was not added to the symbol table, so it could
not be referred to. It is now a member named `__invariant`, like the generated
destructor is `__xdtor`, and is included in `__traits(allMembers)`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While it's good to mention this in the changelog, I wouldn't advertise it as something users should reach for (double underscore means internal reserved name). Maybe mention the specialized use case.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm actually tempted to just skip the changelog and spec entry for this... nobody wants to know about it, and in the future, allMembers will make it plainly discoverable to anyone.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did delete it; a double-underscore symbol isn't public contract, and I don't think we need to advertise it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While technically true, from experience with similar changes (like updating __unittest .mangleof) I have a strong feeling this will break someone's delicate introspection contraption when upgrading dmd, so it is courteous to mention it in that regard.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sure it will, but that's the reason we make no guarantees.
So, a changelog mention? I wouldn't spec it...?

@github-actions

github-actions Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

DMD perf check

Metric Base PR Δ
compile Phobos codegen (instr) 1,423.7 M 1,427.5 M +0.269%
All measurements
Metric Base PR Δ
compile hello.d (instr) 211.6 M 211.5 M -0.041%
compile hello.d -O -release (instr) 229.2 M 229.1 M -0.040%
compile Phobos (instr) 4,896.2 M 4,899.7 M +0.071%
compile Phobos codegen (instr) 1,423.7 M 1,427.5 M +0.269%
compile vibe.d (instr) 14,468.8 M 14,477.3 M +0.058%
dmd binary size (stripped) 8.12 MB 8.12 MB +0.05%
hello binary size (stripped) 0.72 MB 0.72 MB 0.00%
peak RSS (compile hello.d) 44.03 MB 44.12 MB +0.22%
peak RSS (compile Phobos) 615.0 MB 614.9 MB -0.01%
peak RSS (compile vibe.d) 1902 MB 1902 MB 0.00%
page faults (compile hello.d) 8,928 8,929 +0.01%
page faults (compile Phobos) 153,198 153,198 0.00%
page faults (compile vibe.d) 476,435 476,435 0.00%
compile dmd itself (wall) 12.2 s 12.3 s +0.89%
compile hello.d (wall) 63.2 ms 63.0 ms -0.27%
compile Phobos (wall) 1,458 ms 1,455 ms -0.22%

f50dc28 vs merge-base 10adc6c · about these metrics

@TurkeyMan
TurkeyMan force-pushed the pr3_invariant_member branch 3 times, most recently from fedb2f9 to f5f23a5 Compare September 29, 2026 14:05
@TurkeyMan

Copy link
Copy Markdown
Contributor Author

needs a spec update though

I removed the changelog and spec update; I decided this is a private/internal symbol name, and shouldn't be part of the compiler contract. If you disagree, I can put it back, but I think this is correct; we should (theoretically) reserve the right to change __ things.

People that ever care about this in the future will easily discover it among allMembers, but we should make no commitments.

buildInv pushed the merged `__invariant` into the members but never added
it to the symbol table, so `T.__invariant` could not be named. Add it, and
list it in __traits(allMembers) like `__xdtor`.
@thewilsonator thewilsonator added the Review:Needs Spec PR A PR updating the language specification needs to be submitted to dlang.org label Sep 29, 2026
@thewilsonator

Copy link
Copy Markdown
Contributor

I removed the changelog and spec update; I decided this is a private/internal symbol name, and shouldn't be part of the compiler contract. If you disagree, I can put it back, but I think this is correct; we should (theoretically) reserve the right to change __ things.

I see that __dtor is not very well spec'd, its only appearance is

ef S opAssign(ref S s)
{
    S tmp = this;   // bitcopy this into tmp
    this = s;       // bitcopy s into this
    tmp.__dtor();   // call destructor on tmp
    return this;
}

and in the output of __traits(allMembers,...) tests in traits.dd. I think you should document __invariant in the __traits(allMembers,...) example for classes and structs in traits.dd.

@TurkeyMan

Copy link
Copy Markdown
Contributor Author

I think these should not appear in the spec personally. Are you expressing an opinion, or a ruling? Happy either way.

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

Labels

Review:Needs Spec PR A PR updating the language specification needs to be submitted to dlang.org

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants