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

checker, cgen: fix for c stmt with option or result calls (fix #19630) #19641

Merged
merged 1 commit into from Oct 24, 2023

Conversation

yuyi98
Copy link
Member

@yuyi98 yuyi98 commented Oct 24, 2023

This PR fix for c stmt with option or result calls (fix #19630).

  • Fix for c stmt with option or result calls.
  • Add test.
module main

fn foo() !int {
	return 5
}

fn main() {
	mut pos := 0

	for ; pos > 10; pos = foo()! {
		println(pos)
	}

	for pos = foo()!; pos > 10; {
		println(pos)
	}

	assert true
}

PS D:\Test\v\tt1> v run .  
import rand
import math { abs }

const nmax = 20

fn ana(n int) f64 {
	return 1.0 // haven't started
}

fn avg(n int) f64 {
	tests := 1e4
	mut sum := 0
	for t := 0; t < tests; t++ {
		mut v := []bool{len: nmax, init: false}
		for x := 0; !v[x]; x = rand.intn(n) or { 0 } {
			v[x] = true
			sum++
		}
	}
	return f64(sum) / tests
}

fn main() {
	println(' N   average   analytical   (error)')
	println('=== ========= ============ =========')
	for n in 1 .. nmax + 1 {
		a := avg(n)
		b := ana(n)
		println('${n:3} ${a:9.4f} ${b:12.4f} (${(abs(a - b) / b * 100):6.2f}%)')
	}
	assert true
}

PS D:\Test\v\tt1> v run .
 N   average   analytical   (error)
=== ========= ============ =========
  1    1.0000       1.0000 (  0.00%)
  2    1.5047       1.0000 ( 50.47%)
  3    1.6645       1.0000 ( 66.45%)
  4    1.7550       1.0000 ( 75.50%)
  5    1.7972       1.0000 ( 79.72%)
  6    1.8323       1.0000 ( 83.23%)
  7    1.8565       1.0000 ( 85.65%)
  8    1.8808       1.0000 ( 88.08%)
  9    1.8890       1.0000 ( 88.90%)
 10    1.8931       1.0000 ( 89.31%)
 11    1.9026       1.0000 ( 90.26%)
 12    1.9171       1.0000 ( 91.71%)
 13    1.9179       1.0000 ( 91.79%)
 14    1.9242       1.0000 ( 92.42%)
 15    1.9329       1.0000 ( 93.29%)
 16    1.9389       1.0000 ( 93.89%)
 17    1.9399       1.0000 ( 93.99%)
 18    1.9449       1.0000 ( 94.49%)
 19    1.9498       1.0000 ( 94.98%)
 20    1.9534       1.0000 ( 95.34%)

@spytheman spytheman merged commit 82d2dd5 into vlang:master Oct 24, 2023
51 checks passed
@yuyi98 yuyi98 deleted the fix_for_c_stmt_with_opt_res branch October 24, 2023 12:10
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.

Poor compilation warning for error propogation in loop
2 participants