fmt: enable shared return types (#8614)

pull/8615/head
Uwe Krüger 2021-02-06 21:25:06 +01:00 committed by GitHub
parent 5343f1374b
commit cf230644b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 85 additions and 3 deletions

View File

@ -26,7 +26,8 @@ pub fn (node &FnDecl) stringify(t &table.Table, cur_mod string, m2a map[string]s
}
mut receiver := ''
if node.is_method {
mut styp := util.no_cur_mod(t.type_to_code(node.receiver.typ), cur_mod)
mut styp := util.no_cur_mod(t.type_to_code(node.receiver.typ.clear_flag(.shared_f)),
cur_mod)
m := if node.rec_mut { node.receiver.typ.share().str() + ' ' } else { '' }
if node.rec_mut {
styp = styp[1..] // remove &
@ -88,7 +89,7 @@ pub fn (node &FnDecl) stringify(t &table.Table, cur_mod string, m2a map[string]s
f.write(arg.typ.share().str() + ' ')
}
f.write(arg.name)
mut s := t.type_to_str(arg.typ)
mut s := t.type_to_str(arg.typ.clear_flag(.shared_f))
if arg.is_mut {
// f.write(' mut')
if s.starts_with('&') {

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/shared_type_mismatch.vv:13:3: error: wrong return type `St` in the `or {}` block, expected `shared St`
11 | fn test_shared_opt_bad() {
12 | shared yy := f() or {
13 | St{ x: 37.5 }
| ~~~~~~~~~~~~~
14 | }
15 | rlock yy {

View File

@ -0,0 +1,18 @@
struct St {
mut:
x f64
}
fn f() ?shared St {
shared x := St{ x: 12.75 }
return x
}
fn test_shared_opt_bad() {
shared yy := f() or {
St{ x: 37.5 }
}
rlock yy {
println(yy.x)
}
}

View File

@ -17,6 +17,34 @@ fn (shared x St) f(shared z St) {
}
}
fn g() shared St {
shared x := St{
a: 12
}
return x
}
fn h() ?shared St {
return error('no value')
}
fn k() {
shared x := g()
shared y := h() or {
shared f := St{}
f
}
shared z := h() ?
shared p := v
v := rlock z {
z.a
}
lock y, z; rlock x, p {
z.a = x.a + y.a
}
println(v)
}
const (
reads_per_thread = 30
read_threads = 10

View File

@ -18,6 +18,30 @@ fn (shared x St) f(shared z St) {
}
}
fn g() shared St {
shared x := St{a: 12 }
return x
}
fn h() ?shared St {
return error('no value')
}
fn k() {
shared x := g()
shared y := h() or {
shared f := St{}
f
}
shared z := h() ?
shared p := v
v := rlock z { z.a }
rlock x; lock y, z; rlock p {
z.a = x.a + y.a
}
println(v)
}
const (
reads_per_thread = 30
read_threads = 10

View File

@ -846,7 +846,11 @@ pub fn (mytable &Table) type_to_str_using_aliases(t Type, import_aliases map[str
res = mytable.shorten_user_defined_typenames(res, import_aliases)
}
}
nr_muls := t.nr_muls()
mut nr_muls := t.nr_muls()
if t.has_flag(.shared_f) {
nr_muls--
res = 'shared ' + res
}
if nr_muls > 0 {
res = strings.repeat(`&`, nr_muls) + res
}