Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgen: fix gen wrong for thread wrappers, when spawn with anon-fn array args(fix #19425) #20241

Merged
merged 2 commits into from
Dec 22, 2023

Conversation

shove70
Copy link
Contributor

@shove70 shove70 commented Dec 21, 2023

  1. Fixed C gen error when spawning function with mutable type #19425
  2. Add tests.
module main

import time
import io
import os

interface Message {
	a int
}

struct Impl {
	a int
}

type HookFunc = fn (Message) ?Message

struct TestStruct {
	fns []HookFunc
}

fn (ts TestStruct) run(mut writer io.Writer) {
	spawn handle_fns(ts.fns, mut writer)
}

fn handle_fns(fns []HookFunc, mut writer io.Writer) {
	mut message := Message(Impl{a: 1})
	for f in fns {
		message = f(message) or { continue }
	}
	println(message)
}

fn main() {
	stack := TestStruct{
		fns: [
			fn (m Message) ?Message {
				return Message(Impl{a: m.a + 1})
			},
			fn (m Message) ?Message {
				return none
			}
		]
	}
	stack.run(mut os.stdout())
	time.sleep(2 * time.second)
}

outputs:

Message(Impl{
    a: 2
})

@spytheman spytheman merged commit 869bac1 into vlang:master Dec 22, 2023
54 checks passed
@shove70 shove70 deleted the patch-5 branch December 22, 2023 07:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

C gen error when spawning function with mutable type
2 participants