diff --git a/examples/eventbus/modules/some_module/some_module.v b/examples/eventbus/modules/some_module/some_module.v index c2cf77f136..a3b7bfed55 100644 --- a/examples/eventbus/modules/some_module/some_module.v +++ b/examples/eventbus/modules/some_module/some_module.v @@ -30,5 +30,5 @@ pub fn do_work(){ } pub fn get_subscriber() eventbus.Subscriber { - return eb.subscriber + return *eb.subscriber } diff --git a/vlib/math/unsafe.v b/vlib/math/unsafe.v index 0c0dee8152..54a9f3b115 100644 --- a/vlib/math/unsafe.v +++ b/vlib/math/unsafe.v @@ -6,7 +6,7 @@ module math // with the sign bit of f and the result in the same bit position. // f32_bits(f32_from_bits(x)) == x. pub fn f32_bits(f f32) u32 { - p := &u32(&f) + p := *(&u32(&f)) return p } @@ -15,7 +15,7 @@ pub fn f32_bits(f f32) u32 { // and the result in the same bit position. // f32_from_bits(f32_bits(x)) == x. pub fn f32_from_bits(b u32) f32 { - p := &f32(&b) + p := *(&f32(&b)) return p } @@ -23,7 +23,7 @@ pub fn f32_from_bits(b u32) f32 { // with the sign bit of f and the result in the same bit position, // and f64_bits(f64_from_bits(x)) == x. pub fn f64_bits(f f64) u64 { - p := &u64(&f) + p := *(&u64(&f)) return p } @@ -32,7 +32,7 @@ pub fn f64_bits(f f64) u64 { // and the result in the same bit position. // f64_from_bits(f64_bits(x)) == x. pub fn f64_from_bits(b u64) f64 { - p := &f64(&b) + p := *(&f64(&b)) return p } diff --git a/vlib/rand/mt19937/mt19937.v b/vlib/rand/mt19937/mt19937.v index 90defcc126..5139fb31b3 100644 --- a/vlib/rand/mt19937/mt19937.v +++ b/vlib/rand/mt19937/mt19937.v @@ -72,7 +72,7 @@ fn calculate_state(seed_data []u32, mut state []u64) []u64 { for j := 1; j < nn; j++ { state[j] = u64(6364136223846793005) * (state[j - 1] ^ (state[j - 1] >> 62)) + u64(j) } - return state + return *state } // seed() - Set the seed, needs only two u32s in little endian format as [lower, higher] diff --git a/vlib/sokol/gfx/gfx_structs.v b/vlib/sokol/gfx/gfx_structs.v index a729052136..7810ef33d5 100644 --- a/vlib/sokol/gfx/gfx_structs.v +++ b/vlib/sokol/gfx/gfx_structs.v @@ -165,7 +165,7 @@ pub mut: pub fn (mut desc C.sg_shader_stage_desc) set_image(index int, name string) C.sg_shader_stage_desc { desc.images[index].name = name.str desc.images[index].@type = ._2d - return desc + return *desc } diff --git a/vlib/sync/pool.v b/vlib/sync/pool.v index 65eced2dfc..ca11d30d87 100644 --- a/vlib/sync/pool.v +++ b/vlib/sync/pool.v @@ -164,7 +164,7 @@ fn process_in_thread(mut pool PoolProcessor, task_id int) { pub fn (pool &PoolProcessor) get_string_item(idx int) string { // return *(&string(pool.items[idx])) // TODO: the below is a hack, remove it when v2 casting works again - return &string( pool.items[idx] ) + return *(&string( pool.items[idx] )) } // get_int_item - called by the worker callback. diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 40988b96e3..471423dcd0 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -400,7 +400,7 @@ pub mut: pub fn (i &Ident) var_info() IdentVar { match i.info as info { IdentVar { - return info + return *info } else { // return IdentVar{} diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 27dc58243d..5b95f3da04 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1515,6 +1515,11 @@ pub fn (mut c Checker) return_stmt(mut return_stmt ast.Return) { c.error('cannot use `$got_typ_sym.name` as type `$exp_typ_sym.name` in return argument', pos) } + if got_typ.is_ptr() && !exp_type.is_ptr() { + pos := return_stmt.exprs[i].position() + c.error('fn `$c.cur_fn.name` expects you to return a non reference type `${c.table.type_to_str(exp_type)}`, but you are returning `${c.table.type_to_str(got_typ)}` instead', + pos) + } } } @@ -1905,7 +1910,7 @@ fn const_int_value(cfield ast.ConstField) ?int { fn is_const_integer(cfield ast.ConstField) ?ast.IntegerLiteral { match cfield.expr { - ast.IntegerLiteral { return it } + ast.IntegerLiteral { return *it } else {} } return none diff --git a/vlib/v/checker/tests/return_ref_as_no_ref_bug.out b/vlib/v/checker/tests/return_ref_as_no_ref_bug.out new file mode 100644 index 0000000000..1f76d2088f --- /dev/null +++ b/vlib/v/checker/tests/return_ref_as_no_ref_bug.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/return_ref_as_no_ref_bug.v:11:9: error: fn `return_not_reference` expects you to return a non reference type `BugStruct`, but you are returning `&BugStruct` instead + 9 | + 10 | fn return_not_reference() BugStruct{ + 11 | return &BugStruct { + | ^ + 12 | id: 1 + 13 | } diff --git a/vlib/v/checker/tests/return_ref_as_no_ref_bug.vv b/vlib/v/checker/tests/return_ref_as_no_ref_bug.vv new file mode 100644 index 0000000000..8d04dc9bdb --- /dev/null +++ b/vlib/v/checker/tests/return_ref_as_no_ref_bug.vv @@ -0,0 +1,14 @@ +struct BugStruct { + id int +} + +fn main() { + x := return_not_reference() + println(x.id) +} + +fn return_not_reference() BugStruct{ + return &BugStruct { + id: 1 + } +} diff --git a/vlib/v/doc/doc.v b/vlib/v/doc/doc.v index d51f47a6ce..f3803500e0 100644 --- a/vlib/v/doc/doc.v +++ b/vlib/v/doc/doc.v @@ -431,7 +431,7 @@ fn (mut d Doc) generate() ?Doc { d.time_generated = time.now() d.contents.sort_by_name() d.contents.sort_by_category() - return d + return *d } pub fn generate(input_path string, pub_only, with_comments bool) ?Doc { diff --git a/vlib/v/table/atypes.v b/vlib/v/table/atypes.v index b25f2bd1a1..67b1ee5cdf 100644 --- a/vlib/v/table/atypes.v +++ b/vlib/v/table/atypes.v @@ -362,7 +362,7 @@ pub fn (t &TypeSymbol) str() string { [inline] pub fn (t &TypeSymbol) enum_info() Enum { match t.info { - Enum { return it } + Enum { return *it } else { panic('TypeSymbol.enum_info(): no enum info for type: $t.name') } } } @@ -370,7 +370,7 @@ pub fn (t &TypeSymbol) enum_info() Enum { [inline] pub fn (t &TypeSymbol) mr_info() MultiReturn { match t.info { - MultiReturn { return it } + MultiReturn { return *it } else { panic('TypeSymbol.mr_info(): no multi return info for type: $t.name') } } } @@ -378,7 +378,7 @@ pub fn (t &TypeSymbol) mr_info() MultiReturn { [inline] pub fn (t &TypeSymbol) array_info() Array { match t.info { - Array { return it } + Array { return *it } else { panic('TypeSymbol.array_info(): no array info for type: $t.name') } } } @@ -386,7 +386,7 @@ pub fn (t &TypeSymbol) array_info() Array { [inline] pub fn (t &TypeSymbol) array_fixed_info() ArrayFixed { match t.info { - ArrayFixed { return it } + ArrayFixed { return *it } else { panic('TypeSymbol.array_fixed(): no array fixed info for type: $t.name') } } } @@ -394,7 +394,7 @@ pub fn (t &TypeSymbol) array_fixed_info() ArrayFixed { [inline] pub fn (t &TypeSymbol) map_info() Map { match t.info { - Map { return it } + Map { return *it } else { panic('TypeSymbol.map_info(): no map info for type: $t.name') } } } @@ -402,7 +402,7 @@ pub fn (t &TypeSymbol) map_info() Map { [inline] pub fn (t &TypeSymbol) struct_info() Struct { match t.info { - Struct { return it } + Struct { return *it } else { panic('TypeSymbol.struct_info(): no struct info for type: $t.name') } } } diff --git a/vlib/v/tests/generics_test.v b/vlib/v/tests/generics_test.v index a7269fc827..0d6e4ce459 100644 --- a/vlib/v/tests/generics_test.v +++ b/vlib/v/tests/generics_test.v @@ -70,7 +70,7 @@ fn mut_arg(mut x T) { fn mut_arg2(mut x T) T { println(x.name) // = 'foo' - return x + return *x } fn test_create() {