Skip to content

cgen: fix code generation wrong, when 'x.?[]array or {}' as a 'for-in' condition(fix #20528) - #20542

Merged
spytheman merged 1 commit into
vlang:masterfrom
shove70:temp
Jan 15, 2024
Merged

spytheman merged 1 commit into
vlang:masterfrom
shove70:temp

Conversation

@shove70

@shove70 shove70 commented Jan 15, 2024

Copy link
Copy Markdown
Contributor
  1. Fixed cgen generating statements in expression #20528
  2. Add tests.
pub type Snowflake = int

pub struct PermissionOverwrite {
	id Snowflake
	n  int
}

pub struct Channel {
	permission_overwrites ?[]PermissionOverwrite
}

pub fn (c Channel) get_overwrite(id Snowflake) ?PermissionOverwrite {
	for overwrite in c.permission_overwrites or { return none } {
		if overwrite.id == id {
			return overwrite
		}
	}
	return none
}

fn main() {
	channel := Channel{
		permission_overwrites: [
			PermissionOverwrite{
				id: 1
				n: 42
			},
			PermissionOverwrite{
				id: 2
				n: 84
			},
			PermissionOverwrite{
				id: 3
				n: 6969
			},
			PermissionOverwrite{
				id: 4
				n: 4242
			},
		]
	}
	println(channel.get_overwrite(3)?.n)
}

outputs:

6969

@shove70 shove70 changed the title cgen: fix code generation wrong, when '?foo.array or {}' as a 'for-in' condition(fix #20528) cgen: fix code generation wrong, when 'x.?[]array or {}' as a 'for-in' condition(fix #20528) Jan 15, 2024

@spytheman spytheman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Excellent work.

@spytheman
spytheman merged commit d783cda into vlang:master Jan 15, 2024
@shove70
shove70 deleted the temp branch January 15, 2024 13:52
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.

cgen generating statements in expression

3 participants