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

all: change index expr returning optional to result too (#16097)

This commit is contained in:
yuyi 2022-10-20 02:04:16 +08:00 committed by GitHub
parent eda65ad660
commit 612faac0f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 63 additions and 47 deletions

View File

@ -154,8 +154,7 @@ fn (upd VlsUpdater) download_prebuilt() ! {
} }
latest_release := releases_json[0].as_map() latest_release := releases_json[0].as_map()
latest_assets := latest_release['assets'] or { return } assets := latest_release['assets']!.arr()
assets := latest_assets.arr()
mut checksum_asset_idx := -1 mut checksum_asset_idx := -1
mut exec_asset_idx := -1 mut exec_asset_idx := -1
@ -199,13 +198,11 @@ fn (upd VlsUpdater) download_prebuilt() ! {
upd.log('Executable found for this system. Downloading...') upd.log('Executable found for this system. Downloading...')
upd.init_download_prebuilt()! upd.init_download_prebuilt()!
t_asset := exec_asset['browser_download_url'] or { return } http.download_file(exec_asset['browser_download_url']!.str(), exec_asset_file_path)!
http.download_file(t_asset.str(), exec_asset_file_path)!
checksum_file_path := os.join_path(vls_cache_folder, 'checksums.txt') checksum_file_path := os.join_path(vls_cache_folder, 'checksums.txt')
checksum_file_asset := assets[checksum_asset_idx].as_map() checksum_file_asset := assets[checksum_asset_idx].as_map()
t_checksum_asset := checksum_file_asset['browser_download_url'] or { return } http.download_file(checksum_file_asset['browser_download_url']!.str(), checksum_file_path)!
http.download_file(t_checksum_asset.str(), checksum_file_path)!
checksums := os.read_file(checksum_file_path)!.split_into_lines() checksums := os.read_file(checksum_file_path)!.split_into_lines()
upd.log('Verifying checksum...') upd.log('Verifying checksum...')

View File

@ -1290,7 +1290,7 @@ large_index := 999
val := arr[large_index] or { panic('out of bounds') } val := arr[large_index] or { panic('out of bounds') }
println(val) println(val)
// you can also do this, if you want to *propagate* the access error: // you can also do this, if you want to *propagate* the access error:
val2 := arr[333]? val2 := arr[333]!
println(val2) println(val2)
``` ```

View File

@ -144,16 +144,16 @@ fn test_ranges() {
assert s6 == 'first' assert s6 == 'first'
} }
fn ranges_propagate_first(s string) ?string { fn ranges_propagate_first(s string) !string {
return s[10..]? return s[10..]!
} }
fn ranges_propagate_last(s string) ?string { fn ranges_propagate_last(s string) !string {
return s[..20]? return s[..20]!
} }
fn ranges_propagate_both(s string) ?string { fn ranges_propagate_both(s string) !string {
return s[1..20]? return s[1..20]!
} }
fn test_split_nth() { fn test_split_nth() {

View File

@ -18,6 +18,6 @@ fn test_crlf_is_parsable_just_like_lf() {
for content in all { for content in all {
res := toml.parse_text(content)? res := toml.parse_text(content)?
assert res.value('title') == toml.Any('TOML Example') assert res.value('title') == toml.Any('TOML Example')
assert (res.value('database') as map[string]toml.Any)['server']? == toml.Any('192.168.1.1') assert (res.value('database') as map[string]toml.Any)['server']! == toml.Any('192.168.1.1')
} }
} }

View File

@ -41,21 +41,21 @@ fn test_type_size() {
mut t := b.table mut t := b.table
size01, _ := t.type_size(t.type_idxs['main.T01']?) size01, _ := t.type_size(t.type_idxs['main.T01']!)
assert sizeof(T01) == size01 assert sizeof(T01) == size01
size02, _ := t.type_size(t.type_idxs['main.T02']?) size02, _ := t.type_size(t.type_idxs['main.T02']!)
assert sizeof(T02) == size02 assert sizeof(T02) == size02
size03, _ := t.type_size(t.type_idxs['main.T03']?) size03, _ := t.type_size(t.type_idxs['main.T03']!)
assert sizeof(T03) == size03 assert sizeof(T03) == size03
size04, _ := t.type_size(t.type_idxs['main.T04']?) size04, _ := t.type_size(t.type_idxs['main.T04']!)
assert sizeof(T04) == size04 assert sizeof(T04) == size04
size05, _ := t.type_size(t.type_idxs['main.T05']?) size05, _ := t.type_size(t.type_idxs['main.T05']!)
assert sizeof(T05) == size05 assert sizeof(T05) == size05
size06, _ := t.type_size(t.type_idxs['main.T06']?) size06, _ := t.type_size(t.type_idxs['main.T06']!)
assert sizeof(T06) == size06 assert sizeof(T06) == size06
size07, _ := t.type_size(t.type_idxs['main.T07']?) size07, _ := t.type_size(t.type_idxs['main.T07']!)
assert sizeof(T07) == size07 assert sizeof(T07) == size07
size08, _ := t.type_size(t.type_idxs['main.T08']?) size08, _ := t.type_size(t.type_idxs['main.T08']!)
assert sizeof(T08) == size08 assert sizeof(T08) == size08
println('done') println('done')

View File

@ -952,7 +952,7 @@ pub fn (mut c Checker) check_expr_opt_call(expr ast.Expr, ret_type ast.Type) ast
} }
} else if expr is ast.IndexExpr { } else if expr is ast.IndexExpr {
if expr.or_expr.kind != .absent { if expr.or_expr.kind != .absent {
c.check_or_expr(expr.or_expr, ret_type, ret_type.set_flag(.optional)) c.check_or_expr(expr.or_expr, ret_type, ret_type.set_flag(.result))
} }
} }
return ret_type return ret_type

