diff options
-rw-r--r-- | server/src/core/diagnostics.lua | 38 | ||||
-rw-r--r-- | server/src/vm/function.lua | 4 | ||||
-rw-r--r-- | server/src/vm/local.lua | 13 | ||||
-rw-r--r-- | server/test/diagnostics/init.lua | 2 |
4 files changed, 35 insertions, 22 deletions
diff --git a/server/src/core/diagnostics.lua b/server/src/core/diagnostics.lua index 5f2e03e1..47542fa5 100644 --- a/server/src/core/diagnostics.lua +++ b/server/src/core/diagnostics.lua @@ -141,30 +141,27 @@ function mt:searchSpaces(callback) end function mt:searchRedefinition(callback) - local results = self.results - local uri = self.uri local used = {} - for _, var in ipairs(results.locals) do - if var.key == '_' - or var.key == '_ENV' - or var.key == '' - then - goto NEXT_VAR - end - if var.hide then - goto NEXT_VAR + local uri = self.uri + self.vm:eachSource(function (source) + local loc = source:bindLocal() + if not loc then + return end - local shadow = var.shadow + local shadow = loc:shadow() if not shadow then - goto NEXT_VAR + return end if used[shadow] then - goto NEXT_VAR + return end used[shadow] = true - -- 如果多次重定义,则不再警告 - if #shadow >= 4 then - goto NEXT_VAR + if loc:getFlag 'hide' then + return + end + local name = loc:getName() + if name == '_' or name == '_ENV' or name == '' then + return end local related = {} for i = 1, #shadow do @@ -175,10 +172,9 @@ function mt:searchRedefinition(callback) } end for i = 2, #shadow do - callback(shadow[i].source.start, shadow[i].source.finish, shadow[i].key, related) + callback(shadow[i].source.start, shadow[i].source.finish, name, related) end - ::NEXT_VAR:: - end + end) end function mt:searchNewLineCall(callback) @@ -278,7 +274,7 @@ return function (vm, lines, uri) -- 重定义局部变量 session:doDiagnostics(session.searchRedefinition, 'redefined-local', function (key, related) return { - level = DiagnosticSeverity.Information, + level = DiagnosticSeverity.Hint, message = lang.script('DIAG_REDEFINED_LOCAL', key), related = related, } diff --git a/server/src/vm/function.lua b/server/src/vm/function.lua index 2d9e570d..a6835f5b 100644 --- a/server/src/vm/function.lua +++ b/server/src/vm/function.lua @@ -26,6 +26,10 @@ function mt:saveLocal(name, loc) if loc.type ~= 'local' then error('saveLocal必须是local') end + local old = self:loadLocal(name) + if old then + loc:shadow(old) + end self.locals[self._top][name] = loc end diff --git a/server/src/vm/local.lua b/server/src/vm/local.lua index 75f41db2..6062d28e 100644 --- a/server/src/vm/local.lua +++ b/server/src/vm/local.lua @@ -56,6 +56,19 @@ function mt:getName() return self.name end +function mt:shadow(old) + if not old then + return self._shadow + end + local group = old._shadow + if not group then + group = {} + group[#group+1] = old + end + group[#group+1] = self + self._shadow = group +end + return function (name, source, value) if not value then error('Local must has a value') diff --git a/server/test/diagnostics/init.lua b/server/test/diagnostics/init.lua index 1ab50954..edb10e82 100644 --- a/server/test/diagnostics/init.lua +++ b/server/test/diagnostics/init.lua @@ -115,7 +115,7 @@ print(_) local _ print(_) local _ENV -print(_ENV) +<!print!>(_ENV) -- 由于重定义了_ENV,因此print变为了未定义全局变量 ]] TEST [[ |