From 3e333af0aa9ab8bebea1bcd19ebadf035e357606 Mon Sep 17 00:00:00 2001 From: YellowAfterlife Date: Fri, 14 Oct 2022 12:36:07 +0300 Subject: [PATCH] Partial fix for #133 ("treat X as block-scoped" will now help) --- src/parsers/linter/GmlLinter.hx | 12 +++++++++-- tests/TestGMEditGMS23/TestGMEditGMS23.yyp | 1 + .../v_constructor_function_redefinitions.gml | 20 +++++++++++++++++++ .../v_constructor_function_redefinitions.yy | 12 +++++++++++ 4 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 tests/TestGMEditGMS23/scripts/v_constructor_function_redefinitions/v_constructor_function_redefinitions.gml create mode 100644 tests/TestGMEditGMS23/scripts/v_constructor_function_redefinitions/v_constructor_function_redefinitions.yy diff --git a/src/parsers/linter/GmlLinter.hx b/src/parsers/linter/GmlLinter.hx index 8f9a7457..8c00e5ee 100644 --- a/src/parsers/linter/GmlLinter.hx +++ b/src/parsers/linter/GmlLinter.hx @@ -601,12 +601,14 @@ class GmlLinter { // if (!skipIf(nk == KIdent)) break; var varName = nextVal; + var allowTypeRedefinition = false; if (mainKind != KGlobalVar && mainKind != KStatic) { + var lk = localKinds[varName]; if (mainKind != KVar || prefs.blockScopedVar) { - var lk = localKinds[varName]; if (lk != null && lk != KGhostVar) { addWarning('Redefinition of a variable `$varName`'); } else { + allowTypeRedefinition = true; var arr = localNamesPerDepth[oldDepth]; if (arr == null) { arr = []; @@ -614,6 +616,8 @@ class GmlLinter { } arr.push(varName); } + } else if (lk == null || lk == KGhostVar) { + allowTypeRedefinition = true; } localKinds[varName] = mainKind; } @@ -667,7 +671,11 @@ class GmlLinter { if (lastVarType == null) { if (setLocalVars) typeInfo = "type " + varExprType.toString() + " (auto)"; imp.localTypes[varName] = varExprType; - } else if (!varExprType.equals(lastVarType)) { + } + else if (allowTypeRedefinition) { + imp.localTypes[varName] = varExprType; + } + else if (!varExprType.equals(lastVarType)) { addWarning('Implicit redefinition of type for local variable $varName from ' + lastVarType.toString() + " to " + varExprType.toString()); } diff --git a/tests/TestGMEditGMS23/TestGMEditGMS23.yyp b/tests/TestGMEditGMS23/TestGMEditGMS23.yyp index 496310ab..8e904bba 100644 --- a/tests/TestGMEditGMS23/TestGMEditGMS23.yyp +++ b/tests/TestGMEditGMS23/TestGMEditGMS23.yyp @@ -80,6 +80,7 @@ {"id":{"name":"v_paren_call","path":"scripts/v_paren_call/v_paren_call.yy",},"order":3,}, {"id":{"name":"v_empty_switch","path":"scripts/v_empty_switch/v_empty_switch.yy",},"order":4,}, {"id":{"name":"v_broken_switch","path":"scripts/v_broken_switch/v_broken_switch.yy",},"order":5,}, + {"id":{"name":"v_constructor_function_redefinitions","path":"scripts/v_constructor_function_redefinitions/v_constructor_function_redefinitions.yy",},"order":6,}, ], "Options": [ {"name":"Amazon Fire","path":"options/amazonfire/options_amazonfire.yy",}, diff --git a/tests/TestGMEditGMS23/scripts/v_constructor_function_redefinitions/v_constructor_function_redefinitions.gml b/tests/TestGMEditGMS23/scripts/v_constructor_function_redefinitions/v_constructor_function_redefinitions.gml new file mode 100644 index 00000000..9b749be8 --- /dev/null +++ b/tests/TestGMEditGMS23/scripts/v_constructor_function_redefinitions/v_constructor_function_redefinitions.gml @@ -0,0 +1,20 @@ +function v_constructor_function_redefinitions() constructor { + static f1 = function() { + let v = 0; + v += 1; + v += ""; ///want_warn + array_push(v, 1); ///want_warn + } + static f2 = function() { + let v = "hi!"; + v += 1; ///want_warn + v += ""; + array_push(v, 1); ///want_warn + } + static f3 = function() { + let v = []; + v += 1; ///want_warn + v += ""; ///want_warn + array_push(v, 1); + } +} \ No newline at end of file diff --git a/tests/TestGMEditGMS23/scripts/v_constructor_function_redefinitions/v_constructor_function_redefinitions.yy b/tests/TestGMEditGMS23/scripts/v_constructor_function_redefinitions/v_constructor_function_redefinitions.yy new file mode 100644 index 00000000..425330fb --- /dev/null +++ b/tests/TestGMEditGMS23/scripts/v_constructor_function_redefinitions/v_constructor_function_redefinitions.yy @@ -0,0 +1,12 @@ +{ + "resourceType": "GMScript", + "resourceVersion": "1.0", + "name": "v_constructor_function_redefinitions", + "isDnD": false, + "isCompatibility": false, + "parent": { + "name": "Constructors", + "path": "folders/Scripts/Linter/Constructors.yy", + }, + "tags": [], +} \ No newline at end of file