From 4608898bcdf3109208f0eaa89f35839173c36635 Mon Sep 17 00:00:00 2001 From: yuyi Date: Sat, 23 May 2020 23:30:28 +0800 Subject: [PATCH] checker: fix multiple assign immutable error --- vlib/net/ftp/ftp.v | 40 +++++++++---------- vlib/v/checker/checker.v | 1 + .../tests/assign_multi_immutable_err.out | 7 ++++ .../tests/assign_multi_immutable_err.vv | 7 ++++ 4 files changed, 35 insertions(+), 20 deletions(-) create mode 100644 vlib/v/checker/tests/assign_multi_immutable_err.out create mode 100644 vlib/v/checker/tests/assign_multi_immutable_err.vv diff --git a/vlib/net/ftp/ftp.v b/vlib/net/ftp/ftp.v index 1c43f80bb6..f475b24df8 100644 --- a/vlib/net/ftp/ftp.v +++ b/vlib/net/ftp/ftp.v @@ -20,17 +20,17 @@ module ftp import net const ( - Connected = 220 - SpecifyPassword = 331 - LoggedIn = 230 - LoginFirst = 503 - Anonymous = 530 - OpenDataConnection = 150 - CloseDataConnection = 226 - CommandOk = 200 - denied = 550 - PassiveMode = 227 - complete = 226 + connected = 220 + specify_password = 331 + logged_in = 230 + login_first = 503 + anonymous = 530 + open_data_connection = 150 + close_data_connection = 226 + command_ok = 200 + denied = 550 + passive_mode = 227 + complete = 226 ) struct DTP { @@ -110,7 +110,7 @@ pub fn (mut ftp FTP) connect(ip string) bool { } ftp.sock = sock code, _ := ftp.read() - if code == Connected { + if code == connected { return true } return false @@ -123,11 +123,11 @@ pub fn (ftp FTP) login(user, passwd string) bool { } return false } - mut code, data := ftp.read() - if code == LoggedIn { + mut code, mut data := ftp.read() + if code == logged_in { return true } - if code != SpecifyPassword { + if code != specify_password { return false } ftp.write('PASS $passwd') or { @@ -139,7 +139,7 @@ pub fn (ftp FTP) login(user, passwd string) bool { code, data = ftp.read() // TODO Replace `data` with `_` _ := data - if code == LoggedIn { + if code == logged_in { return true } return false @@ -209,7 +209,7 @@ fn (ftp FTP) pasv() ?DTP { $if debug { println('pass: $data') } - if code != PassiveMode { + if code != passive_mode { return error('pasive mode not allowed') } dtp := new_dtp(data) or { @@ -228,12 +228,12 @@ pub fn (ftp FTP) dir() ?[]string { if code == denied { return error('LIST denied') } - if code != OpenDataConnection { + if code != open_data_connection { return error('data channel empty') } list_dir := dtp.read() result, _ := ftp.read() - if result != CloseDataConnection { + if result != close_data_connection { println('LIST not ok') } dtp.close() @@ -258,7 +258,7 @@ pub fn (ftp FTP) get(file string) ?[]byte { if code == denied { return error('Permission denied') } - if code != OpenDataConnection { + if code != open_data_connection { return error('Data connection not ready') } blob := dtp.read() diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index fb0edb4b4d..30dd0537ef 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1261,6 +1261,7 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) { mut ident_var_info := ident.var_info() // c.assigned_var_name = ident.name if assign_stmt.op == .assign { + c.fail_if_immutable(ident) var_type := c.expr(ident) assign_stmt.left_types << var_type if !c.table.check(val_type, var_type) { diff --git a/vlib/v/checker/tests/assign_multi_immutable_err.out b/vlib/v/checker/tests/assign_multi_immutable_err.out new file mode 100644 index 0000000000..c1fcc40dea --- /dev/null +++ b/vlib/v/checker/tests/assign_multi_immutable_err.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/assign_multi_immutable_err.v:4:2: error: `a` is immutable, declare it with `mut` to make it mutable + 2 | a := 10 + 3 | b := 20 + 4 | a, b = 1, 2 + | ^ + 5 | + 6 | println('$a, $b') \ No newline at end of file diff --git a/vlib/v/checker/tests/assign_multi_immutable_err.vv b/vlib/v/checker/tests/assign_multi_immutable_err.vv new file mode 100644 index 0000000000..a0c4f0d82d --- /dev/null +++ b/vlib/v/checker/tests/assign_multi_immutable_err.vv @@ -0,0 +1,7 @@ +fn main() { + a := 10 + b := 20 + a, b = 1, 2 + + println('$a, $b') +} \ No newline at end of file