Issue 23127 - @safe inference seems to depend on semantic analysis order
Summary: @safe inference seems to depend on semantic analysis order
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 blocker
Assignee: No Owner
URL:
Keywords: industry
Depends on:
Blocks:
 
Reported: 2022-05-19 16:35 UTC by kinke
Modified: 2022-07-23 10:17 UTC (History)
2 users (show)

See Also:


Attachments
4 source modules (892 bytes, application/x-xz)
2022-05-19 16:35 UTC, kinke
Details

Note You need to log in before you can comment on or make changes to this issue.
Description kinke 2022-05-19 16:35:33 UTC
Created attachment 1853 [details]
4 source modules

The attached archive contains 4 dustmited modules. With DMD v2.100.0, all 4 modules can be individually compiled just fine.

With `-allinst` however, compiling `variant.d` (alone) fails with:
-----
rbtree.d(36): Error: `@safe` function `rbtree.RedBlackTree!(Pair, lessPairKeys).RedBlackTree.toHash` cannot call `@system` function `rbtree.RBRange!(RBNode!(Pair)*).RBRange.popFront`
rbtree.d(22):        `rbtree.RBRange!(RBNode!(Pair)*).RBRange.popFront` is declared here
statictypes.d(12): Error: template instance `rbtree.RedBlackTree!(Pair, lessPairKeys)` error instantiating
statictypes.d(15):        instantiated from here: `Map!(string, SType)`
-----

The 3 other modules compile fine with `-allinst` too.

Prepending another root module works: `dmd -o- -allinst statictypes.d variant.d`. Swapping the root modules order makes it fail again: `dmd -o- -allinst variant.d statictypes.d`
Comment 1 mhh 2022-05-19 21:34:18 UTC
I'm going to have a crack at this tonight.
Comment 2 mhh 2022-05-20 01:27:42 UTC
Some printf debugging:

Context: popFront
exp: sym/rbtree.d(22):(*this._begin).next()
Data about f:
	next
	sym/rbtree.d(9)
	(null)
	next has type RBNode!(Pair)*()
setUnsafe:  next
setUnsafe: In progress
	sc.flag & compile: not
setUnsafe: sym/rbtree.d(9) popFront
setUnsafe: In progress

FuncDeclaration::semantic3(function 'next', sc = 0x7f7eb91f03d0)

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Semantic3 is on next run *after* its first use.
Comment 3 mhh 2022-05-20 02:23:38 UTC
RBNode has it's AggregateDecl semantic3 done far far too late. I've checked for plain memory corruption, I think this is something trying to be clever and failing (as per usual).
Comment 4 kinke 2022-07-21 13:31:39 UTC
The 2 failing cmdlines work when disabling an apparent hack in https://github.com/dlang/dmd/blob/e2b4fb6d8a60ef33f5bbc6309e250024bbebb1a0/compiler/src/dmd/func.d#L441-L450.
Comment 5 johanengelen 2022-07-23 10:17:26 UTC
Related issue: https://issues.dlang.org/show_bug.cgi?id=17541