gen: c don't generate x >= 0 check for unsigned ints in range exprs in matches (#6414)

pull/6416/head
Daniel Däschle 2020-09-18 11:53:19 +02:00 committed by GitHub
parent f7aa9cb0c5
commit ffc8cf3925
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 4 deletions

View File

@ -2671,11 +2671,21 @@ fn (mut g Gen) match_expr(node ast.MatchExpr) {
g.expr(expr)
g.write(')')
} else if expr is ast.RangeExpr {
// if type is unsigned and low is 0, check is unneeded
mut skip_low := false
if expr.low is ast.IntegerLiteral as expr_low {
if node.cond_type in [table.u16_type, table.u32_type, table.u64_type] &&
expr_low.val == '0' {
skip_low = true
}
}
g.write('(')
g.write(cond_var)
g.write(' >= ')
g.expr(expr.low)
g.write(' && ')
if !skip_low {
g.write(cond_var)
g.write(' >= ')
g.expr(expr.low)
g.write(' && ')
}
g.write(cond_var)
g.write(' <= ')
g.expr(expr.high)