Skip to content

Commit 08fb650

Browse files
committed
capabilities are erased at run time: a value type (a fn or extern result, an enum field, a type arg of one) that names a capability is error[capability-erased]; a capability is only a {} binder or a handle local
1 parent ea44b76 commit 08fb650

8 files changed

Lines changed: 79 additions & 18 deletions

File tree

‎bootstrap/s2c-seed.c‎

Lines changed: 22 additions & 14 deletions
Large diffs are not rendered by default.

‎s2/types.srs‎

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ enum Ty.Clash:
3737
case Ty.CapValue(line: I64, x: Sym.Sym)
3838
case Ty.BinderHides(line: I64, x: Sym.Sym)
3939
case Ty.CapParam(line: I64, x: Sym.Sym)
40+
// a value type (a result, a field, a type arg of one) that names capability p: capabilities are erased (vic 2026-11-09)
41+
case Ty.CapErased(line: I64, p: Sym.Sym)
4042
case Ty.TraitArity(line: I64, t: Sym.Sym)
4143
case Ty.NotMethod(line: I64, m: Sym.Sym)
4244
case Ty.MissingMethod(line: I64, m: Sym.Sym)
@@ -646,7 +648,7 @@ def Ty.fn(cx: Ty.Cx, st0: Ty.St, f: Ast.Fn, row: Coll.List[Eff.Cap]): Ty.FnEx =
646648
val rt = Ty.term(ret, tv)
647649
val caps = Coll.revOnto(Coll.rev(Ty.capNames(protos, ev, Coll.Nil)), Ty.rowNames(row))
648650
val eps = Ty.elParams(ps, tv, Coll.Nil)
649-
val st = Ty.fnShape(cx, st0, line, tps, ps)
651+
val st = Ty.fnShape(cx, st0, line, tps, ps, ret)
650652
body match
651653
case Ast.Extern => Ty.FnEx(st, El.Fn(line, name, tps, caps, eps, rt, El.Extern))
652654
case Ast.Def(_, e) =>
@@ -763,6 +765,9 @@ def Ty.decls(cx: Ty.Cx, st: Ty.St, ds: Coll.List[Ast.Decl], acc: Coll.List[El.Fn
763765
val Ty.Fns(st1, efs, _, _) = Ty.givenFns(Ty.byGiven(cx), Ty.givenShape(cx, st, g), tn, Ty.underGroups(gs, fs, Coll.Nil), Coll.Nil)
764766
val gv = El.Given(Ty.tparams(gs, Coll.Nil), Ty.binderNames(Ty.evidence(gs, Coll.Nil), Coll.Nil), Ty.term(head, Coll.Nil), efs)
765767
Ty.decls(cx, st1, rest, acc, hacc, Coll.Cons(gv, gacc))
768+
case Ast.DEnum(e) =>
769+
val Ast.Enum(_, _, _, cases) = e
770+
Ty.decls(cx, Ty.erasedFields(cx, st, cases), rest, acc, hacc, gacc)
766771
case _ => Ty.decls(cx, st, rest, acc, hacc, gacc)
767772

768773
def Ty.underGroups(gs: Coll.List[Ast.Group], fs: Coll.List[Ast.Fn], acc: Coll.List[Ast.Fn]): Coll.List[Ast.Fn] = fs match
@@ -1110,9 +1115,32 @@ def Ty.isCapOf(s: Coll.IMap[K.Term], bs: Coll.List[Ty.Bind], x: Sym.Sym, p: Sym.
11101115
case K.Con(n, args) => Sym.eq(n, p) && Coll.length(args) == 0
11111116
case _ => false
11121117

1113-
// a [] binder may not hide a type; a capability is a {} binder, never a value param
1114-
def Ty.fnShape(cx: Ty.Cx, st: Ty.St, line: I64, tps: Coll.List[Sym.Sym], ps: Coll.List[Ast.Binder]): Ty.St =
1115-
Ty.capParams(cx, Ty.hides(cx, st, line, tps), ps)
1118+
// a [] binder may not hide a type; a capability is a {} binder, never a value param, and erased (vic 2026-11-09): no
1119+
// result type names one
1120+
def Ty.fnShape(cx: Ty.Cx, st: Ty.St, line: I64, tps: Coll.List[Sym.Sym], ps: Coll.List[Ast.Binder], ret: Ast.Ty): Ty.St =
1121+
Ty.erased(cx, Ty.capParams(cx, Ty.hides(cx, st, line, tps), ps), ret)
1122+
1123+
// a value type that names a capability, at any depth: error[capability-erased]
1124+
def Ty.erased(cx: Ty.Cx, st: Ty.St, t: Ast.Ty): Ty.St =
1125+
val Ast.Ty(line, n, args, _, _) = t
1126+
val Ty.Cx(_, _, protos, _, _, _, _, _, _) = cx
1127+
Ty.erasedAll(cx, if Ty.isProto(protos, n) then Ty.clash(st, Ty.CapErased(line, n)) else st, args)
1128+
1129+
def Ty.erasedAll(cx: Ty.Cx, st: Ty.St, ts: Coll.List[Ast.Ty]): Ty.St = ts match
1130+
case Coll.Nil => st
1131+
case Coll.Cons(t, rest) => Ty.erasedAll(cx, Ty.erased(cx, st, t), rest)
1132+
1133+
def Ty.erasedFields(cx: Ty.Cx, st: Ty.St, cs: Coll.List[Ast.Case]): Ty.St = cs match
1134+
case Coll.Nil => st
1135+
case Coll.Cons(c, rest) =>
1136+
val Ast.Case(_, _, fs) = c
1137+
Ty.erasedFields(cx, Ty.erasedAll(cx, st, Ty.binderTys(fs)), rest)
1138+
1139+
def Ty.binderTys(bs: Coll.List[Ast.Binder]): Coll.List[Ast.Ty] = bs match
1140+
case Coll.Nil => Coll.Nil
1141+
case Coll.Cons(b, rest) =>
1142+
val Ast.Binder(_, _, t) = b
1143+
Coll.Cons(t, Ty.binderTys(rest))
11161144

11171145
def Ty.hides(cx: Ty.Cx, st: Ty.St, line: I64, tps: Coll.List[Sym.Sym]): Ty.St = tps match
11181146
case Coll.Nil => st

‎s2c/main.srs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ def S2c.showClashes(fs: Coll.List[S2c.File], s: Coll.IMap[K.Term], cs: Coll.List
140140
case Ty.CapValue(l, x) => S2c.diag(fs, "capability-value", l, "a capability is second-class, not a value:", S2c.Name(x), "pass it as a {} binder: f{c}(x)")
141141
case Ty.BinderHides(l, x) => S2c.diag(fs, "binder-hides-type", l, "a [] binder hides the type", S2c.Name(x), "rename the binder")
142142
case Ty.CapParam(l, x) => S2c.diag(fs, "capability-param", l, "a capability is a {} binder, not a param:", S2c.Name(x), "write it as {x: P} before the params")
143+
case Ty.CapErased(l, p) => S2c.diag(fs, "capability-erased", l, "a capability is erased at run time: no value type names", S2c.Name(p), "take it as a {} binder ({c: Console} or {Console}); it is never a result, a field or a type arg")
143144
case Ty.TraitArity(l, t) => S2c.diag(fs, "trait-arity", l, "wrong number of type args for the trait", S2c.Name(t), "give one type arg per [] binder of the trait")
144145
case Ty.NotMethod(l, m) => S2c.diag(fs, "not-a-method", l, "not a method of the trait:", S2c.Name(m), "a given defines exactly the trait's methods")
145146
case Ty.MissingMethod(l, m) => S2c.diag(fs, "missing-method", l, "the given misses the trait method", S2c.Name(m), "define it in the given")

‎tests/s2/neg/erased_arg.srs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// expect: error[capability-erased]
2+
// a capability is erased at run time: not inside a value type either
3+
enum Bag [A: Type]:
4+
case Bag(x: A)
5+
def mk(): Bag[Console] = mk()
6+
def main(): I64 = 0
7+
// expect: --> tests/s2/neg/erased_arg.srs:5

‎tests/s2/neg/erased_extern.srs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// expect: error[capability-erased]
2+
// a capability is erased at run time (vic 2026-11-09): an extern cannot make one
3+
extern def mkc(): Console
4+
def main(): I64 = 0
5+
// expect: --> tests/s2/neg/erased_extern.srs:3

‎tests/s2/neg/erased_field.srs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// expect: error[capability-erased]
2+
// a capability is erased at run time: not stored in a field
3+
enum Box:
4+
case Box(c: Console)
5+
def main(): I64 = 0
6+
// expect: --> tests/s2/neg/erased_field.srs:4

‎tests/s2/neg/erased_result.srs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// expect: error[capability-erased]
2+
// a capability is erased at run time: not a result
3+
def mk(): Console = mk()
4+
def main(): I64 = 0
5+
// expect: --> tests/s2/neg/erased_result.srs:3

‎tests/s2/types.srs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def TT.show(c: Ty.Clash): Unit = c match
4343
case Ty.CapValue(l, x) => TT.sayN("capvalue", l, x)
4444
case Ty.BinderHides(l, x) => TT.sayN("binderhides", l, x)
4545
case Ty.CapParam(l, x) => TT.sayN("capparam", l, x)
46+
case Ty.CapErased(l, p) => TT.sayN("caperased", l, p)
4647
case Ty.TraitArity(l, t) => TT.sayN("traitarity", l, t)
4748
case Ty.NotMethod(l, m) => TT.sayN("notmethod", l, m)
4849
case Ty.MissingMethod(l, m) => TT.sayN("missingmethod", l, m)

0 commit comments

Comments
 (0)