Credence.Semantic.RemoveUnusedTypespecWhenVar
(credence v0.8.0)
Copy Markdown
Fixes compiler errors caused by unused type variables in @spec clauses.
When an LLM generates a typespec with a when clause that defines a type
variable (e.g., when var_ok: true) but that variable is referenced only
once (in the when clause itself), the Elixir compiler rejects it:
type variable var_ok is used only once. Type variables in typespecs
must be referenced at least twice, otherwise it is equivalent to term()Because the compiler counts the when binding as the variable's only use,
the named variable is guaranteed not to appear in the spec's args or return
type. The fix therefore removes only the offending binding from the when
clause, leaving every other binding (and the spec itself) untouched:
# Before (compiler error)
@spec foo(x) :: x when x: integer, var_ok: true
# After (compiles cleanly, `x` constraint preserved)
@spec foo(x) :: x when x: integerWhen the offending binding is the only one in the when clause, the whole
clause is dropped:
# Before
@spec foo(integer) :: integer when var_ok: true
# After
@spec foo(integer) :: integer