Stroustrup objects to adding Contracts to C++

jmh530 john.michael.hall at gmail.com
Thu Nov 12 04:34:53 UTC 2026


On Thursday, 12 November 2026 at 01:42:29 UTC, Richard (Rikki) 
Andrew Cattermole wrote:
> What can be done has already been done.
>
> D has had a lot of intelligent people working on it for over 25 
> years.
> All of the low hanging fruit, and many of the more costly ones 
> were done well over a decade ago.
>
> I've reviewed parts of dmd, such as the backend involved in the 
> application of abstract interpretation, optimizations and so 
> on, and what is there is rock solid.
>
> I wish there was something that could be done, but semantic 
> analysis isn't designed to be reentrant, it has scoping issues 
> across symbols lurking underneath it as well. There isn't 
> anything that has a ROI left that I've found.

I asked Codex (with GPT-6 Astra Extra High) to investigate this. 
It worked for a little over an hour. What follows is written by 
the LLM. The broad thrust was quite sympathetic to your points 
after trying to implement something. It suggests trying to 
improve diagnostics (like why inference failed) and deal with 
inference failures around source order (which is a bug I filed, 
so it might be giving that extra weight) as a starting point.

---

I investigated this against a recent DMD checkout, including 
tracing semantic analysis, reproducing the failures, reviewing 
the earlier PRs, and building a small experimental patch. The 
results support your concerns about the general problem, but 
suggest a narrower distinction between improvements that require 
reopening semantic decisions and improvements that might avoid 
doing so.

A few concrete findings:

* Direct recursion already infers `pure`, `nothrow`, and `@nogc` 
in the tested function templates. Equivalent mutual recursion 
fails. Annotating one function can make the example pass or fail 
depending on which function is analyzed first.
* [Issue #24202](https://github.com/dlang/dmd/issues/24202) still 
reproduces: reversing the source-file order changes whether 
compilation succeeds. The trace showed an explicitly annotated 
function querying an inferred function whose body is still being 
analyzed. Purity and `@nogc` inference are then abandoned; the 
`nothrow` check reports an error before the callee eventually 
finishes as `nothrow`. That particular failure did not involve 
missing aggregate scope information.
* In the current implementation, querying pending `@nogc` 
inference through `isNogc` can call `setGC` and clear the 
inference state. There is no ordinary mechanism to revisit that 
decision once the recursive dependency finishes. 
[Source](https://github.com/dlang/dmd/blob/dd86c699bcef756744adaa167ded1d1377d20540/compiler/src/dmd/nogc.d#L358).

The investigation also found concrete reasons that simply 
rerunning semantic analysis would be unsafe. Bodies are mutated 
and lowered, saved scopes retain shared context, template 
instances are cached, and speculative analysis has error-gagging 
boundaries. A second pass cannot just reconstruct everything from 
the existing attribute-violation records.

However, collecting effects and unresolved call dependencies 
**during the existing analysis**, then resolving them without 
rerunning bodies, appears possible for a restricted subset.

The experimental `@nogc` patch demonstrated that for very simple, 
unconstrained module-level function templates with fixed integer 
signatures. It retained unresolved calls as obligations, waited 
for the participating bodies to finish, and only then published 
successful inference. It did not restart function bodies or 
expose an unknown callee as proven `@nogc`.

Five previously rejected positive cases compiled, all 42 negative 
controls remained rejected, and 14 selected upstream regression 
targets passed. Those results are limited: many negative cases 
exercise conservative fallback because the prototype excludes 
their syntax.

I would **not** propose merging this prototype. Its supported 
subset is too small to establish useful library coverage, it does 
not fix #24202, and its naive implementation added about 64% to a 
synthetic 300-function-cycle compilation. That timing used 
matching nonoptimized compiler builds, not production throughput 
measurements.

The main obstacle to expanding it is when attributes become 
observable. Traits, `static if`, constraints, and overload 
resolution can use an unfinished function’s attributes to choose 
the code being analyzed. That is a different problem from solving 
an already-fixed call graph. Publishing newly inferred attributes 
also changes function types and potentially mangling. An SCC 
algorithm does not resolve those issues.

The earlier work reinforces that distinction: 
[#5726](https://github.com/dlang/dmd/pull/5726) explored 
scheduling changes, while 
[#11781](https://github.com/dlang/dmd/pull/11781) encountered 
forward-reference problems when expanding inference eligibility. 
The existing fast DFA did not provide a missing GC-effect solver; 
the experiment reused frontend checks.

So I agree that a comprehensive solution could have poor ROI. 
What remains less clear is whether that also applies to better 
diagnostics and a bounded source-order fix. Those look like 
stronger initial targets than general interprocedural inference, 
although the investigation has not demonstrated a production fix 
for either.

The experiment establishes a small amount of architectural room 
to work, not that exploiting it broadly would be worth the cost.


More information about the Digitalmars-d mailing list