Stroustrup objects to adding Contracts to C++
jmh530
john.michael.hall at gmail.com
Thu Nov 12 01:31:10 UTC 2026
On Wednesday, 11 November 2026 at 20:24:12 UTC, Richard (Rikki)
Andrew Cattermole wrote:
> On 12/11/2026 4:18 AM, Paul Backus wrote:
>> On Wednesday, 11 November 2026 at 12:37:58 UTC, Richard
>> (Rikki) Andrew Cattermole wrote:
>>> Effects analysis in dmd is DOA in the type system.
>>>
>>> It cannot do it correctly.
>>>
>>> Walter screwed up designing the architecture for dmd's
>>> semantic analysis when he designed it to be fast and not for
>>> analysis capabilities.
>>>
>>> See: attribute soup. Without inference working correctly,
>>> this problem cannot be fixed, and we can't fix it.
>>
>> If we've given up on fixing DMD's semantic analysis then D as
>> a language is as good as dead.
>
> We are not rewriting the entirety of semantic analysis just to
> fix this one architecture problem.
>
> It'll take a decade for the compiler to stabilize, and that
> will definitely kill D.
>
> Alternatively we could accept that this architectural problem
> exists, and should find alternative approaches. My first foray
> into that with the fast dfa engine so far appears to be a
> success.
I directed GPT-6 Astra Pro to review this thread, investigate the
relevant DMD implementation, and think through a practical path
forward. What follows is a summary of its recommendations.
---
I think Rikki identifies a real architectural problem in DMD, but
the evidence doesn't suggest that addressing it requires
rewriting semantic analysis from scratch. There appears to be a
useful middle ground between accepting the current limitations
and undertaking a multi-year rewrite.
The design goals should be:
- Preserve the existing transitive guarantees of `@nogc`, `pure`,
and `nothrow`.
- Improve inference, particularly for internal functions and
mutually recursive functions.
- Avoid significant compilation-time regressions or changes to
existing language semantics.
- Make improvements incrementally, with changes that can be
tested and reviewed independently.
### The underlying problem
DMD's semantic analysis is demand-driven, and attribute inference
can depend on functions whose semantic analysis hasn't finished.
In some cases, an attribute that is not yet known is effectively
treated as unavailable, and that decision isn't revisited.
This creates problems with mutually recursive functions, template
instantiation, and even source-file ordering. For example, DMD
issue [#24202](https://github.com/dlang/dmd/issues/24202)
demonstrates a case where reversing the order of two source files
changes whether compilation succeeds.
Importantly, checking a transitive contract and inferring that
contract are different problems. DMD already knows how to enforce
explicit `@nogc` contracts across function calls. Improving
inference shouldn't require weakening that guarantee.
### A possible path forward
I'd suggest pursuing three related improvements.
1. **Improve how semantic analysis handles incomplete
information.** Distinguish between an attribute that has been
determined to be absent and one whose inference hasn't finished.
Track dependencies so that decisions can be revisited when the
necessary information becomes available.
2. **Introduce a limited interprocedural inference mechanism.**
For functions whose bodies and call relationships can be
resolved, collect local effects and propagate them through the
call graph. Strongly connected components and fixed-point
analysis could handle mutually recursive functions. Start with
`@nogc`, then evaluate extending the mechanism to `nothrow` and
`pure`.
3. **Expand inference incrementally.** Once the underlying
machinery is reliable, investigate inference for ordinary
private/internal functions while keeping explicit contracts at
public API boundaries. This could reduce annotation burden
without making public interfaces dependent on implementation
details.
There is an important complication: D's compile-time reflection,
template constraints, and `static if` can make the code being
analyzed depend on the attributes being inferred. A fixed-point
solver alone cannot resolve every such dependency. Those cases
need explicit rules for when inferred attributes become
observable, and conservative behavior where necessary.
### Where fast DFA fits
Rikki's fast DFA work demonstrates that useful analysis can be
added without replacing semantic analysis. However, the existing
implementation is primarily intraprocedural, with limited
interprocedural summaries. It also has some precision and
ordering limitations.
It may be useful for local analysis, but DMD already detects many
of the operations relevant to `@nogc`. The more important missing
infrastructure appears to be dependency tracking, inference
scheduling, and handling incomplete results. I wouldn't make the
new DFA engine a prerequisite for that work.
### Recommendation
Rather than starting with a language redesign, I'd propose a
sequence of relatively focused PRs: regression tests for ordering
and recursive inference, improvements to tracking incomplete
semantic information, a limited `@nogc` inference prototype, and
then broader inference where compatibility permits.
Existing work such as [PR
#5726](https://github.com/dlang/dmd/pull/5726) and [PR
#11781](https://github.com/dlang/dmd/pull/11781) may provide
useful starting points.
The key is to separate what is genuinely difficult because of D's
metaprogramming semantics from what is difficult because of DMD's
current implementation. The former may require language-design
decisions; the latter looks like technical debt that can be
addressed incrementally.
That seems a more promising direction than either removing useful
attributes or committing to a wholesale rewrite of the compiler.
More information about the Digitalmars-d
mailing list