Skip to content

Commit 6a69029

Browse files
authored
clarify static state section of macros proposal (#3145)
Actually resolve #1917 - I missed this section in the proposal. We already chose option 4 so I removed the other options and elaborated on the chosen strategy.
1 parent 421b979 commit 6a69029

File tree

1 file changed

+13
-24
lines changed

1 file changed

+13
-24
lines changed

working/macros/feature-specification.md

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,30 +1217,19 @@ independently. That order could become visible if separate macro applications
12171217
accessed the same static mutable state—top-level variables and static
12181218
fields.
12191219

1220-
We are still considering how to address this. Options:
1221-
1222-
1. Don't allow macro code to mutate static state at all. This is probably
1223-
overly-restrictive, and may be hard to enforce. There are legitimate use
1224-
cases for this like using `package:logging`.
1225-
1226-
2. Run each macro application in a separate isolate. Each application has its
1227-
own independent global mutable state. This is permissive in macros while
1228-
keeping them isolated, but may be slow.
1229-
1230-
3. Reset all static state between macro application executions. If this is
1231-
feasible to implement and fast enough, it could work.
1232-
1233-
4. Document that mutating static state is a bad practice, but don't block it.
1234-
Give no guarantees around static state persistence between macro
1235-
applications.
1236-
1237-
In practice, most macros won't access any static state, so this is harmless.
1238-
But if macros do exploit this (deliberately or inadvertently) then it could
1239-
force implementations to be stuck with a specific execution order in order
1240-
to not break existing code. This is the easiest and fastest solution, but
1241-
the least safe.
1242-
1243-
**TODO**: Choose a solution (#1917).
1220+
In an ideal world we might run each macro invocation in its own sandbox, with
1221+
its own static state, but we don't do that. Instead, we simply state that a well
1222+
behaved macro should not use static state in an observable way. Not doing so may
1223+
lead to undefined behavior.
1224+
1225+
For example, it is OK to cache the result of an expensive computation in global
1226+
state, assuming the result would always be the same. However it would not be
1227+
advisable to cache information pertaining to the observed state of a program.
1228+
Global state should also not be used to pass such information between macros, or
1229+
the phases of a given macro.
1230+
1231+
Implementations also reserve the right to clear the global state of a macro at
1232+
any time, possibly even at random.
12441233

12451234
### Platform semantics
12461235

0 commit comments

Comments
 (0)