View File

@ -92,7 +92,7 @@ bar'
}, Two{ }, Two{
value: 'two' value: 'two'
})" })"
r := m[key]? r := m[key]!
compile_time_env := $env('ENV_VAR') compile_time_env := $env('ENV_VAR')
func() func()
} }

View File

@ -2674,11 +2674,14 @@ fn (mut p Parser) index_expr(left ast.Expr, is_gated bool) ast.IndexExpr {
is_gated: is_gated is_gated: is_gated
} }
} }
// `a[start..end] ?` // `a[start..end]!`
if p.tok.kind == .question { if p.tok.kind == .not {
or_pos_high = p.tok.pos() or_pos_high = p.tok.pos()
or_kind_high = .propagate_option or_kind_high = .propagate_result
p.next() p.next()
} else if p.tok.kind == .question {
p.error_with_pos('`?` for propagating errors from index expressions is no longer supported, use `!` instead of `?`',
p.tok.pos())
} }
} }
@ -2740,11 +2743,14 @@ fn (mut p Parser) index_expr(left ast.Expr, is_gated bool) ast.IndexExpr {
is_gated: is_gated is_gated: is_gated
} }
} }
// `a[start..end] ?` // `a[start..end]!`
if p.tok.kind == .question { if p.tok.kind == .not {
or_pos_low = p.tok.pos() or_pos_low = p.tok.pos()
or_kind_low = .propagate_option or_kind_low = .propagate_result
p.next() p.next()
} else if p.tok.kind == .question {
p.error_with_pos('`?` for propagating errors from index expressions is no longer supported, use `!` instead of `?`',
p.tok.pos())
} }
} }
@ -2789,11 +2795,14 @@ fn (mut p Parser) index_expr(left ast.Expr, is_gated bool) ast.IndexExpr {
is_gated: is_gated is_gated: is_gated
} }
} }
// `a[i] ?` // `a[i]!`
if p.tok.kind == .question { if p.tok.kind == .not {
or_pos = p.tok.pos() or_pos = p.tok.pos()
or_kind = .propagate_option or_kind = .propagate_result
p.next() p.next()
} else if p.tok.kind == .question {
p.error_with_pos('`?` for propagating errors from index expressions is no longer supported, use `!` instead of `?`',
p.tok.pos())
} }
} }
return ast.IndexExpr{ return ast.IndexExpr{

View File

@ -0,0 +1,6 @@
vlib/v/parser/tests/index_expr_optional_err.vv:3:14: error: `?` for propagating errors from index expressions is no longer supported, use `!` instead of `?`
1 | fn main() {
2 | a := [1, 2]
3 | println(a[0]?)
| ^
4 | }

View File

@ -0,0 +1,4 @@
fn main() {
a := [1, 2]
println(a[0]?)
}

View File

@ -51,26 +51,26 @@ fn test_map_or() {
assert good == 5 assert good == 5
} }
fn get_map_el(key string) ?int { fn get_map_el(key string) !int {
m := { m := {
'as': 3 'as': 3
'qw': 4 'qw': 4
'kl': 5 'kl': 5
} }
r := m[key]? r := m[key]!
return r return r
} }
fn get_arr_el(i int) ?int { fn get_arr_el(i int) !int {
m := [3, 4, 5] m := [3, 4, 5]
r := m[i]? r := m[i]!
return r return r
} }
[direct_array_access] [direct_array_access]
fn get_arr_el_direct(i int) ?int { fn get_arr_el_direct(i int) !int {
m := [3, 4, 5] m := [3, 4, 5]
r := m[i]? r := m[i]!
return r return r
} }
@ -105,10 +105,10 @@ fn test_propagation() {
assert n == 3 assert n == 3
} }
fn get_arr_el_nested(i int) ?int { fn get_arr_el_nested(i int) !int {
ind := [2, 1, 0, 5] ind := [2, 1, 0, 5]
m := [3, 4, 5] m := [3, 4, 5]
r := m[ind[i]]? r := m[ind[i]]!
return r return r
} }

View File

@ -32,11 +32,11 @@ fn test_nested_map_init_with_multi_enum_keys() {
} }
} }
println(mp) println(mp)
assert mp[.a]? == NestedAbc({ assert mp[.a]! == NestedAbc({
'A': 'AA' 'A': 'AA'
}) })
assert mp[.b]? == NestedAbc('B') assert mp[.b]! == NestedAbc('B')
assert mp[.c]? == NestedAbc({ assert mp[.c]! == NestedAbc({
'c': 'C' 'c': 'C'
}) })
} }

View File

@ -18,7 +18,7 @@ fn test_nested_map_index() {
} }
} }
} }
ret := f.foo[11]?.bar[22]? ret := f.foo[11]!.bar[22]!
println(ret) println(ret)
assert ret == 'hello' assert ret == 'hello'
} }

View File

@ -38,8 +38,8 @@ fn test_if_guard_good() {
assert res == '1' assert res == '1'
} }
fn get_propagate(s string, i int) ?string { fn get_propagate(s string, i int) !string {
c := s[i]? c := s[i]!
return 'got `${c:c}`' return 'got `${c:c}`'
} }