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 error for or_expr inside infix expression (fix #19199) #19213

Merged
merged 1 commit into from
Aug 25, 2023

Conversation

yuyi98
Copy link
Member

@yuyi98 yuyi98 commented Aug 24, 2023

This PR check error for or_expr inside infix expression (fix #19199).

  • Check error for or_expr inside infix expression.
  • Add test.
import time

enum StopAfterType {
	time
	tx
}

struct StopAfter {
	t               f64
	stop_after_type StopAfterType
}

pub fn parse(str string) !StopAfter {
	time_format := [str[str.len - 1]].bytestr()
	time_string := str[0..str.len - 1]
	if time_string == '0' {
		return StopAfter{}
	}
	timef64 := time_string.f64()
	if timef64 == 0 {
		return error('invalid string "${str}"')
	}

	match time_format {
		's' {
			return StopAfter{timef64 * time.second, .time}
		}
		'm' {
			return StopAfter{timef64 * time.minute, .time}
		}
		'h' {
			return StopAfter{timef64 * time.hour, .time}
		}
		'd' {
			return StopAfter{timef64 * time.hour * 24, .time}
		}
		't' {
			return StopAfter{timef64, .tx}
		}
		else {
			return error('no match for "${time_format}" format')
		}
	}
}

fn main() {
	assert parse('0s')! == StopAfter{}
	assert parse('10s')! == StopAfter{10 * time.second, .time}
	assert parse('10m')! == StopAfter{10 * time.minute, .time}
	assert parse('10h')! == StopAfter{10 * time.hour, .time}
	assert parse('10d')! == StopAfter{10 * time.hour * 24, .time}
	assert parse('10t')! == StopAfter{10, .tx}
	assert parse('10x') or { err } == StopAfter{}
	assert StopAfter{} == parse('10x') or { err }
}

PS D:\Test\v\tt1> v run .
tt1.v:53:27: error: wrong return type `IError` in the `or {}` block, expected `StopAfter`
   51 |     assert parse('10d')! == StopAfter{10 * time.hour * 24, .time}
   52 |     assert parse('10t')! == StopAfter{10, .tx}
   53 |     assert parse('10x') or { err } == StopAfter{}
      |                              ~~~
   54 |     assert StopAfter{} == parse('10x') or { err }
   55 | }
tt1.v:54:42: error: wrong return type `IError` in the `or {}` block, expected `StopAfter`
   52 |     assert parse('10t')! == StopAfter{10, .tx}
   53 |     assert parse('10x') or { err } == StopAfter{}
   54 |     assert StopAfter{} == parse('10x') or { err }
      |                                             ~~~
   55 | }

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.

A very elegant fix!

Excellent work.

@spytheman spytheman merged commit d417bba into vlang:master Aug 25, 2023
48 checks passed
@yuyi98 yuyi98 deleted the check_infix_expr_or_expr branch August 26, 2023 01:06
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.

cannot convert 'struct IError' to 'struct parse_stop_after__StopAfter'
2 participants