@@ -16216,3 +16216,141 @@ fn compound_constants_use_boolean_locals_without_converting_bool_to_int() {
1621616216 };
1621716217 assert!(messages.is_empty(), "{messages:?}");
1621816218}
16219+
16220+ #[test]
16221+ fn chained_constants_are_evaluated_once_per_field() {
16222+ // `S1 = S0 + 1; S2 = S1 + 1; ...` in another file, and a chain declared
16223+ // below its use: each field is bound once, whatever the chain length,
16224+ // and its value survives a narrowing compound assignment.
16225+ let mut chain = String::from("public static class Chain { public const int S0 = 0;");
16226+ for index in 1..40 {
16227+ chain.push_str(&format!(" public const int S{index} = S{} + 1;", index - 1));
16228+ }
16229+ chain.push('}');
16230+ let mut sources = vec![
16231+ SourceCode::new(
16232+ "caller.cs",
16233+ r#"
16234+ using MenSharp;
16235+ public class Probe : MenSharpBehaviour {
16236+ public void Interact() {
16237+ byte b = 0; b += Chain.S39; b += Local.L29;
16238+ uint u = 0; u += Chain.S39;
16239+ int x = Chain.S39 * 2;
16240+ }
16241+ public static class Local {
16242+ public const int L0 = 0; public const int L1 = L0 + 1; public const int L2 = L1 + 1;
16243+ public const int L3 = L2 + 1; public const int L4 = L3 + 1; public const int L5 = L4 + 1;
16244+ public const int L6 = L5 + 1; public const int L7 = L6 + 1; public const int L8 = L7 + 1;
16245+ public const int L9 = L8 + 1; public const int L10 = L9 + 1; public const int L11 = L10 + 1;
16246+ public const int L12 = L11 + 1; public const int L13 = L12 + 1; public const int L14 = L13 + 1;
16247+ public const int L15 = L14 + 1; public const int L16 = L15 + 1; public const int L17 = L16 + 1;
16248+ public const int L18 = L17 + 1; public const int L19 = L18 + 1; public const int L20 = L19 + 1;
16249+ public const int L21 = L20 + 1; public const int L22 = L21 + 1; public const int L23 = L22 + 1;
16250+ public const int L24 = L23 + 1; public const int L25 = L24 + 1; public const int L26 = L25 + 1;
16251+ public const int L27 = L26 + 1; public const int L28 = L27 + 1; public const int L29 = L28 + 1;
16252+ }
16253+ }
16254+ "#,
16255+ ),
16256+ SourceCode::new("chain.cs", chain),
16257+ ];
16258+ sources.extend(Compiler::corlib_sources());
16259+ let Some(dir) = dotnet_shared_dir() else {
16260+ return;
16261+ };
16262+ let compiler = Compiler::new(CompilerSettings::default()).unwrap();
16263+ let bytes = vec![std::fs::read(dir.join("System.Private.CoreLib.dll")).unwrap()];
16264+ let references = compiler.load_references(&bytes).unwrap();
16265+ let files = compiler.parse(sources);
16266+ let declarations = compiler.collect_declarations(&files);
16267+ let signatures = compiler.resolve_signatures(&declarations, &references);
16268+ let started = std::time::Instant::now();
16269+ let bodies = compiler.check_bodies(&declarations, &signatures, &references);
16270+ assert!(bodies.errors.is_empty(), "{:#?}", bodies.errors);
16271+ assert!(
16272+ started.elapsed() < std::time::Duration::from_secs(5),
16273+ "constant chains must not be re-evaluated per reference"
16274+ );
16275+ }
16276+
16277+ #[test]
16278+ fn cyclic_constants_across_classes_are_rejected_without_looping() {
16279+ // The memo's in-progress set catches a cycle through two classes in
16280+ // two files; both fields are then non-constant.
16281+ let mut sources = vec![
16282+ SourceCode::new(
16283+ "caller.cs",
16284+ r#"
16285+ using MenSharp;
16286+ public class Probe : MenSharpBehaviour {
16287+ public void Interact() { byte b = 0; b += Left.K; b += Right.L; }
16288+ }
16289+ public static class Left { public const int K = Right.L + 1; }
16290+ "#,
16291+ ),
16292+ SourceCode::new(
16293+ "right.cs",
16294+ "public static class Right { public const int L = Left.K + 1; }",
16295+ ),
16296+ ];
16297+ sources.extend(Compiler::corlib_sources());
16298+ let Some(dir) = dotnet_shared_dir() else {
16299+ return;
16300+ };
16301+ let compiler = Compiler::new(CompilerSettings::default()).unwrap();
16302+ let bytes = vec![std::fs::read(dir.join("System.Private.CoreLib.dll")).unwrap()];
16303+ let references = compiler.load_references(&bytes).unwrap();
16304+ let files = compiler.parse(sources);
16305+ let declarations = compiler.collect_declarations(&files);
16306+ let signatures = compiler.resolve_signatures(&declarations, &references);
16307+ let bodies = compiler.check_bodies(&declarations, &signatures, &references);
16308+ let mismatches = bodies
16309+ .errors
16310+ .iter()
16311+ .filter(|error| {
16312+ matches!(
16313+ error.kind,
16314+ men_sharp_semantics::SemanticErrorKind::TypeMismatch { .. }
16315+ )
16316+ })
16317+ .count();
16318+ assert_eq!(mismatches, 2, "{:#?}", bodies.errors);
16319+ }
16320+
16321+ #[test]
16322+ fn chained_constants_initialize_in_declaration_order() {
16323+ // `S3 = S2 + 1` is initializer code (not a baked literal), so the class's
16324+ // unit must create and run every field's initializer in textual order —
16325+ // not only the fields a body read, which put S3 before S2 and gave 1
16326+ let Some(emulator) = run_behaviour(
16327+ r#"
16328+ using MenSharp;
16329+ public class Probe : MenSharpBehaviour {
16330+ public int a; public int b; public int c;
16331+ public void Interact() {
16332+ a = Chain.S3;
16333+ b = Chain.S3 * 2;
16334+ c = Chain.T3;
16335+ }
16336+ }
16337+ public static class Chain {
16338+ public const int S0 = 0;
16339+ public const int S1 = S0 + 1;
16340+ public const int S2 = S1 + 1;
16341+ public const int S3 = S2 + 1;
16342+ public static int T0 = 10;
16343+ public static int T1 = T0 + 1;
16344+ public static int T2 = T1 + 1;
16345+ public static int T3 = T2 + 1;
16346+ }
16347+ "#,
16348+ "Probe",
16349+ "_interact",
16350+ ) else {
16351+ return;
16352+ };
16353+ assert_eq!(int_of(&emulator, "a"), 3);
16354+ assert_eq!(int_of(&emulator, "b"), 6);
16355+ assert_eq!(int_of(&emulator, "c"), 13);
16356+ }
0 commit comments