Skip to content

checker: option casting after array indexing #26000

Description

@enghitalo

Describe the bug

It is not casting properly by using or { '' } after array indexing

Reproduction Steps

struct Struct {
	f1 string
	f2 int
	f3 bool
	f4 f64
}

struct StructWithFieldsOfOptionalArray {
	f1 []?string
	f2 []?int
	f3 []?bool
	f4 []?f64
}

fn main() {
	v1 := StructWithFieldsOfOptionalArray{
		f1: [?string('a'), 'b']
		f2: [?int(1), 2]
		f3: [?bool(true), false]
		f4: [?f64(1.1), 2.2]
	}
	v2 := Struct{
		f1: v1.f1[0] or { '' }
		f2: v1.f2[0] or { 0 }
		f3: v1.f3[0] or { false }
		f4: v1.f4[0] or { 0.0 }
	}
	assert v2.f1 == 'a'
	assert v2.f2 == 1
	assert v2.f3 == true
	assert v2.f4 == 1.1
}

Expected Behavior

assert pass

Current Behavior

examples/hexagonal/src/infrastructure/repositories/pg_user_repository.v:23:3: error: cannot assign an Option value to a non-option struct field
   21 |     }
   22 |     v2 := Struct{
   23 |         f1: v1.f1[0] or { '' }
      |         ~~~~~~~~~~~~~~~~~~~~~~
   24 |         f2: v1.f2[0] or { 0 }
   25 |         f3: v1.f3[0] or { false }
examples/hexagonal/src/infrastructure/repositories/pg_user_repository.v:24:3: error: cannot assign an Option value to a non-option struct field
   22 |     v2 := Struct{
   23 |         f1: v1.f1[0] or { '' }
   24 |         f2: v1.f2[0] or { 0 }
      |         ~~~~~~~~~~~~~~~~~~~~~
   25 |         f3: v1.f3[0] or { false }
   26 |         f4: v1.f4[0] or { 0.0 }
examples/hexagonal/src/infrastructure/repositories/pg_user_repository.v:25:3: error: cannot assign an Option value to a non-option struct field
   23 |         f1: v1.f1[0] or { '' }
   24 |         f2: v1.f2[0] or { 0 }
   25 |         f3: v1.f3[0] or { false }
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~
   26 |         f4: v1.f4[0] or { 0.0 }
   27 |     }
examples/hexagonal/src/infrastructure/repositories/pg_user_repository.v:26:3: error: cannot assign an Option value to a non-option struct field
   24 |         f2: v1.f2[0] or { 0 }
   25 |         f3: v1.f3[0] or { false }
   26 |         f4: v1.f4[0] or { 0.0 }
      |         ~~~~~~~~~~~~~~~~~~~~~~~
   27 |     }
   28 |     assert v2.f1 == 'a'

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.12 0800feb

Environment details (OS name and version, etc.)

Linux

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

Metadata

Metadata

Assignees

Labels

BugThis tag is applied to issues which reports bugs.Status: ConfirmedThis bug has been confirmed to be valid by a contributor.Unit: CheckerBugs/feature requests, that are related to the type checker.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions