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: fix incorrect checks when struct fields are ref and option type(fix #19555) #20195

Merged
merged 1 commit into from
Dec 16, 2023

Conversation

shove70
Copy link
Contributor

@shove70 shove70 commented Dec 16, 2023

  1. Fixed Uncaught type checking error with options and pointers #19555
  2. Add tests.
module main
import rand

struct MyType {
	foo string
}

struct TypeWithOption {
	maybe ?&MyType
}

fn main() {
	println("trying...")
	x := rand.int()

	ptr := if x > 0 {
		?MyType(MyType{foo:"bar"})
	} else {
		?MyType(none)
	}

	container := TypeWithOption{maybe: ptr} // Expects ?&MyType, not ?MyType. Sema doesn't catch this, I guess.
	println("success!")
	println(container)
}

outputs:

a.v:22:30: error: cannot assign to field `maybe`: expected a pointer `?&MyType`, but got `?MyType`
   20 |     }
   21 | 
   22 |     container := TypeWithOption{maybe: ptr} // Expects ?&MyType, not ?MyType. Sema doesn't catch this, I guess.
      |                                 ~~~~~~~~~~
   23 |     println("success!")
   24 |     println(container)

Copy link
Member

@spytheman spytheman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work.

@spytheman spytheman merged commit 51aaf3c into vlang:master Dec 16, 2023
54 checks passed
@shove70 shove70 deleted the ref_ptr_init branch December 16, 2023 21:09
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.

Uncaught type checking error with options and pointers
2 participants