You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Purity hole: foul trait methods are callable from pure context #172
A foul trait method called through a trait-typed receiver is not checked against the caller's purity. A direct call is caught:
foul realNow() -> Integer = 1
let pure2() -> Integer = realNow()
# error: Cannot call foul function 'realNow' from pure context
The same call through a trait is not:
trait Clock do
foul now : Integer
end
record FakeClock do
t: Integer
end
make FakeClock, implement: Clock do
foul now() -> Integer = @t
end
let pureStamp(c: Clock) -> String = "at ${c.now()}" # no error
main do
IO.printLine(pureStamp(FakeClock { t: 7 })) # prints "at 7"
end
pureStamp is let and performs a declared-foul call. The trait declares foul now, so the signature is present at the call site — this is not the inference problem, the marker is simply not consulted on trait-dispatched calls.
Distinct from #142, which is about the effect disappearing when a block becomes a value (Block<Void> is the same type pure or foul). Here nothing is erased: Clock.now is declared foul and the receiver is statically typed as Clock.
#143 rests on "traits already carry foul on required methods, and trait-typed parameters already dispatch." The dispatch half is true; the foul half is not enforced. Any capability design built on trait-shaped modules routes its effects through exactly this path, so capability traits would currently hide effects rather than track them.
Fixed on both backends (the check is in the shared typechecker, so -R reports it identically).
The bug was two holes, not one. The second is wider than this issue describes and needs no trait at all:
make Box do
foul bang() -> Integer do
IO.printLine("effect!")
return @n
end
end
let readBox(b: Box) -> Integer = b.bang() # compiled clean, printed "effect!"
The phase-1 purity check runs before type checking (analyzer.cxx:101-107), so it can only resolve module-qualified receivers by name — every foul method reached through a value escaped it. Reporting now happens in the existing post-typecheck pass, which has precise resolved-call foulness.
The trait case needed something else again: a trait-typed receiver has no resolved call target, because dispatch is dynamic. The trait declaration is the only place the effect is stated, so TypeChecker::traitMethodIsFoul was added next to traitRequires and is consulted directly.
Two things that cost time and are worth recording:
The dedup test against phase 1 cannot use receiverModuleName — it returns the name of any lowercase identifier, so a plain value receiver b looks module-qualified. It has to test isQualifiedCallFoul instead.
A trait-typed parameter is a ConstrainedType, not a NamedType; the trait name comes from con->traitName. The pass also had an if (resolved.empty()) return early exit that had to go, since a program whose only foul call goes through a trait has no resolved targets at all.
Specs: spec/purity_value_receiver.kex, spec/purity_trait_receiver.kex. Both run on BEAM too.
Activity
akoskovacs commentedon Aug 22, 2026
Fixed on both backends (the check is in the shared typechecker, so
-Rreports it identically).The bug was two holes, not one. The second is wider than this issue describes and needs no trait at all:
The phase-1 purity check runs before type checking (
analyzer.cxx:101-107), so it can only resolve module-qualified receivers by name — everyfoulmethod reached through a value escaped it. Reporting now happens in the existing post-typecheck pass, which has precise resolved-call foulness.The trait case needed something else again: a trait-typed receiver has no resolved call target, because dispatch is dynamic. The trait declaration is the only place the effect is stated, so
TypeChecker::traitMethodIsFoulwas added next totraitRequiresand is consulted directly.Two things that cost time and are worth recording:
receiverModuleName— it returns the name of any lowercase identifier, so a plain value receiverblooks module-qualified. It has to testisQualifiedCallFoulinstead.ConstrainedType, not aNamedType; the trait name comes fromcon->traitName. The pass also had anif (resolved.empty()) returnearly exit that had to go, since a program whose only foul call goes through a trait has no resolved targets at all.Specs:
spec/purity_value_receiver.kex,spec/purity_trait_receiver.kex. Both run on BEAM too.