-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
checker: fix missing struct field type checking for type mismatch (re…
- Loading branch information
Showing
3 changed files
with
61 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
vlib/v/checker/tests/struct_field_mismatch_ref_err.vv:6:26: error: field is not reference but default value is reference | ||
4 | struct SafeCounter { | ||
5 | mut: | ||
6 | mt sync.Mutex = sync.new_mutex() | ||
| ~~~~~~~~~~~ | ||
7 | pub mut: | ||
8 | value map[string]int |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import sync | ||
import time | ||
|
||
struct SafeCounter { | ||
mut: | ||
mt sync.Mutex = sync.new_mutex() | ||
pub mut: | ||
value map[string]int | ||
} | ||
|
||
fn (mut c SafeCounter) inc(key string) { | ||
c.mt.@lock() | ||
defer { | ||
c.mt.unlock() | ||
} | ||
c.value[key]++ | ||
} | ||
|
||
fn (mut c SafeCounter) value(key string) int { | ||
c.mt.@lock() | ||
defer { | ||
c.mt.unlock() | ||
} | ||
return c.value[key] | ||
} | ||
|
||
fn main() { | ||
mut counter := &SafeCounter{} | ||
|
||
for i := 0; i < 5; i++ { | ||
spawn fn [mut counter] () { | ||
for j := 0; j < 100; j++ { | ||
counter.inc('key') | ||
} | ||
}() | ||
} | ||
|
||
time.sleep(1 * time.second) | ||
println(counter.value('key')) | ||
} |