Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions bundler/build_tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ func buildRuntimeLib(runtimeDir, libsDir string, bits int) error {
for _, src := range entries {
obj := filepath.Join(objDir, strings.TrimSuffix(filepath.Base(src), ".c")+".o")
args := []string{"-std=c11", "-O2", "-Wall", "-Wextra", "-I", runtimeDir, "-c", src, "-o", obj}
if runtime.GOOS != "windows" {
args = append(args, "-pthread")
}
if bits == abi.Bits32 {
args = append(args[:1], append([]string{"-m32"}, args[1:]...)...)
}
Expand Down
35 changes: 35 additions & 0 deletions ferret_libs_dev/std/task.fer
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// std/task - explicit task/thread runtime surface.
//
// Current implementation is backed by OS threads.

import "std/mem"

type taskInner struct {}

type Handle struct {
raw: *taskInner
}

#[extern("ferret_task_run_raw")]
fn run_ref<T>(entry: fn(T) -> void, arg: T) -> ^taskInner;

#[extern("ferret_task_wait")]
fn wait_raw(handle: ^void) -> void;

fn Run<T>(callback: fn(T) -> void, arg: T) -> Handle {
unsafe {
return .{ .raw = mem::Adopt(run_ref(callback, arg)) }
Comment on lines +14 to +21
}
}

fn Handle::Wait(self) -> void {
unsafe {
wait_raw(mem::Expose(self.raw) as ^void)
}
}

fn WaitAll(handles: ...Handle) -> void {
for handles | handle | {
handle.Wait()
}
}
71 changes: 67 additions & 4 deletions internal/analysis/semantics/ownership/ownership.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ func (a *ownershipAnalyzer) collectValueLocalUses(used cfg.LocalSet, value mir.V
a.collectValueLocalUses(used, v.Left)
case *mir.CompositeValue:
for _, item := range v.Items {
a.collectValueLocalUses(used, item.Key)
a.collectValueLocalUses(used, item.Value)
}
case *mir.InterfaceValue:
Expand All @@ -554,7 +555,9 @@ func (a *ownershipAnalyzer) checkComputedValue(scope *valueScope, instr *mir.Com
a.checkValue(scope, instr.Value)
if info, ok := a.tempInfoForValue(instr.Value); ok {
a.temps[instr.TargetID] = info
return
}
a.consumeMoveValue(scope, instr.Value, a.localType(instr.TargetID))
}

func (a *ownershipAnalyzer) checkValue(scope *valueScope, value mir.Value) {
Expand Down Expand Up @@ -602,6 +605,7 @@ func (a *ownershipAnalyzer) checkValue(scope *valueScope, value mir.Value) {
a.checkValue(scope, v.Left)
case *mir.CompositeValue:
for _, item := range v.Items {
a.checkValue(scope, item.Key)
a.checkValue(scope, item.Value)
}
case *mir.InterfaceValue:
Expand Down Expand Up @@ -845,10 +849,17 @@ func (a *ownershipAnalyzer) consumeMoveValue(scope *valueScope, value mir.Value,
if value == nil {
return
}
if unary, ok := value.(*mir.UnaryValue); ok && unary.Op == "copy" {
return
}
if ifaceValue, ok := value.(*mir.InterfaceValue); ok {
a.consumeMoveValue(scope, ifaceValue.Value, ifaceValue.ConcreteType)
return
}
if composite, ok := value.(*mir.CompositeValue); ok {
a.consumeCompositeMoveValue(scope, composite, typ)
return
}
if local, ok := value.(*mir.LocalValue); ok && scope != nil {
if slot, ok := scope.Lookup(local.LocalID); ok && slot != nil && a.isMoveType(slot.concrete) {
a.consumeLocalPath(scope, local.LocalID, "", value.Loc())
Expand All @@ -859,10 +870,6 @@ func (a *ownershipAnalyzer) consumeMoveValue(scope *valueScope, value mir.Value,
return
}
switch v := value.(type) {
case *mir.UnaryValue:
if v.Op == "copy" {
return
}
case *mir.LocalValue:
if info, ok := a.temps[v.LocalID]; ok {
if !info.root.isLocal() {
Expand All @@ -881,6 +888,62 @@ func (a *ownershipAnalyzer) consumeMoveValue(scope *valueScope, value mir.Value,
a.consumeLocalPath(scope, root, path, value.Loc())
}

func (a *ownershipAnalyzer) consumeCompositeMoveValue(scope *valueScope, value *mir.CompositeValue, typ typeinfo.Type) {
if value == nil {
return
}
for i, item := range value.Items {
keyType, valueType := a.compositeItemTypes(typ, item, i)
a.consumeMoveValue(scope, item.Key, keyType)
a.consumeMoveValue(scope, item.Value, valueType)
}
}

func (a *ownershipAnalyzer) compositeItemTypes(typ typeinfo.Type, item mir.CompositeItem, index int) (typeinfo.Type, typeinfo.Type) {
base := a.compositePayloadType(typ)
if base == nil || typeinfo.IsInvalid(base) || typeinfo.IsUnknown(base) {
return valueType(item.Key), valueType(item.Value)
}
switch t := base.(type) {
case *typeinfo.StructType:
if item.Name != "" {
if field := t.Fields[item.Name]; field != nil {
return nil, field.Type
}
}
if index >= 0 && index < len(t.OrderedFields) && t.OrderedFields[index] != nil {
return nil, t.OrderedFields[index].Type
}
case *typeinfo.TupleType:
if index >= 0 && index < len(t.Elems) {
return nil, t.Elems[index]
}
case *typeinfo.ArrayType:
return nil, t.Inner
case *typeinfo.SliceType:
return nil, t.Inner
case *typeinfo.MapType:
return t.Key, t.Value
}
return valueType(item.Key), valueType(item.Value)
}

func (a *ownershipAnalyzer) compositePayloadType(typ typeinfo.Type) typeinfo.Type {
for typ != nil && !typeinfo.IsInvalid(typ) && !typeinfo.IsUnknown(typ) {
switch t := a.underlying(typ).(type) {
case *typeinfo.ApproxType:
typ = t.Inner
case *typeinfo.OptionalType:
typ = t.Inner
case *typeinfo.ErrorUnionType:
typ = t.Value
default:
return t
}
}
return typ
}

func (a *ownershipAnalyzer) consumeLocalPath(scope *valueScope, root int, path string, loc source.Location) {
if scope == nil || root < 0 {
return
Expand Down
81 changes: 81 additions & 0 deletions internal/analysis/semantics/ownership/ownership_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,70 @@ fn main(n: Node) -> i32 {
t.Fatalf("expected %s diagnostic, got %#v", diagnostics.ErrUseAfterMove, result.Diagnostics.Diagnostics())
}

func TestOwnershipPhaseRejectsMoveAfterStructLiteralFieldMove(t *testing.T) {
assertOwnershipUseAfterMove(t, `
type Node struct {}

type Box struct {
Item: *Node
}

fn take(box: Box) -> void {
box
}

fn main(node: *Node) -> *Node {
take(.{ .Item = node })
return node
}
`)
}

func TestOwnershipPhaseRejectsMoveAfterTupleLiteralElementMove(t *testing.T) {
assertOwnershipUseAfterMove(t, `
type Node struct {}

fn take(pair: (*Node, i32)) -> void {
pair
}

fn main(node: *Node) -> *Node {
take((node, 1))
return node
}
`)
}

func TestOwnershipPhaseRejectsMoveAfterSliceLiteralElementMove(t *testing.T) {
assertOwnershipUseAfterMove(t, `
type Node struct {}

fn take(nodes: []*Node) -> void {
nodes
}

fn main(node: *Node) -> *Node {
take([]*Node{node})
return node
}
`)
}

func TestOwnershipPhaseRejectsMoveAfterVariadicArgumentMove(t *testing.T) {
assertOwnershipUseAfterMove(t, `
type Node struct {}

fn take(nodes: ...*Node) -> void {
nodes
}

fn main(node: *Node) -> *Node {
take(node)
return node
}
`)
}

func TestOwnershipPhaseAllowsPlainValueReceiverMethodReuse(t *testing.T) {
root := t.TempDir()
mustWriteOwnership(t, filepath.Join(root, "main.fer"), `
Expand Down Expand Up @@ -1049,6 +1113,23 @@ fn main() -> void {
t.Fatalf("expected %s diagnostic, got %#v", diagnostics.ErrUseAfterMove, result.Diagnostics.Diagnostics())
}

func assertOwnershipUseAfterMove(t *testing.T, source string) {
t.Helper()
root := t.TempDir()
mustWriteOwnership(t, filepath.Join(root, "main.fer"), source)

result := compiler.New(root, ".fer", diagnostics.NewDiagnosticBag("")).ParseEntry(filepath.Join(root, "main.fer"))
if result.Entry == nil || result.Entry.Phase < phase.PhaseOwnershipAnalyzed {
t.Fatalf("expected ownership analyzed phase, got %#v", result.Entry)
}
for _, diag := range result.Diagnostics.Diagnostics() {
if diag.Code == diagnostics.ErrUseAfterMove {
return
}
}
t.Fatalf("expected %s diagnostic, got %#v", diagnostics.ErrUseAfterMove, result.Diagnostics.Diagnostics())
}

func mustWriteOwnership(t *testing.T, path, content string) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/analysis/semantics/typechecker/const_eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ func allowsConstValueCache(node ast.Node) bool {
case *ast.ConstDecl, *ast.ConstStmt:
return true
case *ast.LetDecl:
return n != nil && !n.IsMut
return n != nil && !n.IsMut && !n.IsAtomic
case *ast.LetStmt:
return n != nil && !n.IsMut
return n != nil && !n.IsMut && !n.IsAtomic
default:
return false
}
Expand Down
4 changes: 2 additions & 2 deletions internal/analysis/semantics/typechecker/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ func (c *checker) symbolMutable(sym *symbols.Symbol) bool {
}
switch node := sym.Node.(type) {
case *ast.LetDecl:
return node != nil && node.IsMut
return node != nil && (node.IsMut || node.IsAtomic)
case *ast.LetStmt:
return node != nil && node.IsMut
return node != nil && (node.IsMut || node.IsAtomic)
case *ast.ConstDecl, *ast.ConstStmt:
return false
default:
Expand Down
5 changes: 4 additions & 1 deletion internal/analysis/semantics/typechecker/stmts.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ func (c *checker) checkStmt(scope *refineScope, stmt ast.Stmt) {
if finalType == nil {
finalType = typeinfo.UnknownType{}
}
if s.Type != nil && declared != nil && !typeinfo.Equal(declared, finalType) {
if s.IsAtomic {
finalType = c.atomicStorageType(s.Loc(), finalType)
}
if !s.IsAtomic && s.Type != nil && declared != nil && !typeinfo.Equal(declared, finalType) {
c.info.BindNode(s.Type, finalType)
}
if declared != nil && s.Value != nil && !c.checkExprAssignable(scope, s.Value, declared, value) {
Expand Down
24 changes: 20 additions & 4 deletions internal/analysis/semantics/typechecker/symbol_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,18 @@ func (c *checker) synthesizeSymbolType(mod *context.Module, sym *symbols.Symbol)
switch n := sym.Node.(type) {
case *ast.LetDecl:
if n.Type != nil {
return c.typeFromSyntax(mod, n.Type)
typ := c.typeFromSyntax(mod, n.Type)
if n.IsAtomic {
return c.atomicStorageType(n.Loc(), typ)
}
return typ
}
if n.Value != nil {
return c.typeOfExpr(nil, n.Value, nil)
typ := c.typeOfExpr(nil, n.Value, nil)
if n.IsAtomic {
return c.atomicStorageType(n.Loc(), typ)
}
return typ
}
case *ast.ConstDecl:
if n.Type != nil {
Expand All @@ -69,10 +77,18 @@ func (c *checker) synthesizeSymbolType(mod *context.Module, sym *symbols.Symbol)
}
case *ast.LetStmt:
if n.Type != nil {
return c.typeFromSyntax(mod, n.Type)
typ := c.typeFromSyntax(mod, n.Type)
if n.IsAtomic {
return c.atomicStorageType(n.Loc(), typ)
}
return typ
}
if n.Value != nil {
return c.typeOfExpr(nil, n.Value, nil)
typ := c.typeOfExpr(nil, n.Value, nil)
if n.IsAtomic {
return c.atomicStorageType(n.Loc(), typ)
}
return typ
}
}
if sym.Node == nil {
Expand Down
2 changes: 2 additions & 0 deletions internal/analysis/semantics/typechecker/syntax_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ func (c *checker) typeFromSyntax(mod *context.Module, expr ast.TypeExpr) typeinf
return &typeinfo.PointerType{Inner: inner}
case *ast.RefType:
return &typeinfo.RefType{Mutable: t.Mutable, Inner: c.typeFromSyntax(mod, t.Inner)}
case *ast.AtomicType:
return c.atomicStorageType(t.Loc(), c.typeFromSyntax(mod, t.Inner))
case *ast.RawPtrType:
inner := c.typeFromSyntax(mod, t.Inner)
if t.Inner == nil || typeinfo.IsBuiltinNamed(inner, "void") {
Expand Down
Loading
Loading