Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checker: check struct reference fields uninitialized (fix #19559) #19607

Closed
wants to merge 2 commits into from

Conversation

yuyi98
Copy link
Member

@yuyi98 yuyi98 commented Oct 20, 2023

This PR check struct reference fields uninitialized (fix #19559).

  • Check struct reference fields uninitialized.
  • Add test.
struct Viewport {
mut:
	parent &Window
}

pub struct Window {
mut:
	desktop_viewport ?&Viewport = none
	phone_viewport   ?&Viewport = none
}

fn main() {
	_ := &Window{}
}

PS D:\Test\v\tt1> v run .
tt1.v:8:32: warning: unnecessary default value of `none`: struct fields are zeroed by default
    6 | pub struct Window {
    7 | mut:
    8 |     desktop_viewport ?&Viewport = none
      |                                   ~~~~
    9 |     phone_viewport   ?&Viewport = none
   10 | }
tt1.v:9:32: warning: unnecessary default value of `none`: struct fields are zeroed by default
    7 | mut:
    8 |     desktop_viewport ?&Viewport = none
    9 |     phone_viewport   ?&Viewport = none
      |                                   ~~~~
   10 | }
   11 |
tt1.v:13:8: error: reference field `Window.desktop_viewport.parent` must be initialized (part of struct `Viewport`)
   11 | 
   12 | fn main() {
   13 |     _ := &Window{}
      |           ~~~~~~~~
   14 | }
tt1.v:13:8: error: reference field `Window.phone_viewport.parent` must be initialized (part of struct `Viewport`)
   11 | 
   12 | fn main() {
   13 |     _ := &Window{}
      |           ~~~~~~~~
   14 | }

@yuyi98 yuyi98 closed this Oct 20, 2023
@yuyi98 yuyi98 deleted the check_uninit_ref_field branch October 20, 2023 09:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

V error when not initializing ?&Type
1 participant