Skip to content

Commit

Permalink
checker: warn -> error for uninitialized ref fields
Browse files Browse the repository at this point in the history
  • Loading branch information
medvednikov committed Sep 24, 2023
1 parent 7bfb35d commit a1f5552
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 34 deletions.
3 changes: 2 additions & 1 deletion ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@
- [ ] -skip-unused on by default
- [ ] 64/32 bit int depending on arch (will remove array.len limitation on 64 bit systems)
- [ ] `copy()` builtin function (e.g. for easier conversion from `[]Foo` to `[4]Foo`)
- [ ] Lambdas: `a.sort(|a, b| a > b)`
- [x] Lambdas: `a.sort(|a, b| a > b)`
- [ ] Custom attributes.
- [ ] `arr.first() or { }` like `arr[0] or { }`

## [Version 1.0]

Expand Down
4 changes: 2 additions & 2 deletions vlib/v/checker/struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.',
if field.typ.is_ptr() && !field.typ.has_flag(.shared_f)
&& !field.typ.has_flag(.option) && !node.has_update_expr && !c.pref.translated
&& !c.file.is_translated {
c.warn('reference field `${type_sym.name}.${field.name}` must be initialized',
c.error('reference field `${type_sym.name}.${field.name}` must be initialized',
node.pos)
continue
}
Expand Down Expand Up @@ -840,7 +840,7 @@ fn (mut c Checker) check_ref_fields_initialized(struct_sym &ast.TypeSymbol, mut
}
if field.typ.is_ptr() && !field.typ.has_flag(.shared_f) && !field.typ.has_flag(.option)
&& !field.has_default_expr {
c.warn('reference field `${linked_name}.${field.name}` must be initialized (part of struct `${struct_sym.name}`)',
c.error('reference field `${linked_name}.${field.name}` must be initialized (part of struct `${struct_sym.name}`)',
node.pos)
continue
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
vlib/v/checker/tests/reference_field_must_be_initialized.vv:8:7: warning: reference field `Node.next` must be initialized
vlib/v/checker/tests/reference_field_must_be_initialized.vv:8:7: error: reference field `Node.next` must be initialized
6 |
7 | fn main(){
8 | n := Node{ data: 123 }
Expand Down
9 changes: 1 addition & 8 deletions vlib/v/checker/tests/struct_field_reference_type_err.out
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
vlib/v/checker/tests/struct_field_reference_type_err.vv:12:16: warning: reference field `Animal.duck.age` must be initialized (part of struct `Duck`)
10 |
11 | fn main() {
12 | mut animal := Animal{
| ~~~~~~~
13 | ageee: 20
14 | }
vlib/v/checker/tests/struct_field_reference_type_err.vv:12:16: warning: reference field `Duck.age` must be initialized
vlib/v/checker/tests/struct_field_reference_type_err.vv:12:16: error: reference field `Animal.duck.age` must be initialized (part of struct `Duck`)
10 |
11 | fn main() {
12 | mut animal := Animal{
Expand Down
24 changes: 2 additions & 22 deletions vlib/v/checker/tests/struct_ref_fields_uninitialized_err.out
Original file line number Diff line number Diff line change
@@ -1,31 +1,11 @@
vlib/v/checker/tests/struct_ref_fields_uninitialized_err.vv:25:7: warning: reference field `Outer.c1.b` must be initialized (part of struct `ContainsRef`)
vlib/v/checker/tests/struct_ref_fields_uninitialized_err.vv:25:7: error: reference field `Outer.c1.b` must be initialized (part of struct `ContainsRef`)
23 |
24 | fn main() {
25 | _ := Outer{}
| ~~~~~~~
26 | _ := Struct{}
27 | }
vlib/v/checker/tests/struct_ref_fields_uninitialized_err.vv:25:7: warning: reference field `ContainsRef.b` must be initialized
23 |
24 | fn main() {
25 | _ := Outer{}
| ~~~~~~~
26 | _ := Struct{}
27 | }
vlib/v/checker/tests/struct_ref_fields_uninitialized_err.vv:25:7: warning: reference field `Outer.c2.b` must be initialized (part of struct `ContainsRef`)
23 |
24 | fn main() {
25 | _ := Outer{}
| ~~~~~~~
26 | _ := Struct{}
27 | }
vlib/v/checker/tests/struct_ref_fields_uninitialized_err.vv:26:7: warning: reference field `Struct.ref2` must be initialized
24 | fn main() {
25 | _ := Outer{}
26 | _ := Struct{}
| ~~~~~~~~
27 | }
vlib/v/checker/tests/struct_ref_fields_uninitialized_err.vv:26:7: warning: reference field `EmbedStruct.ref2` must be initialized
vlib/v/checker/tests/struct_ref_fields_uninitialized_err.vv:26:7: error: reference field `Struct.ref2` must be initialized
24 | fn main() {
25 | _ := Outer{}
26 | _ := Struct{}
Expand Down
1 change: 1 addition & 0 deletions vlib/vweb/vweb.v
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ pub fn (mut ctx Context) redirect(url string) Result {

// Send an not_found response
pub fn (mut ctx Context) not_found() Result {
// TODO add a [must_be_returned] attribute, so that the caller is forced to use `return app.not_found()`
if ctx.done {
return Result{}
}
Expand Down

0 comments on commit a1f5552

Please sign in to comment.