diff --git a/lib/Drop.bux b/lib/Drop.bux new file mode 100644 index 0000000..4069081 --- /dev/null +++ b/lib/Drop.bux @@ -0,0 +1,4 @@ +// Drop trait — automatic cleanup for heap-allocated or resource-holding types +interface Drop { + func Drop(self: *Self); +} diff --git a/src/sema.bux b/src/sema.bux index 050787b..c41cb7a 100644 --- a/src/sema.bux +++ b/src/sema.bux @@ -1148,11 +1148,24 @@ func Sema_CollectGlobals(sema: *Sema) { let implTypeName: String = decl.strValue; var m: *Decl = decl.childDecl1; while m != null as *Decl { - if m.kind == dkFunc && sema.methodCount < 256 { - sema.methodEntries[sema.methodCount].typeName = implTypeName; - sema.methodEntries[sema.methodCount].methodName = m.strValue; - sema.methodEntries[sema.methodCount].decl = m; - sema.methodCount = sema.methodCount + 1; + if m.kind == dkFunc { + if sema.methodCount < 256 { + sema.methodEntries[sema.methodCount].typeName = implTypeName; + sema.methodEntries[sema.methodCount].methodName = m.strValue; + sema.methodEntries[sema.methodCount].decl = m; + sema.methodCount = sema.methodCount + 1; + } + // Also define TypeName_MethodName in scope so auto-drop can find it + let methodSymName: String = String_Concat(implTypeName, "_"); + let methodSymName2: String = String_Concat(methodSymName, m.strValue); + var methodSym: Symbol; + Sema_ZeroInitSymbol(&methodSym); + methodSym.kind = skFunc; + methodSym.name = methodSymName2; + methodSym.typeKind = tyFunc; + methodSym.isPublic = decl.isPublic; + methodSym.decl = m; + discard Scope_Define(sema.scope, methodSym); } m = m.childDecl2; }