Skip to content

Commit d6f8041

Browse files
committed
fix implicit convert for field init expression
1 parent 632feef commit d6f8041

5 files changed

Lines changed: 108 additions & 2 deletions

File tree

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1571,7 +1571,9 @@ impl<'a, 'ast> Generator<'a, 'ast> {
15711571
};
15721572
self.emit_function_start_mark(&ctx);
15731573
let value = match written {
1574-
InitializerValue::Expression(value) => self.lower_expression(&mut ctx, value),
1574+
// converted to the field's type: `float value = 0;` stores a
1575+
// Single, not the Int32 the literal is
1576+
InitializerValue::Expression(value) => self.owned_value_as(&mut ctx, value, &ty),
15751577
// `static int[] Steps = { 1, 2 };`
15761578
InitializerValue::Nested(nested) => {
15771579
self.lower_array_shorthand(&mut ctx, &ty, nested, nested.span())
@@ -4014,6 +4016,30 @@ fn literal_heap_init(expression: &Expression, udon_type: &str) -> Option<HeapIni
40144016
let value = if negated { -value } else { value };
40154017
i32::try_from(value).ok().map(HeapInit::Int32)
40164018
}
4019+
// an integer literal converts implicitly to the wider types: the
4020+
// default is then of the slot's type, as `float value = 0;` means
4021+
(LiteralExpression::Integer(text), "SystemInt64") => {
4022+
let raw: String = text.value.chars().filter(|c| *c != '_').collect();
4023+
let value = raw.parse::<i64>().ok()?;
4024+
Some(HeapInit::Int64(if negated { -value } else { value }))
4025+
}
4026+
(LiteralExpression::Integer(text), "SystemUInt32") if !negated => {
4027+
let raw: String = text.value.chars().filter(|c| *c != '_').collect();
4028+
raw.parse::<u32>().ok().map(HeapInit::UInt32)
4029+
}
4030+
(LiteralExpression::Integer(text), "SystemSingle") => {
4031+
let raw: String = text.value.chars().filter(|c| *c != '_').collect();
4032+
let value = raw.parse::<i64>().ok()?;
4033+
let value = if negated { -value } else { value };
4034+
// exactly representable, as C# folds it
4035+
(value.unsigned_abs() <= 1 << 24).then_some(HeapInit::Single(value as f32))
4036+
}
4037+
(LiteralExpression::Integer(text), "SystemDouble") => {
4038+
let raw: String = text.value.chars().filter(|c| *c != '_').collect();
4039+
let value = raw.parse::<i64>().ok()?;
4040+
let value = if negated { -value } else { value };
4041+
(value.unsigned_abs() <= 1 << 53).then_some(HeapInit::Double(value as f64))
4042+
}
40174043
(LiteralExpression::Real(text), "SystemSingle") => {
40184044
let raw: String = text.value.chars().filter(|c| *c != '_').collect();
40194045
let value = raw.strip_suffix(['f', 'F'])?.parse::<f32>().ok()?;

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,15 @@ impl<'a, 'ast> Generator<'a, 'ast> {
722722
}
723723
continue;
724724
};
725-
if let Some(value) = self.owned_value(ctx, initializer) {
725+
// converted to the field's type: `float inner = 4;` stores a
726+
// Single, not the Int32 the literal is
727+
let field_type = match self.signatures.members.get(&member) {
728+
Some(MemberSignature::Field(ty)) | Some(MemberSignature::Property(ty)) => {
729+
self.substitute(ty, &ctx.key.bindings)
730+
}
731+
_ => continue,
732+
};
733+
if let Some(value) = self.owned_value_as(ctx, initializer, &field_type) {
726734
let index = self.int_constant(slot_index as i32);
727735
self.set_element(ctx, object, index, value, 0..0);
728736
}

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12271,3 +12271,63 @@ fn operators_on_an_external_enum_compute_on_the_int_and_box_the_result() {
1227112271
));
1227212272
assert_eq!(int_of(&emulator, "plainComplement"), -6);
1227312273
}
12274+
12275+
#[test]
12276+
fn an_integer_initializer_is_stored_as_the_field_type() {
12277+
// `float value = 0;` — the literal is an Int32, the field a Single; the
12278+
// slot has to hold a Single, or the first `value * 0.92f` halts the VM
12279+
// (issue). Instance, static, plain-class and local fields alike.
12280+
let Some(emulator) = run_behaviour(
12281+
r#"
12282+
using MenSharp;
12283+
public class Holder { public float inner = 4; }
12284+
public class Thing : MenSharpBehaviour
12285+
{
12286+
public float value = 0;
12287+
public float negative = -3;
12288+
public double wide = 2;
12289+
public long big = 5;
12290+
static float shared = 1;
12291+
public float product;
12292+
public float negativeProduct;
12293+
public double wideProduct;
12294+
public long bigProduct;
12295+
public float sharedProduct;
12296+
public float innerProduct;
12297+
public float localProduct;
12298+
public void Start()
12299+
{
12300+
float local = 7;
12301+
product = value * 0.92f;
12302+
negativeProduct = negative * 0.5f;
12303+
wideProduct = wide * 1.5;
12304+
bigProduct = big * 2L;
12305+
sharedProduct = shared * 0.5f;
12306+
innerProduct = new Holder().inner * 0.5f;
12307+
localProduct = local * 0.5f;
12308+
}
12309+
}
12310+
"#,
12311+
"Thing",
12312+
"_start",
12313+
) else {
12314+
return;
12315+
};
12316+
let single = |name: &str| match emulator.value_of(name) {
12317+
Some(Value::Single(value)) => *value,
12318+
other => panic!("{name} = {other:?}"),
12319+
};
12320+
assert_eq!(single("product"), 0.0);
12321+
assert_eq!(single("negativeProduct"), -1.5);
12322+
assert!(matches!(emulator.value_of("wideProduct"), Some(Value::Double(v)) if *v == 3.0));
12323+
assert!(matches!(
12324+
emulator.value_of("bigProduct"),
12325+
Some(Value::Int64(10))
12326+
));
12327+
assert_eq!(single("sharedProduct"), 0.5);
12328+
assert_eq!(single("innerProduct"), 2.0);
12329+
assert_eq!(single("localProduct"), 3.5);
12330+
// the literal defaults are baked as the field's own type
12331+
assert!(matches!(emulator.value_of("value"), Some(Value::Single(v)) if *v == 0.0));
12332+
assert!(matches!(emulator.value_of("wide"), Some(Value::Double(v)) if *v == 2.0));
12333+
}

