Skip to content

Commit c08fd9a

Browse files
committed
fix null string
1 parent e1bfa00 commit c08fd9a

2 files changed

Lines changed: 68 additions & 4 deletions

File tree

‎src/men-sharp-codegen/src/generator/expressions.rs‎

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5269,6 +5269,11 @@ impl<'a, 'ast> Generator<'a, 'ast> {
52695269
}
52705270
LiteralExpression::InterpolatedString(interpolated) => {
52715271
let mut current: Option<DataId> = None;
5272+
// `$"{s}"` with `s` a null string: the hole's text is `s`
5273+
// itself, and with nothing to concatenate it to, nothing turns
5274+
// that null into the "" C# gives. Noted here, fixed after the
5275+
// loop, once it is known to be the only piece.
5276+
let mut sole_string_hole = false;
52725277
for part in interpolated.parts {
52735278
let piece_slot = match part {
52745279
InterpolationPart::Text(text) => {
@@ -5308,12 +5313,17 @@ impl<'a, 'ast> Generator<'a, 'ast> {
53085313
hole.span.clone(),
53095314
);
53105315
// `{x,-8}` — padded to the field width
5311-
self.align_to_width(ctx, text, hole)
5316+
let padded = self.align_to_width(ctx, text, hole);
5317+
sole_string_hole = current.is_none()
5318+
&& padded == slot
5319+
&& self.extern_type_name(&ty).as_deref() == Some("SystemString");
5320+
padded
53125321
}
53135322
};
53145323
current = Some(match current {
53155324
None => piece_slot,
53165325
Some(previous) => {
5326+
sole_string_hole = false;
53175327
let out = self.temp("SystemString");
53185328
self.call_extern(
53195329
ctx,
@@ -5325,9 +5335,23 @@ impl<'a, 'ast> Generator<'a, 'ast> {
53255335
}
53265336
});
53275337
}
5328-
let slot = current.unwrap_or_else(|| {
5329-
self.constant("SystemString", "", HeapInit::Str(String::new()))
5330-
});
5338+
let slot = match current {
5339+
Some(value) if sole_string_hole => {
5340+
// Concat treats a null as "", which is the result C#
5341+
// gives for an interpolated null
5342+
let empty = self.string_constant("");
5343+
let out = self.temp("SystemString");
5344+
self.call_extern(
5345+
ctx,
5346+
"SystemString.__Concat__SystemString_SystemString__SystemString",
5347+
&[value, empty, out],
5348+
interpolated.span.clone(),
5349+
);
5350+
out
5351+
}
5352+
Some(value) => value,
5353+
None => self.constant("SystemString", "", HeapInit::Str(String::new())),
5354+
};
53315355
Piece::Value(slot, self.corlib_type("String"))
53325356
}
53335357
other => {

‎src/men-sharp-compiler/tests/codegen.rs‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,46 @@ fn strings_and_interpolation() {
372372
assert_eq!(string_of(&emulator, "text"), "door: 4 visits");
373373
}
374374

375+
#[test]
376+
fn interpolating_a_null_string_alone_gives_an_empty_string() {
377+
// `$"{s}"` is `s` with nothing to concatenate it to, and C# still turns
378+
// a null into "" (issue: a const string null came out null)
379+
let Some(emulator) = run(
380+
r#"
381+
namespace Game
382+
{
383+
public class Program
384+
{
385+
public static string fromConst;
386+
public static string fromVariable;
387+
public static string fromNull;
388+
public static string joined;
389+
public static string other;
390+
public static void Main()
391+
{
392+
const string value = null;
393+
string variable = null;
394+
string text = "x";
395+
fromConst = $"{value}";
396+
fromVariable = $"{variable}";
397+
fromNull = $"{null}";
398+
joined = $"{variable}{value}";
399+
other = $"{text}";
400+
}
401+
}
402+
}
403+
"#,
404+
"Main",
405+
) else {
406+
return;
407+
};
408+
assert_eq!(string_of(&emulator, "fromConst"), "");
409+
assert_eq!(string_of(&emulator, "fromVariable"), "");
410+
assert_eq!(string_of(&emulator, "fromNull"), "");
411+
assert_eq!(string_of(&emulator, "joined"), "");
412+
assert_eq!(string_of(&emulator, "other"), "x");
413+
}
414+
375415
#[test]
376416
fn recursion_just_works() {
377417
// static frames plus a save/restore stack woven in around the calls that

0 commit comments

Comments
 (0)