diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2019-11-21 20:49:42 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2019-11-21 20:49:42 +0800 |
commit | 442b2d559537ed0caeb9116bf904bd93002aaa8b (patch) | |
tree | e0785189e18388332f1f90127ef6677f726bad64 | |
parent | 128e0f95772c0afc43a447c039f08f13b268ad0a (diff) | |
download | lua-language-server-442b2d559537ed0caeb9116bf904bd93002aaa8b.zip |
局部变量在一定程度上向后推测类型
-rw-r--r-- | server-beta/src/vm/getValue.lua | 25 | ||||
-rw-r--r-- | server-beta/test/type_inference/init.lua | 6 |
2 files changed, 31 insertions, 0 deletions
diff --git a/server-beta/src/vm/getValue.lua b/server-beta/src/vm/getValue.lua index 6faf591e..c1b3622d 100644 --- a/server-beta/src/vm/getValue.lua +++ b/server-beta/src/vm/getValue.lua @@ -665,6 +665,30 @@ local function inferByBinary(results, source) end end +local function inferBySetOfLocal(results, source) + if source.ref then + for i = 1, #source.ref do + local ref = source.ref[i] + if ref.type == 'setlocal' then + break + end + merge(results, vm.getValue(ref)) + end + end +end + +local function inferBySet(results, source) + if #results ~= 0 then + return + end + if source.type == 'local' then + inferBySetOfLocal(results, source) + elseif source.type == 'setlocal' + or source.type == 'getlocal' then + inferBySetOfLocal(results, source.node) + end +end + local function getValue(source) local results = checkLiteral(source) or checkValue(source) @@ -679,6 +703,7 @@ local function getValue(source) results = {} checkDef(results, source) + inferBySet(results, source) inferByCall(results, source) inferByGetTable(results, source) inferByUnary(results, source) diff --git a/server-beta/test/type_inference/init.lua b/server-beta/test/type_inference/init.lua index 53ee6131..f509ca3f 100644 --- a/server-beta/test/type_inference/init.lua +++ b/server-beta/test/type_inference/init.lua @@ -176,6 +176,12 @@ TEST 'integer' [[ local y = <?x?> << 0 ]] +TEST 'integer' [[ +local function f(<?a?>, b) + return a << b +end +]] + -- 不根据调用者的输入参数来推测 --TEST 'number' [[ --local function x(a) |