Skip to content

Commit e8217c7

Browse files
Fix implicit string conversion breaking NativeStrings (#6035)
* Fix implicit string conversion breaking NativeStrings * Allow string literals to coerce to either string type * Add test for CPU string passing --------- Co-authored-by: Yong He <yonghe@outlook.com>
1 parent b72e62f commit e8217c7

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

source/slang/core.meta.slang

+6
Original file line numberDiff line numberDiff line change
@@ -1463,6 +1463,11 @@ struct String
14631463
__intrinsic_op($(kIROp_MakeString))
14641464
__init(double val);
14651465

1466+
[require(cpp)]
1467+
__implicit_conversion($(kConversionCost_None))
1468+
__intrinsic_op($(kIROp_MakeString))
1469+
__init(NativeString value);
1470+
14661471
/// Returns the length of the string.
14671472
[require(cpp)]
14681473
int64_t getLength();
@@ -1502,6 +1507,7 @@ struct NativeString
15021507

15031508
property int length { [__unsafeForceInlineEarly] get{return getLength();} }
15041509

1510+
__implicit_conversion($(kConversionCost_None))
15051511
__intrinsic_op($(kIROp_getNativeStr))
15061512
__init(String value);
15071513
};

source/slang/slang-check-conversion.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -785,8 +785,8 @@ bool SemanticsVisitor::_coerce(
785785
return true;
786786
}
787787

788-
// If both are string types we assume they are convertable in both directions
789-
if (as<StringTypeBase>(fromType) && as<StringTypeBase>(toType))
788+
// Assume string literals are convertible to any string type.
789+
if (as<StringLiteralExpr>(fromExpr) && as<StringTypeBase>(toType))
790790
{
791791
if (outToExpr)
792792
*outToExpr = fromExpr;
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//TEST:EXECUTABLE:
2+
3+
void printer(NativeString string)
4+
{
5+
printf(string);
6+
}
7+
8+
void indirectPrinter(String string)
9+
{
10+
printer(string);
11+
}
12+
13+
export __extern_cpp int main()
14+
{
15+
indirectPrinter("success\n");
16+
return 0;
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
result code = 0
2+
standard error = {
3+
}
4+
standard output = {
5+
success
6+
}

0 commit comments

Comments
 (0)