From: Francis De Brabandere <francisdb@gmail.com> Reject Dim/Const/Sub/Function/Class declarations whose names collide at global scope with an earlier declaration of a different kind, returning error 1041 instead of silently accepting the duplicate or failing later with a generic error. Only check for function name collisions when compiling at global scope. Local Dim inside a Sub/Function/Property does not conflict with global functions of the same name, matching Windows behavior. --- dlls/vbscript/compile.c | 58 +++++++++++++++++++++-- dlls/vbscript/interp.c | 4 +- dlls/vbscript/tests/lang.vbs | 56 ++++++++++++++++++++++ dlls/vbscript/tests/run.c | 92 ++++++++++++++++++++++++++++++++++++ 4 files changed, 205 insertions(+), 5 deletions(-) diff --git a/dlls/vbscript/compile.c b/dlls/vbscript/compile.c index 54314372a13..a284d8c12f2 100644 --- a/dlls/vbscript/compile.c +++ b/dlls/vbscript/compile.c @@ -432,6 +432,18 @@ static expression_t *lookup_const_decls(compile_ctx_t *ctx, const WCHAR *name, B return NULL; } +static const_decl_t *find_const_decl(compile_ctx_t *ctx, const WCHAR *name) +{ + const_decl_t *decl; + + for(decl = ctx->const_decls; decl; decl = decl->next) { + if(!vbs_wcsicmp(decl->name, name)) + return decl; + } + + return NULL; +} + static BOOL lookup_args_name(compile_ctx_t *ctx, const WCHAR *name) { unsigned i; @@ -456,6 +468,18 @@ static BOOL lookup_dim_decls(compile_ctx_t *ctx, const WCHAR *name) return FALSE; } +static BOOL lookup_func_decls(compile_ctx_t *ctx, const WCHAR *name) +{ + function_decl_t *func_decl; + + for(func_decl = ctx->func_decls; func_decl; func_decl = func_decl->next) { + if(!vbs_wcsicmp(func_decl->name, name)) + return TRUE; + } + + return FALSE; +} + static HRESULT compile_args(compile_ctx_t *ctx, expression_t *args, unsigned *ret) { unsigned arg_cnt = 0; @@ -1203,11 +1227,21 @@ static HRESULT compile_dim_statement(compile_ctx_t *ctx, dim_statement_t *stat) while(1) { if(lookup_dim_decls(ctx, dim_decl->name) || lookup_args_name(ctx, dim_decl->name) - || lookup_const_decls(ctx, dim_decl->name, FALSE)) { + || (ctx->func->type == FUNC_GLOBAL && lookup_func_decls(ctx, dim_decl->name))) { ctx->loc = dim_decl->loc; + WARN("dim %s name redefined\n", debugstr_w(dim_decl->name)); return MAKE_VBSERROR(VBSE_NAME_REDEFINED); } + { + const_decl_t *const_decl = find_const_decl(ctx, dim_decl->name); + if(const_decl) { + ctx->loc = dim_decl->loc > const_decl->loc ? dim_decl->loc : const_decl->loc; + WARN("dim %s name redefined\n", debugstr_w(dim_decl->name)); + return MAKE_VBSERROR(VBSE_NAME_REDEFINED); + } + } + ctx->func->var_cnt++; if(dim_decl->is_array) { @@ -1282,10 +1316,18 @@ static HRESULT compile_const_statement(compile_ctx_t *ctx, const_statement_t *st if(!lookup_const_decls(ctx, decl->name, FALSE)) { if(lookup_args_name(ctx, decl->name) || lookup_dim_decls(ctx, decl->name)) { + ctx->loc = decl->loc; + WARN("%s redefined\n", debugstr_w(decl->name)); return MAKE_VBSERROR(VBSE_NAME_REDEFINED); } } + if(lookup_func_decls(ctx, decl->name)) { + ctx->loc = decl->loc; + WARN("%s redefined\n", debugstr_w(decl->name)); + return MAKE_VBSERROR(VBSE_NAME_REDEFINED); + } + if(ctx->func->type == FUNC_GLOBAL) { HRESULT hres; @@ -1448,10 +1490,15 @@ static HRESULT collect_const_decls(compile_ctx_t *ctx, statement_t *stat) for(decl = const_stat->decls; decl; decl = decl->next) { const_decl_t *new_decl; - if(lookup_const_decls(ctx, decl->name, FALSE)) - break; /* already collected */ + if(lookup_const_decls(ctx, decl->name, FALSE)) { + ctx->loc = decl->loc; + WARN("%s: const redefined\n", debugstr_w(decl->name)); + return MAKE_VBSERROR(VBSE_NAME_REDEFINED); + } if(lookup_args_name(ctx, decl->name) || lookup_dim_decls(ctx, decl->name)) { + ctx->loc = decl->loc; + WARN("%s redefined\n", debugstr_w(decl->name)); return MAKE_VBSERROR(VBSE_NAME_REDEFINED); } @@ -1459,6 +1506,7 @@ static HRESULT collect_const_decls(compile_ctx_t *ctx, statement_t *stat) if(!new_decl) return E_OUTOFMEMORY; new_decl->name = decl->name; + new_decl->loc = decl->loc; new_decl->value_expr = decl->value_expr; new_decl->next = ctx->const_decls; ctx->const_decls = new_decl; @@ -1789,6 +1837,8 @@ static HRESULT create_function(compile_ctx_t *ctx, function_decl_t *decl, functi HRESULT hres; if(lookup_dim_decls(ctx, decl->name) || lookup_const_decls(ctx, decl->name, FALSE)) { + ctx->loc = decl->name_loc; + WARN("%s: redefinition\n", debugstr_w(decl->name)); return MAKE_VBSERROR(VBSE_NAME_REDEFINED); } @@ -1911,6 +1961,8 @@ static HRESULT compile_class(compile_ctx_t *ctx, class_decl_t *class_decl) if(lookup_dim_decls(ctx, class_decl->name) || lookup_funcs_name(ctx, class_decl->name) || lookup_const_decls(ctx, class_decl->name, FALSE) || lookup_class_name(ctx, class_decl->name)) { + ctx->loc = class_decl->loc; + WARN("%s: redefinition\n", debugstr_w(class_decl->name)); return MAKE_VBSERROR(VBSE_NAME_REDEFINED); } diff --git a/dlls/vbscript/interp.c b/dlls/vbscript/interp.c index 2510c1ee05a..099d5c0b95f 100644 --- a/dlls/vbscript/interp.c +++ b/dlls/vbscript/interp.c @@ -1131,8 +1131,8 @@ static HRESULT interp_const(exec_ctx_t *ctx) return hres; if(ref.type != REF_NONE) { - FIXME("%s already defined\n", debugstr_w(arg)); - return E_FAIL; + WARN("%s already defined\n", debugstr_w(arg)); + return MAKE_VBSERROR(VBSE_NAME_REDEFINED); } hres = stack_assume_val(ctx, 0); diff --git a/dlls/vbscript/tests/lang.vbs b/dlls/vbscript/tests/lang.vbs index d59f05c54a0..ccb5e490002 100644 --- a/dlls/vbscript/tests/lang.vbs +++ b/dlls/vbscript/tests/lang.vbs @@ -2619,6 +2619,36 @@ Class class_test_identifiers_as_property_name End Property End Class +' Local Dim inside a class method should not conflict with a global Function of the same name +Function GlobalShadowFunc() + GlobalShadowFunc = 42 +End Function + +Class TestLocalDimShadowsGlobalFunc + Public Sub TestShadow() + Dim GlobalShadowFunc + GlobalShadowFunc = 10 + Call ok(GlobalShadowFunc = 10, "local Dim should shadow global Function: " & GlobalShadowFunc) + End Sub + + Private Function ClassPrivateFunc() + ClassPrivateFunc = 99 + End Function + + Public Sub TestPrivate() + Call ok(ClassPrivateFunc() = 99, "private class function should be callable: " & ClassPrivateFunc()) + End Sub +End Class + +' Global function with same name as private class function +Function ClassPrivateFunc() + ClassPrivateFunc = 1 +End Function + +Dim objShadow : Set objShadow = New TestLocalDimShadowsGlobalFunc +objShadow.TestShadow +objShadow.TestPrivate + sub test_dotIdentifiers ' test keywords that can also be an identifier after a dot Call ok(testObj.rem = 10, "testObj.rem = " & testObj.rem & " expected 10") @@ -3318,6 +3348,32 @@ Call ok(Err.Number = 13, "Eval type mismatch: Err.Number = " & Err.Number & " ex Call ok(Err.Source = "Microsoft VBScript runtime error", "Eval type mismatch: Err.Source = """ & Err.Source & """") On Error Goto 0 +' Duplicate Sub declarations: last one wins +dim dupSubResult +dupSubResult = 0 +Sub DupSub() + dupSubResult = 1 +End Sub +Sub DupSub() + dupSubResult = 2 +End Sub +DupSub +call ok(dupSubResult = 2, "dupSubResult = " & dupSubResult) + +' Sub replaced by Function with same name: Function wins +dim dupMixedResult +dupMixedResult = 0 +Sub DupMixed() + dupMixedResult = 1 +End Sub +Function DupMixed() + dupMixedResult = 2 + DupMixed = 42 +End Function +dim dupMixedRet +dupMixedRet = DupMixed() +call ok(dupMixedResult = 2, "dupMixedResult = " & dupMixedResult) +call ok(dupMixedRet = 42, "dupMixedRet = " & dupMixedRet) ' Test calling a dispatch variable as statement (invokes default property) funcCalled = "" diff --git a/dlls/vbscript/tests/run.c b/dlls/vbscript/tests/run.c index bc09f7a96c0..a23e04e79fb 100644 --- a/dlls/vbscript/tests/run.c +++ b/dlls/vbscript/tests/run.c @@ -3257,6 +3257,98 @@ static void test_parse_errors(void) "End Class\n", -4, -20, NULL, S_OK, 1051 + }, + { + /* Const redefined */ + L"Const X = 1\n" + "Const X = 2\n", + 1, 6, + L"Const X = 2", S_OK, 1041 + }, + { + /* Const redefined on same statement */ + L"Const C = 1, C = 2\n", + 0, 13, + L"Const C = 1, C = 2", S_OK, 1041 + }, + { + /* Dim after Const with same name */ + L"Const D = 1\n" + "Dim D\n", + 1, 4, + L"Dim D", S_OK, 1041 + }, + { + /* Const after Dim with same name */ + L"Dim E\n" + "Const E = 1\n", + 1, 6, + L"Const E = 1", S_OK, 1041 + }, + { + /* Sub after Const with same name */ + L"Const F = 1\n" + "Sub F()\n" + "End Sub\n", + 1, 4, + L"Sub F()", S_OK, 1041 + }, + { + /* Sub after Dim with same name */ + L"Dim G\n" + "Sub G()\n" + "End Sub\n", + 1, 4, + L"Sub G()", S_OK, 1041 + }, + { + /* Function after Const with same name */ + L"Const H = 1\n" + "Function H()\n" + "End Function\n", + 1, 9, + L"Function H()", S_OK, 1041 + }, + { + /* Const after Sub with same name */ + L"Sub I()\n" + "End Sub\n" + "Const I = 1\n", + 2, 6, + L"Const I = 1", S_OK, 1041 + }, + { + /* Dim after Sub with same name */ + L"Sub J()\n" + "End Sub\n" + "Dim J\n", + 2, 4, + L"Dim J", S_OK, 1041 + }, + { + /* Class after Const with same name */ + L"Const K = 1\n" + "Class K\n" + "End Class\n", + 1, 6, + L"Class K", S_OK, 1041 + }, + { + /* Class after Dim with same name */ + L"Dim L\n" + "Class L\n" + "End Class\n", + 1, 6, + L"Class L", S_OK, 1041 + }, + { + /* Class after Sub with same name */ + L"Sub M()\n" + "End Sub\n" + "Class M\n" + "End Class\n", + 2, 6, + L"Class M", S_OK, 1041 } }; HRESULT hres; -- GitLab https://gitlab.winehq.org/wine/wine/-/merge_requests/10464