caller-attr: Option A @CTX_SWITCH caller-required attribute (postfix marker, foreach/opApply, mangling) - #12
Draft
yanivbenyehuda wants to merge 7 commits into
Draft
caller-attr: Option A @CTX_SWITCH caller-required attribute (postfix marker, foreach/opApply, mangling)#12yanivbenyehuda wants to merge 7 commits into
yanivbenyehuda wants to merge 7 commits into
Conversation
Implements the generalized callerAttr facility from the design spec:
a function tagged @CTX_SWITCH (callerAttr!"NAME") is a context-switching
callee; every call must carry the member-access marker callee.NAME(args),
and the capability propagates upward (a non-@CTX_SWITCH function may not
call one). callerAttrFake is the migration shim. Lambdas infer @CTX_SWITCH
from their body; a context-switching lambda may not be passed to a plain
delegate parameter.
Frontend:
- core.attribute: callerAttr / callerAttrFake templates
- id.d: udaCallerAttr; attrib.d: isCallerAttrStruct / isCallerAttrExp
- expression.d: CallExp.markedCallerAttr
- expressionsem.d: .ATTR marker interception (resolveUFCS + DotIdExp),
checkCallerAttr enforcement (E1/E2/E3) for direct, method, and indirect
(delegate / function-pointer) calls; lambda inference; value-side
argument check (checkCallerAttrArgs)
Docs: design spec + Option A implementation spec.
Tests: compilable/callerattr.d; fail_compilation/callerattr_{unmarked,
propagation,badmarker,delegate,lambda_leak}.d.
Replaces the member-access marker (callee.CTX_SWITCH(args)) with a postfix attribute on the call. Parser change in parsePostExp adds the new expression grammar (PostfixExpression '@' Identifier); the member-access interception in resolveUFCS/visit(DotIdExp) is removed. checkCallerAttr now resolves the raw marker identifier through its callerAttr alias. Enforcement, lambda inference and the value-side argument check are unchanged. Note: unlike the member-access form, this `@`-postfix grammar is intrusive to third-party D parsers (libdparse/DCD/serve-d) until they are taught it. Tests and the implementation spec updated to the new syntax.
Extends the Option A caller-required-attribute feature (@CTX_SWITCH):
- foreach over @CTX_SWITCH opApply: statementsem auto-marks the lowered
opApply call (CallExp.callerAttrAutoMarked) and propagates the attribute
to the enclosing function; funcsem adds an overload tie-breaker so the
@CTX_SWITCH opApply overload is selected; expressionsem reports the
propagation error at the context-switching call site.
- CallExp.syntaxCopy copies markedCallerAttr so markers survive template
instantiation.
- dmangle: caller-attr now participates in name mangling, but ONLY when the
function has a same-name/same-type overload sibling (the real collision,
e.g. RobinHashTable's plain vs @CTX_SWITCH opApply). Standalone caller-attr
functions keep their baseline mangle, so cross-module references resolve.
- tests: compilable foreach cases + fail_compilation/callerattr_foreach{,_unmarked}.d.
Add .gitignore for the out-of-source /build/ dir.
… syntax
Replace the postfix `callee() @CTX_SWITCH` marker with a prefix `@CTX_SWITCH callee()`
form.
Parser (dmd/parse.d):
- Remove the postfix `@`-marker handling from parsePostExp.
- Add a prefix `@`-marker case in parseUnaryExp: `@MARK` binds the following
unary expression and attaches the marker to its (outermost) CallExp.
- Add statement-level disambiguation in parseStatement: a statement starting with
`@` is routed to expression parsing only when, after skipAttributes, what follows
is not a declaration and not an attribute scope (`:`) or block (`{`); otherwise it
remains a declaration. This resolves the new `@CTX_SWITCH yield();` vs `@safe void f()`
ambiguity that the prefix form introduces.
Diagnostics (dmd/expressionsem.d): E2 suggested-fix wording now reads
`must be marked `@CTX_SWITCH callee()``; comments updated.
Tests/docs: all callerattr tests and the walkthrough doc updated to the prefix form.
Switch the call-site marker from prefix to the glued/infix form `callee@CTX_SWITCH(args)` (handled in parsePostExp via a pendingCallerAttr stashed on @name and attached to the next CallExp). This removes the statement-level @-ambiguity the prefix form introduced (no Phobos regression). Enforcement is unchanged: E1 (propagation), E2 (must-mark), E3 (marker on a non-attr callee is an error) all still apply.
…reen baseline) Snapshot of the callerattr-option-a working tree that produces the ldc-base image (642510c03f462ee0) building wekapp weka 6.0.0.3189 green. - Layer 2: fold caller-attr into the TypeFunction type identity (foldParamCallerAttrs / foldFuncDeclCallerAttrs), asymmetric implicit-conversion rule (callerAttrsCovariant), type-level deco mangling, foreach/opApply overload selection reflection, clean hdrgen rendering. - druntime core.attribute: rename callerAttrFake -> callerAttrUnchecked. - Fix (this baseline): gate FAKE (@mayYieldUnchecked) caller-attrs OUT of the symbol mangle (dmangle.d mangleCallerAttrs: `if (fake) return`) so standalone boundary functions keep baseline names -> no cross-unit "undefined symbol" (e.g. ReactorThreadPool.submitTask); real @mayYield still mangles (YR...). Also invalidate fd.mangleString on the function-side fold (funcsem.d) so a mangle taken before the fold is recomputed. - Tests: callerattr* compilable/fail_compilation/runnable, incl. new callerattr_alias_overload / callerattr_overload.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Caller-required attributes ("Option A") —
@CTX_SWITCHAdds a caller-required attribute mechanism to the Weka LDC fork: a callee marked with a
caller-attribute (e.g.
@CTX_SWITCH, defined viacore.attribute.callerAttr!"CTX_SWITCH")must be acknowledged at every call site with a postfix marker, and the requirement
propagates to the calling function. This makes design-time-visible which call paths can
context-switch (yield a fiber), enforced by the compiler. A non-propagating variant
(
callerAttrFake) acts as a radius limiter.Semantics
callee(args) @CTX_SWITCH;— required iffcalleecarries the attribute.Calling a
@CTX_SWITCHfunction without the marker is an error ("must be marked"); putting themarker on a non-
@CTX_SWITCHcallee is an error ("remove the marker").@CTX_SWITCHcallee must itself be@CTX_SWITCH(or the*_FAKEvariant, which carries the attribute but stops propagation to its own callers).@CTX_SWITCHprefix on a delegate/function parameter typemakes the indirect call a context-switching callee (
dg() @CTX_SWITCH), and a context-switchinglambda may only bind to such a parameter. Both concrete and template value parameters are supported.
Commits
callee(args) @CTX_SWITCHsyntax — ergonomic call-site syntax.foreachover a context-switchingopApplypropagates the attribute to the enclosing function;an
opApplythat itself context-switches is marked accordingly.same-name/same-type overload sibling (e.g. a plain vs.
@CTX_SWITCHopApplypair). This fixesoverload-set collisions ("skipping definition … same mangled name") without breaking cross-module
linking — standalone caller-attr functions keep their baseline mangle, since discriminating every
caller-attr function produces inconsistent mangles across compilation units.
Tests
tests/dmd/compilable/callerattr.d— propagation, markers, delegates/function pointers, lambdas,*_FAKE.tests/dmd/fail_compilation/callerattr_foreach.d,callerattr_foreach_unmarked.d— foreach/opApply enforcement.Validated end-to-end by building the Weka
wekanodebinary against this compiler (the migration thatadds
@CTX_SWITCHannotations across the codebase lives in a separate wekapp branch).🤖 Generated with Claude Code