gen: unwrap optional on if guard expr (#6903)
							parent
							
								
									2f50a9ea1f
								
							
						
					
					
						commit
						22fdf76db5
					
				| 
						 | 
					@ -3219,8 +3219,15 @@ pub fn (mut c Checker) ident(mut ident ast.Ident) table.Type {
 | 
				
			||||||
								return table.void_type
 | 
													return table.void_type
 | 
				
			||||||
							}
 | 
												}
 | 
				
			||||||
						}
 | 
											}
 | 
				
			||||||
 | 
											if mut obj.expr is ast.IfGuardExpr {
 | 
				
			||||||
 | 
												// new variable from if guard shouldn't have the optional flag for further use
 | 
				
			||||||
 | 
												// a temp variable will be generated which unwraps it
 | 
				
			||||||
 | 
												if_guard_var_type := c.expr(obj.expr.expr)
 | 
				
			||||||
 | 
												typ = if_guard_var_type.clear_flag(.optional)
 | 
				
			||||||
 | 
											} else {
 | 
				
			||||||
							typ = c.expr(obj.expr)
 | 
												typ = c.expr(obj.expr)
 | 
				
			||||||
						}
 | 
											}
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
					is_optional := typ.has_flag(.optional)
 | 
										is_optional := typ.has_flag(.optional)
 | 
				
			||||||
					ident.kind = .variable
 | 
										ident.kind = .variable
 | 
				
			||||||
					ident.info = ast.IdentVar{
 | 
										ident.info = ast.IdentVar{
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -3544,7 +3544,8 @@ fn (mut g Gen) if_expr(node ast.IfExpr) {
 | 
				
			||||||
					g.expr(branch.cond.expr)
 | 
										g.expr(branch.cond.expr)
 | 
				
			||||||
					g.writeln(', ${var_name}.ok) {')
 | 
										g.writeln(', ${var_name}.ok) {')
 | 
				
			||||||
					if branch.cond.var_name != '_' {
 | 
										if branch.cond.var_name != '_' {
 | 
				
			||||||
						g.writeln('\t${g.typ(branch.cond.expr_type)} $branch.cond.var_name = $var_name;')
 | 
											base_type := g.base_type(branch.cond.expr_type)
 | 
				
			||||||
 | 
											g.writeln('\t$base_type $branch.cond.var_name = *($base_type*)${var_name}.data;')
 | 
				
			||||||
					}
 | 
										}
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
				else {
 | 
									else {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -106,15 +106,15 @@ fn (mut p Parser) if_expr(is_comptime bool) ast.IfExpr {
 | 
				
			||||||
			p.check(.decl_assign)
 | 
								p.check(.decl_assign)
 | 
				
			||||||
			comments << p.eat_comments()
 | 
								comments << p.eat_comments()
 | 
				
			||||||
			expr := p.expr(0)
 | 
								expr := p.expr(0)
 | 
				
			||||||
			p.scope.register(var_name, ast.Var{
 | 
					 | 
				
			||||||
				name: var_name
 | 
					 | 
				
			||||||
				expr: expr
 | 
					 | 
				
			||||||
				pos: var_pos
 | 
					 | 
				
			||||||
			})
 | 
					 | 
				
			||||||
			cond = ast.IfGuardExpr{
 | 
								cond = ast.IfGuardExpr{
 | 
				
			||||||
				var_name: var_name
 | 
									var_name: var_name
 | 
				
			||||||
				expr: expr
 | 
									expr: expr
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
								p.scope.register(var_name, ast.Var{
 | 
				
			||||||
 | 
									name: var_name
 | 
				
			||||||
 | 
									expr: cond
 | 
				
			||||||
 | 
									pos: var_pos
 | 
				
			||||||
 | 
								})
 | 
				
			||||||
			prev_guard = true
 | 
								prev_guard = true
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			prev_guard = false
 | 
								prev_guard = false
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue