Bug 18725 – compiler does not check all levels of methods for privateness, if used in invariant
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Mac OS X
Creation time
2018-04-04T13:22:02Z
Last change time
2023-05-09T14:49:53Z
Assigned to
No Owner
Creator
Alex
Comments
Comment #0 by sascha.orlov — 2018-04-04T13:22:02Z
Given this
´´´
void main()
{
auto s = S();
assert(&s);
}
struct S
{
invariant
{
assert(fun);
}
size_t[] member;
private bool fun() const
{
return fun1();
}
/*private*/ bool fun1() const
{
return true;
}
}
´´´
The code compiles with or without privateness of fun1. However, if the privateness for fun1 is missing the run yields a segmentation fault, while when it is present, the result is as expected.
If privateness for fun is missing, the compiler complains about using it in the invariant, as expected.
Comment #1 by razvan.nitu1305 — 2023-05-09T14:49:53Z
What happens here is that the invariant calls a private method which in turns calls a public method. Since the compiler inserts an invariant call before and after the call to `fun1` you get infinite recursion.
From the spec: "Do not indirectly call exported or public member functions within a class invariant, as this can result in infinite recursion" [1]. So I would say that this bug report is invalid. If the expectation is that the compiler checks for such indirect calls, then that is not going to happen since the frontend does not do dataflow analysis.
The idea is that invariants check public functions. Private functions are exempt so that you can put your validation checks in a private function and call that one, however, what the code example is doing is calling a public method to check the invariant correctness.
This bug is invalid.
[1] https://dlang.org/spec/class.html#invariants