diff --git a/compiler/src/builtins/builtins.cpp b/compiler/src/builtins/builtins.cpp index 951c145f..4742108f 100644 --- a/compiler/src/builtins/builtins.cpp +++ b/compiler/src/builtins/builtins.cpp @@ -722,6 +722,10 @@ struct builtin_cast : builtin { code << dt_compiler->wrap_in_paren(args[1].first); } else if (args[1].second.datatype_->is_none()) { code << "NULL"; + } else if (args[1].second.datatype_->const_unwrap()->is_sr()) { + code << "((" + << dt_compiler->convert_dt(out_dt, datatype_location::CAST, "", "") + << ")yk__bstr_get_reference(" << args[1].first << "))"; } else { code << "((" << dt_compiler->convert_dt(out_dt, datatype_location::CAST, "", "") diff --git a/compiler/test_data/bug_fixes/easy_cstr.yaka b/compiler/test_data/bug_fixes/easy_cstr.yaka index b8d1ed6a..29ef0cc0 100644 --- a/compiler/test_data/bug_fixes/easy_cstr.yaka +++ b/compiler/test_data/bug_fixes/easy_cstr.yaka @@ -5,4 +5,6 @@ def myfunc(d: c.CStr) -> None: def main() -> int: myfunc(cast("c.CStr", "hello\nworld\n")) + a: sr = "bug\nfix\nis good" + myfunc(cast("c.CStr", a)) return 0 diff --git a/compiler/test_data/bug_fixes/easy_cstr.yaka.c b/compiler/test_data/bug_fixes/easy_cstr.yaka.c index baebe06f..655fb0ec 100644 --- a/compiler/test_data/bug_fixes/easy_cstr.yaka.c +++ b/compiler/test_data/bug_fixes/easy_cstr.yaka.c @@ -14,6 +14,8 @@ void yy__myfunc(yy__c_CStr yy__d) int32_t yy__main() { yy__myfunc(((yy__c_CStr)"hello\nworld\n")); + struct yk__bstr yy__a = yk__bstr_s("bug\nfix\nis good" , 15); + yy__myfunc(((yy__c_CStr)yk__bstr_get_reference(yy__a))); return INT32_C(0); } #if defined(YK__MINIMAL_MAIN)