From 397b56e1e313eefcba360e7162768ec959b50b51 Mon Sep 17 00:00:00 2001 From: Vivien Voros Date: Fri, 19 Jan 2024 15:37:59 +0100 Subject: [PATCH] [ArkTS verifier] Verifier fails on string concatenation with undefined Contcatenation some string with 'undefined' as value. Fixes internal issue #14767 Change-Id: I976f8d1ef66c949afd50233c86df5d3bb621e950 Signed-off-by: Vivien Voros --- ets2panda/compiler/core/ETSGen.cpp | 7 ++- .../test/runtime/ets/stringConcatenation.ets | 44 +++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 ets2panda/test/runtime/ets/stringConcatenation.ets diff --git a/ets2panda/compiler/core/ETSGen.cpp b/ets2panda/compiler/core/ETSGen.cpp index 9603461920..cd29ebce85 100644 --- a/ets2panda/compiler/core/ETSGen.cpp +++ b/ets2panda/compiler/core/ETSGen.cpp @@ -2324,8 +2324,11 @@ void ETSGen::StringBuilderAppend(const ir::AstNode *node, VReg builder) signature = Signatures::BUILTIN_STRING_BUILDER_APPEND_BUILTIN_STRING; } - if ((GetAccumulatorType()->IsETSObjectType() || GetAccumulatorType()->IsETSTypeParameter()) && - !GetAccumulatorType()->IsETSStringType()) { + const checker::Type *accumulatorType = GetAccumulatorType(); + bool isNullOrUndefined = accumulatorType->ContainsNull() || accumulatorType->ContainsUndefined(); + bool isETSObjectOrTypeParam = accumulatorType->IsETSObjectType() || accumulatorType->IsETSTypeParameter(); + bool isStringType = accumulatorType->IsETSStringType(); + if (isETSObjectOrTypeParam && (!isStringType || isNullOrUndefined)) { if (Checker()->MayHaveNullValue(GetAccumulatorType())) { Label *ifnull = AllocLabel(); Label *end = AllocLabel(); diff --git a/ets2panda/test/runtime/ets/stringConcatenation.ets b/ets2panda/test/runtime/ets/stringConcatenation.ets new file mode 100644 index 0000000000..ff66d04edc --- /dev/null +++ b/ets2panda/test/runtime/ets/stringConcatenation.ets @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +function main() { + let src: string | undefined = undefined; + let s = "_str_"; + let udef = undefined; + let a :string = "A_"; + let n : string | undefined | null = null; + + let dst: string = "" + src; + let dst_s : string = src + ""; + let a_dst :string = a + dst; + let n_dst : string = n + a_dst; + let dst_a : string = dst + a; + + + + assert dst == "undefined"; + assert dst_s == "undefined"; + assert a_dst == "A_undefined"; + assert dst_a == "undefinedA_"; + assert n_dst == "nullA_undefined"; + assert udef + s == "undefined_str_"; + assert s + udef == "_str_undefined"; + assert a + n == "A_null"; + assert a + s == "A__str_"; + assert a + s + udef == "A__str_undefined"; + assert a + udef + a == "A_undefinedA_"; + assert udef + s + n == "undefined_str_null"; + +} -- Gitee