Skip to content

Fix/deps betterc semantic3 - #23960

Open
zxan wants to merge 6 commits into
dlang:masterfrom
zxan:fix/deps-betterc-semantic3
Open

zxan wants to merge 6 commits into
dlang:masterfrom
zxan:fix/deps-betterc-semantic3

Conversation

@zxan

@zxan zxan commented Oct 2, 2026

Copy link
Copy Markdown

Fixes #20392

Problem

Using -deps with -betterC causes spurious errors in transitively imported druntime modules the user never references. A bare unused import is enough to trigger it:

import core.memory;

dmd repro.d -betterC -deps=deps.txt // fails with throw/GC errors in core.exception, core.time
dmd repro.d -betterC -c // compiles clean

Root cause

When -deps is active, tryMain calls semantic3OnDependencies(), which recursively forces full function-body analysis (semantic3) on every transitively imported module. Under -betterC, that flags constructs in unreferenced druntime functions that are legal in druntime but illegal in user code. The -deps output is populated during normal semantic passes and does not require this extra step.

Confirmed via GDB stack trace:
#40 dmd.dsymbolsem.semantic3OnDependencies(Module m) at src/dmd/dsymbolsem.d:718
#41 dmd.dsymbolsem.semantic3OnDependencies(Module m) at src/dmd/dsymbolsem.d:721
#42 dmd.main.tryMain(...) at src/dmd/main.d:732

Fix

Remove semantic3OnDependencies and its call site in main.d. This revives the approach from #9122, which stalled in 2019 due to a since-resolved objection. For recursive dependencies, use -deps combined with -i.

@dkorpel

@zxan
zxan requested a review from ibuclaw as a code owner October 2, 2026 19:43
Comment thread repro.reduced/a.d Outdated
@@ -0,0 +1 @@
import core.memory;

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.

A test case should be minimal and self-contained. Importing druntime creates a very large deps file which is fragile, as any change could alter that output and require updating the this test.

Comment thread changelog/dmd.fix-deps-betterc.dd Outdated
Comment on lines +1 to +17
`-deps` no longer causes spurious `-betterC` errors in unreferenced modules

Using `-deps` together with `-betterC` previously caused spurious errors
in transitively imported druntime modules the user never referenced —
`throw` statements in `core.exception` and GC array literals in
`core.time` being the most common.

This happened because `-deps` triggered `semantic3OnDependencies`, which
forced full function-body analysis on every transitively imported module.
Under `-betterC`, that analysis flags constructs that are perfectly legal
in druntime but illegal in user code.

The `-deps` output is populated during normal semantic passes and does not
require this additional analysis step, so the call has been removed.

To get recursive dependencies (the original motivation for this behavior),
combine `-deps` with `-i`.

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.

This looks like an AI-written description that is very literal. It describes the issue from a very technical lens through the specific issue that this PR closes, which is only a manifestation of the more general problem that -deps conflates -i.

@zxan

zxan commented Oct 11, 2026

Copy link
Copy Markdown
Author

Removed the druntime repro and the stray deps.txt. Added compilable/test20392.d, which imports a local module with a static assert(0) inside a function body. It fails on the old compiler with -deps and passes now, no druntime involved. Also rewrote the changelog around the -deps/-i conflation instead of the betterC symptom.

@zxan
zxan requested a review from dkorpel October 11, 2026 10:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

'-deps' causes compile errors when using '-betterC'

2 participants