Skip to content

Commit 4f37d18

Browse files
committed
fix: address code review feedback
- EmitRegistrationField now owns the trailing semicolon, callers pass expression only (prevents fragile implicit contract) - Remove dead try/catch around RunClassConstructor — InfrastructureGenerator always emits both shell classes, so TypeLoadException cannot occur
1 parent cb97b83 commit 4f37d18

6 files changed

Lines changed: 12 additions & 55 deletions

TUnit.Core.SourceGenerator.Tests/DuplicateTypeNameAcrossAssembliesTests.InfrastructureGenerator_WithDuplicateTypeNames_CompilesSuccessfully.DotNet10_0.verified.txt

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,8 @@ file static class TUnitInfrastructure
9999
}
100100
catch { /* TUnit.Core not available - skip logging */ }
101101
}
102-
try
103-
{
104-
global::System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(global::TUnit.Generated.TUnit_TestRegistration).TypeHandle);
105-
}
106-
catch (global::System.TypeLoadException) { /* Type not emitted - no test registrations */ }
107-
try
108-
{
109-
global::System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(global::TUnit.Generated.TUnit_HookRegistration).TypeHandle);
110-
}
111-
catch (global::System.TypeLoadException) { /* Type not emitted - no hook registrations */ }
102+
global::System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(global::TUnit.Generated.TUnit_TestRegistration).TypeHandle);
103+
global::System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(global::TUnit.Generated.TUnit_HookRegistration).TypeHandle);
112104
try
113105
{
114106
global::TUnit.Core.GlobalContext.Current.GlobalLogger.LogDebug("[ModuleInitializer:TestsBase`1] TUnit infrastructure initialized");

TUnit.Core.SourceGenerator.Tests/DuplicateTypeNameAcrossAssembliesTests.InfrastructureGenerator_WithDuplicateTypeNames_CompilesSuccessfully.DotNet8_0.verified.txt

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,8 @@ file static class TUnitInfrastructure
9999
}
100100
catch { /* TUnit.Core not available - skip logging */ }
101101
}
102-
try
103-
{
104-
global::System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(global::TUnit.Generated.TUnit_TestRegistration).TypeHandle);
105-
}
106-
catch (global::System.TypeLoadException) { /* Type not emitted - no test registrations */ }
107-
try
108-
{
109-
global::System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(global::TUnit.Generated.TUnit_HookRegistration).TypeHandle);
110-
}
111-
catch (global::System.TypeLoadException) { /* Type not emitted - no hook registrations */ }
102+
global::System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(global::TUnit.Generated.TUnit_TestRegistration).TypeHandle);
103+
global::System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(global::TUnit.Generated.TUnit_HookRegistration).TypeHandle);
112104
try
113105
{
114106
global::TUnit.Core.GlobalContext.Current.GlobalLogger.LogDebug("[ModuleInitializer:TestsBase`1] TUnit infrastructure initialized");

TUnit.Core.SourceGenerator.Tests/DuplicateTypeNameAcrossAssembliesTests.InfrastructureGenerator_WithDuplicateTypeNames_CompilesSuccessfully.DotNet9_0.verified.txt

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,8 @@ file static class TUnitInfrastructure
9999
}
100100
catch { /* TUnit.Core not available - skip logging */ }
101101
}
102-
try
103-
{
104-
global::System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(global::TUnit.Generated.TUnit_TestRegistration).TypeHandle);
105-
}
106-
catch (global::System.TypeLoadException) { /* Type not emitted - no test registrations */ }
107-
try
108-
{
109-
global::System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(global::TUnit.Generated.TUnit_HookRegistration).TypeHandle);
110-
}
111-
catch (global::System.TypeLoadException) { /* Type not emitted - no hook registrations */ }
102+
global::System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(global::TUnit.Generated.TUnit_TestRegistration).TypeHandle);
103+
global::System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(global::TUnit.Generated.TUnit_HookRegistration).TypeHandle);
112104
try
113105
{
114106
global::TUnit.Core.GlobalContext.Current.GlobalLogger.LogDebug("[ModuleInitializer:TestsBase`1] TUnit infrastructure initialized");

TUnit.Core.SourceGenerator.Tests/DuplicateTypeNameAcrossAssembliesTests.InfrastructureGenerator_WithDuplicateTypeNames_CompilesSuccessfully.Net4_7.verified.txt

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,8 @@ file static class TUnitInfrastructure
9999
}
100100
catch { /* TUnit.Core not available - skip logging */ }
101101
}
102-
try
103-
{
104-
global::System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(global::TUnit.Generated.TUnit_TestRegistration).TypeHandle);
105-
}
106-
catch (global::System.TypeLoadException) { /* Type not emitted - no test registrations */ }
107-
try
108-
{
109-
global::System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(global::TUnit.Generated.TUnit_HookRegistration).TypeHandle);
110-
}
111-
catch (global::System.TypeLoadException) { /* Type not emitted - no hook registrations */ }
102+
global::System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(global::TUnit.Generated.TUnit_TestRegistration).TypeHandle);
103+
global::System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(global::TUnit.Generated.TUnit_HookRegistration).TypeHandle);
112104
try
113105
{
114106
global::TUnit.Core.GlobalContext.Current.GlobalLogger.LogDebug("[ModuleInitializer:TestsBase`1] TUnit infrastructure initialized");

TUnit.Core.SourceGenerator/CodeGenerators/InfrastructureGenerator.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -452,20 +452,9 @@ private static void GenerateCode(SourceProductionContext context, AssemblyInfoMo
452452
// Per-class/per-method test sources and per-hook sources contribute static field
453453
// initializers to shared partial classes. RunClassConstructor forces each .cctor
454454
// to execute, performing all registrations in ONE JIT-compiled method per class.
455-
sourceBuilder.AppendLine("try");
456-
sourceBuilder.AppendLine("{");
457-
sourceBuilder.Indent();
455+
// No try/catch needed — InfrastructureGenerator always emits both shell classes.
458456
sourceBuilder.AppendLine("global::System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(global::TUnit.Generated.TUnit_TestRegistration).TypeHandle);");
459-
sourceBuilder.Unindent();
460-
sourceBuilder.AppendLine("}");
461-
sourceBuilder.AppendLine("catch (global::System.TypeLoadException) { /* Type not emitted - no test registrations */ }");
462-
sourceBuilder.AppendLine("try");
463-
sourceBuilder.AppendLine("{");
464-
sourceBuilder.Indent();
465457
sourceBuilder.AppendLine("global::System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(global::TUnit.Generated.TUnit_HookRegistration).TypeHandle);");
466-
sourceBuilder.Unindent();
467-
sourceBuilder.AppendLine("}");
468-
sourceBuilder.AppendLine("catch (global::System.TypeLoadException) { /* Type not emitted - no hook registrations */ }");
469458
sourceBuilder.AppendLine();
470459

471460
sourceBuilder.AppendLine("try");

TUnit.Core.SourceGenerator/Generators/TestMetadataGenerator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2407,7 +2407,7 @@ private static string EscapeString(string value)
24072407
private static void GenerateTestRegistrationField(CodeWriter writer, TestMethodMetadata testMethod, string uniqueClassName)
24082408
{
24092409
EmitRegistrationField(writer, uniqueClassName,
2410-
$"global::TUnit.Core.SourceRegistrar.RegisterReturn({GenerateTypeReference(testMethod.TypeSymbol, testMethod.IsGenericType)}, new {uniqueClassName}());");
2410+
$"global::TUnit.Core.SourceRegistrar.RegisterReturn({GenerateTypeReference(testMethod.TypeSymbol, testMethod.IsGenericType)}, new {uniqueClassName}())");
24112411
}
24122412

24132413
/// <summary>
@@ -2420,7 +2420,7 @@ private static void EmitRegistrationField(CodeWriter writer, string fieldName, s
24202420
writer.AppendLine("internal static partial class TUnit_TestRegistration");
24212421
writer.AppendLine("{");
24222422
writer.Indent();
2423-
writer.AppendLine($"static readonly int _r_{fieldName} = {registrarCall}");
2423+
writer.AppendLine($"static readonly int _r_{fieldName} = {registrarCall};");
24242424
writer.Unindent();
24252425
writer.AppendLine("}");
24262426
}
@@ -3031,7 +3031,7 @@ private static void GeneratePerClassTestSource(SourceProductionContext context,
30313031
writer.AppendLine("}");
30323032

30333033
EmitRegistrationField(writer, classGroup.TestSourceName,
3034-
$"global::TUnit.Core.SourceRegistrar.RegisterReturn(typeof({classGroup.ClassFullyQualified}), {classGroup.TestSourceName}.GetTests, {classGroup.TestSourceName}.EnumerateTestDescriptors);");
3034+
$"global::TUnit.Core.SourceRegistrar.RegisterReturn(typeof({classGroup.ClassFullyQualified}), {classGroup.TestSourceName}.GetTests, {classGroup.TestSourceName}.EnumerateTestDescriptors)");
30353035

30363036
context.AddSource($"{classGroup.TestSourceName}.g.cs", SourceText.From(writer.ToString(), Encoding.UTF8));
30373037
}

0 commit comments

Comments
 (0)