diff --git a/ets2panda/compiler/core/ETSGen.cpp b/ets2panda/compiler/core/ETSGen.cpp index 9603461920f60c560ea4dc739ebc7265f423bd9a..cd29ebce8505b853dd5a3c857c919ebafcf57ee3 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 0000000000000000000000000000000000000000..ff66d04edc5a5967d4e65bf5101872179d05987e --- /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"; + +}