‎tests/unity-project/Assets/MenSharp/MenSharpRuntimeSmoke.cs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ public void EnumOps()
5151
keyName = key.ToString();
5252
}
5353

54+
// `float x = 0;` on a field the proxy does not transfer (private): the
55+
// slot must hold a Single, or the multiplication halts the VM (issue)
56+
private float scale = 0;
57+
public float scaled;
58+
public void Scale()
59+
{
60+
scaled = scale * 0.92f + 1f;
61+
}
62+
5463
const string greeting = "Hello";
5564
public string greeted;
5665
public void Greet()

‎tests/unity-project/Assets/Tests/Editor/MenSharpIntegrationTests.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ public IEnumerator GeneratedProgramsExecuteInTheSdkUdonVm()
140140
smoke.RunProgram("MakeTexture");
141141
Assert.AreEqual("RGBA32", smoke.GetProgramVariable("formatName"));
142142

143+
smoke.RunProgram("Scale");
144+
Assert.AreEqual(1f, smoke.GetProgramVariable("scaled"));
145+
143146
smoke.RunProgram("EnumOps");
144147
Assert.AreEqual(2, smoke.GetProgramVariable("splitCount"));
145148
Assert.AreEqual(1, smoke.GetProgramVariable("flagsValue"));

0 commit comments

Comments
 (0)