cgen: fix error for dereference mut interface in loop (fix #13913) (#13941)

pull/13966/head
yuyi 2022-04-07 17:01:54 +08:00 committed by GitHub
parent a58dde48f8
commit e7fd8c4e7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 4 deletions

View File

@ -775,7 +775,14 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
left_cc_type := g.cc_type(node.left_type, false)
left_type_name := util.no_dots(left_cc_type)
g.write('${c_name(left_type_name)}_name_table[')
g.expr(node.left)
if node.left.is_auto_deref_var() && node.left_type.nr_muls() > 1 {
g.write('(')
g.write('*'.repeat(node.left_type.nr_muls() - 1))
g.expr(node.left)
g.write(')')
} else {
g.expr(node.left)
}
dot := if left_is_shared {
'->val.'
} else if node.left_type.is_ptr() {
@ -785,7 +792,14 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
}
mname := c_name(node.name)
g.write('${dot}_typ]._method_${mname}(')
g.expr(node.left)
if node.left.is_auto_deref_var() && node.left_type.nr_muls() > 1 {
g.write('(')
g.write('*'.repeat(node.left_type.nr_muls() - 1))
g.expr(node.left)
g.write(')')
} else {
g.expr(node.left)
}
g.write('${dot}_object')
if node.args.len > 0 {
g.write(', ')

View File

@ -4,15 +4,15 @@ import rand
import rand.wyrand
import rand.splitmix64
fn main() {
fn test_deref_mut_interface_in_loop() {
mut wyrand_rng := &rand.PRNG(&wyrand.WyRandRNG{})
mut splitmix_rng := &rand.PRNG(&splitmix64.SplitMix64RNG{})
mut generators := [wyrand_rng, splitmix_rng]
for mut rng in generators {
seed_len := rng.block_size() / 32
// NB: `seed_len := (*rng).block_size() / 32` does compile
dump(seed_len)
println(rng.string(15))
assert seed_len == 2
}
}

View File

@ -0,0 +1,8 @@
## Run the tests here with: `v vlib/v/tests/known_errors/known_errors_test.v`
The intended use of this, is for providing samples, that currently do NOT compile,
but that a future compiler improvement WILL be able to compile, and to track,
whether they were not fixed incidentally, due to an unrelated change/improvement.
For example, code that triggers generating invalid C code can go here,
and later when a bug is fixed, can be moved to a proper _test.v or .vv/.out pair,
outside of the vlib/v/tests/known_errors/testdata/ folder.