1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

ast, parser, cgen: fix anon sumtype cast (fix #14967) (#14976)

This commit is contained in:
yuyi 2022-07-07 23:51:57 +08:00 committed by GitHub
parent 029d583bb2
commit 758f84fa86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 37 additions and 12 deletions

View File

@ -401,7 +401,7 @@ fn (mut g Gen) gen_str_for_union_sum_type(info ast.SumType, styp string, str_fn_
mut clean_sum_type_v_type_name := ''
if info.is_anon {
variant_names := info.variants.map(util.strip_main_name(g.table.sym(it).name))
clean_sum_type_v_type_name = '(${variant_names.join(' | ')})'
clean_sum_type_v_type_name = '${variant_names.join('|')}'
} else {
clean_sum_type_v_type_name = styp.replace('__', '.')
if styp.ends_with('*') {

View File

@ -2493,6 +2493,24 @@ pub fn (mut p Parser) name_expr() ast.Expr {
typ: typ
pos: type_pos
}
} else if !known_var && language == .v && (p.table.known_type(p.tok.lit) || lit0_is_capital)
&& p.peek_tok.kind == .pipe {
start_pos := p.tok.pos()
mut to_typ := p.parse_type()
p.check(.lpar)
expr := p.expr(0)
end_pos := p.tok.pos()
p.check(.rpar)
node = ast.CastExpr{
typ: to_typ
typname: p.table.sym(to_typ).name
expr: expr
arg: ast.empty_expr()
has_arg: false
pos: start_pos.extend(end_pos)
}
p.expr_mod = ''
return node
}
ident := p.parse_ident(language)

View File

@ -0,0 +1,7 @@
module main
fn test_cast_to_anon_sumtype() {
x := string|none(none)
println(x)
assert '$x' == 'string|none(none)'
}