From 8d627c02ce905c1fa08d167ef62e62689e856518 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20=C4=8Cert=C3=ADk?= Date: Thu, 3 Mar 2022 12:33:47 -0700 Subject: [PATCH 1/2] LLVM: initialize allocatable character to nullptr --- src/libasr/codegen/asr_to_llvm.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/libasr/codegen/asr_to_llvm.cpp b/src/libasr/codegen/asr_to_llvm.cpp index 757f6dc83b..51f77bc516 100644 --- a/src/libasr/codegen/asr_to_llvm.cpp +++ b/src/libasr/codegen/asr_to_llvm.cpp @@ -1502,6 +1502,10 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor std::string empty(strlen, ' '); llvm::Value *init_value = builder->CreateGlobalStringPtr(s2c(al, empty)); builder->CreateStore(init_value, target_var); + } else if (strlen == -2) { + // Allocatable string. Initialize to `nullptr` (unallocated) + llvm::Value *init_value = llvm::Constant::getNullValue(type); + builder->CreateStore(init_value, target_var); } else if (strlen == -3) { LFORTRAN_ASSERT(t->m_len_expr) this->visit_expr(*t->m_len_expr); From b4d50db12d87f051326220dd8f27e72cf5d91be4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20=C4=8Cert=C3=ADk?= Date: Thu, 3 Mar 2022 12:38:19 -0700 Subject: [PATCH 2/2] Add a test for string assignment --- integration_tests/run_tests.py | 1 + integration_tests/test_str_01.py | 13 +++++++++++++ 2 files changed, 14 insertions(+) create mode 100644 integration_tests/test_str_01.py diff --git a/integration_tests/run_tests.py b/integration_tests/run_tests.py index 1316e74d92..cfa2984d67 100755 --- a/integration_tests/run_tests.py +++ b/integration_tests/run_tests.py @@ -11,6 +11,7 @@ "expr_03.py", "expr_04.py", "expr_05.py", + "test_str_01.py", "modules_01.py", #"modules_02.py", "test_math.py", diff --git a/integration_tests/test_str_01.py b/integration_tests/test_str_01.py new file mode 100644 index 0000000000..ac0c08dbd4 --- /dev/null +++ b/integration_tests/test_str_01.py @@ -0,0 +1,13 @@ +def f(): + x: str + x = "ok" + print(x) + assert x == "ok" + x = "abcdefghijkl" + print(x) + assert x == "abcdefghijkl" + x = x + "123" + print(x) + assert x == "abcdefghijkl123" + +f()