stryker-mutator / stryker-net

Mutation testing for .NET core and .NET framework!
https://stryker-mutator.io
Apache License 2.0
1.75k stars 176 forks source link

Compile error: Use of unassigned local variable #2778

Open jesperolsson-se opened 7 months ago

jesperolsson-se commented 7 months ago

Describe the bug When running Stryker against my project, it asks me to report the following issue.

[11:09:46 WRN] Stryker.NET encountered a compile error in C:\git\stryker_bug\StrykerBug\ClassLibrary1\MessageHandlerBase.cs (at 12:15) with message: Use of unassigned local variable 'typedMessage' (Source code: typedMessage)
[11:09:46 WRN] Safe Mode! Stryker will try to continue by rolling back all mutations in method. This should not happen, please report this as an issue on github with the previous error message.

Logs

2023-11-25T11:11:19.5183205+01:00  [DBG] Stryker started with options: StrykerOptions { MsBuildPath: null, DevMode: False, ProjectPath: "C:\git\stryker_bug\StrykerBug\TestProject1", IsSolutionContext: False, WorkingDirectory: "C:\git\stryker_bug\StrykerBug\TestProject1", OutputPath: "C:\git\stryker_bug\StrykerBug\TestProject1\StrykerOutput\2023-11-25.11-11-19", ReportPath: "C:\git\stryker_bug\StrykerBug\TestProject1\StrykerOutput\2023-11-25.11-11-19\reports", ReportFileName: "mutation-report", SolutionPath: null, TargetFramework: null, LogOptions: LogOptions { LogToFile: True, LogLevel: Information }, MutationLevel: Standard, Thresholds: Thresholds { High: 80, Low: 60, Break: 0 }, AdditionalTimeout: 5000, LanguageVersion: Default, Concurrency: 8, SourceProjectName: "", TestProjects: [], TestCaseFilter: "", Reporters: [Progress, Html], WithBaseline: False, BaselineProvider: Disk, AzureFileStorageUrl: "", AzureFileStorageSas: "", DashboardUrl: "https://dashboard.stryker-mutator.io", DashboardApiKey: null, Since: False, SinceTarget: "master", DiffIgnoreChanges: [], FallbackVersion: "master", ModuleName: "", ReportTypeToOpen: null, Mutate: [FilePattern { Glob: Glob { Tokens: [WildcardDirectoryToken { TrailingPathSeparator: PathSeparatorToken { Value: \ }, LeadingPathSeparator: null }, WildcardToken {  }] }, IsExclude: False, TextSpans: [TextSpan { Start: 0, End: 2147483647, Length: 2147483647, IsEmpty: False }] }], IgnoredMethods: [], ExcludedMutations: [], ExcludedLinqExpressions: [], OptimizationMode: CoverageBasedTest, ProjectName: "", ProjectVersion: "", BreakOnInitialTestFailure: False } (fac41b16)
2023-11-25T11:11:19.5681463+01:00  [INF] Analysis starting. (7076a364)
2023-11-25T11:11:19.5714569+01:00  [DBG] Using "C:\git\stryker_bug\StrykerBug\TestProject1\TestProject1.csproj" as test project (00c34121)
2023-11-25T11:11:19.5719325+01:00  [DBG] Analyzing project file "C:\git\stryker_bug\StrykerBug\TestProject1\TestProject1.csproj" (59a92a9a)
2023-11-25T11:11:21.2572161+01:00  [DBG] Using "C:\git\stryker_bug\StrykerBug\ClassLibrary1\ClassLibrary1.csproj" as project under test (b012007c)
2023-11-25T11:11:21.2572564+01:00  [DBG] Analyzing project file "C:\git\stryker_bug\StrykerBug\ClassLibrary1\ClassLibrary1.csproj" (59a92a9a)
2023-11-25T11:11:23.3062122+01:00  [DBG] Skipping auto-generated code file: "C:\git\stryker_bug\StrykerBug\ClassLibrary1\obj\Debug\net7.0\ClassLibrary1.GlobalUsings.g.cs" (1e1226c3)
2023-11-25T11:11:23.3110634+01:00  [DBG] Skipping auto-generated code file: "C:\git\stryker_bug\StrykerBug\ClassLibrary1\obj\Debug\net7.0\.NETCoreApp,Version=v7.0.AssemblyAttributes.cs" (1e1226c3)
2023-11-25T11:11:23.3166281+01:00  [DBG] Skipping auto-generated code file: "C:\git\stryker_bug\StrykerBug\ClassLibrary1\obj\Debug\net7.0\ClassLibrary1.AssemblyInfo.cs" (1e1226c3)
2023-11-25T11:11:23.3199805+01:00  [INF] Found project "C:\git\stryker_bug\StrykerBug\ClassLibrary1\ClassLibrary1.csproj" to mutate. (2d705ac3)
2023-11-25T11:11:23.3446002+01:00  [INF] Analysis complete. (f039f576)
2023-11-25T11:11:23.3602277+01:00  [INF] Building test project "C:\git\stryker_bug\StrykerBug\TestProject1\TestProject1.csproj" (1/1) (e2c15ab4)
2023-11-25T11:11:23.4220316+01:00  [DBG] Started initial build using "dotnet build" (a4e1a5e2)
2023-11-25T11:11:23.4220975+01:00  [DBG] Initial build using path: "TestProject1.csproj" (657c8664)
2023-11-25T11:11:25.7464833+01:00  [DBG] Initial build successful (61599426)
2023-11-25T11:11:25.8464405+01:00  [DBG] VsTest zip was copied to: "C:\Users\jolsson\AppData\Local\Temp\zxoxukde.21c\.vstest\vstest.zip" (8e340ff1)
2023-11-25T11:11:28.8271992+01:00  [DBG] VsTest zip was unzipped to: "C:\Users\jolsson\AppData\Local\Temp\zxoxukde.21c\.vstest" (679800d3)
2023-11-25T11:11:28.8662714+01:00  [DBG] Using vstest from deployed vstest package (6132c740)
2023-11-25T11:11:28.8711457+01:00  [DBG] Using vstest.console: "WINDOWS" for OS "C:\Users\jolsson\AppData\Local\Temp\zxoxukde.21c\.vstest\tools\net462\vstest.console.exe" (38dfd549)
2023-11-25T11:11:31.4744263+01:00  [INF] Number of tests found: 1 for project "C:\git\stryker_bug\StrykerBug\ClassLibrary1\ClassLibrary1.csproj". Initial test run started. (a32fa62b)
2023-11-25T11:11:32.5310283+01:00  [DBG] Runner 5: Received testrun complete. (c3662fd2)
2023-11-25T11:11:32.5344504+01:00  [DBG] Runner 5: Test session finished. (a1ab700a)
2023-11-25T11:11:32.5481956+01:00  [DBG] Initial test run output: "". (e3b6099a)
2023-11-25T11:11:34.4325351+01:00  [DBG] Mutating C:\git\stryker_bug\StrykerBug\ClassLibrary1\MessageHandlerBase.cs (e9e2e7e7)
2023-11-25T11:11:34.5737734+01:00  [DBG] Mutant 0 created "{
        if (!(message is T typedMessage))
        {
            throw new ArgumentException(
                $@\"Expected parameter to be of type {nameof(T)} but it was {message.GetType().Name}\",
                nameof(message)
            );
        }

        Handle(typedMessage);
    }" -> "{}" using "Stryker.Core.Mutators.BlockMutator" (310d25d3)
2023-11-25T11:11:34.5946621+01:00  [DBG] Mutant 1 created "!(message is T typedMessage)" -> "(message is T typedMessage)" using "Stryker.Core.Mutators.PrefixUnaryMutator" (310d25d3)
2023-11-25T11:11:34.6074294+01:00  [DBG] Mutant 2 created "T typedMessage" -> "not T typedMessage" using "Stryker.Core.Mutators.IsPatternExpressionMutator" (310d25d3)
2023-11-25T11:11:34.6107234+01:00  [DBG] Mutant 3 created "{
            throw new ArgumentException(
                $@\"Expected parameter to be of type {nameof(T)} but it was {message.GetType().Name}\",
                nameof(message)
            );
        }" -> "{}" using "Stryker.Core.Mutators.BlockMutator" (310d25d3)
2023-11-25T11:11:34.6124208+01:00  [DBG] Mutant 4 created "throw new ArgumentException(
                $@\"Expected parameter to be of type {nameof(T)} but it was {message.GetType().Name}\",
                nameof(message)
            );" -> ";" using "Stryker.Core.Mutators.StatementMutator" (310d25d3)
2023-11-25T11:11:34.6216652+01:00  [DBG] Mutant 5 created "$@\"Expected parameter to be of type {nameof(T)} but it was {message.GetType().Name}\"" -> "$\"\"" using "Stryker.Core.Mutators.InterpolatedStringMutator" (310d25d3)
2023-11-25T11:11:34.6588482+01:00  [DBG] Mutant 6 created "Handle(typedMessage);" -> ";" using "Stryker.Core.Mutators.StatementMutator" (310d25d3)
2023-11-25T11:11:34.6720258+01:00  [DBG] Mutating C:\git\stryker_bug\StrykerBug\ClassLibrary1\MsgBase.cs (3b24d790)
2023-11-25T11:11:34.6781948+01:00  [DBG] 7 mutants created (edec1dee)
2023-11-25T11:11:34.8326619+01:00  [DBG] Trying compilation for the "first" time. (f2d91e8b)
2023-11-25T11:11:37.5631199+01:00  [DBG] Compilation failed (5193a3b0)
2023-11-25T11:11:37.5645543+01:00  [DBG] "Use of unassigned local variable 'typedMessage'", "SourceFile(C:\git\stryker_bug\StrykerBug\ClassLibrary1\MessageHandlerBase.cs[534..546))" (c689eebb)
2023-11-25T11:11:37.5645741+01:00  [DBG] "Use of unassigned local variable 'typedMessage'", "SourceFile(C:\git\stryker_bug\StrykerBug\ClassLibrary1\MessageHandlerBase.cs[901..913))" (c689eebb)
2023-11-25T11:11:37.5645770+01:00  [DBG] "Use of unassigned local variable 'typedMessage'", "SourceFile(C:\git\stryker_bug\StrykerBug\ClassLibrary1\MessageHandlerBase.cs[1447..1459))" (c689eebb)
2023-11-25T11:11:37.5662516+01:00  [DBG] RollBacking mutations from C:\git\stryker_bug\StrykerBug\ClassLibrary1\MessageHandlerBase.cs. (e1a517c6)
2023-11-25T11:11:37.5688983+01:00  [DBG] Found mutant 1 of type '"Boolean"' controlled by '"IfInstrumentationEngine"'. (ea1665cd)
2023-11-25T11:11:37.5692410+01:00  [DBG] Found mutant 2 of type '"Equality"' controlled by '"IfInstrumentationEngine"'. (ea1665cd)
2023-11-25T11:11:37.5692650+01:00  [DBG] Found mutant 6 of type '"Statement"' controlled by '"IfInstrumentationEngine"'. (ea1665cd)
2023-11-25T11:11:37.6005261+01:00  [DBG] Trying compilation for the "second" time. (f2d91e8b)
2023-11-25T11:11:37.6111617+01:00  [DBG] Compilation failed (5193a3b0)
2023-11-25T11:11:37.6111866+01:00  [DBG] "Use of unassigned local variable 'typedMessage'", "SourceFile(C:\git\stryker_bug\StrykerBug\ClassLibrary1\MessageHandlerBase.cs[659..671))" (c689eebb)
2023-11-25T11:11:37.6112094+01:00  [DBG] RollBacking mutations from C:\git\stryker_bug\StrykerBug\ClassLibrary1\MessageHandlerBase.cs. (e1a517c6)
2023-11-25T11:11:37.6112309+01:00  [DBG] Found mutant 0 of type '"Block"' controlled by '"IfInstrumentationEngine"'. (ea1665cd)
2023-11-25T11:11:37.6115482+01:00  [DBG] Trying compilation for the "third" time. (f2d91e8b)
2023-11-25T11:11:37.6200377+01:00  [DBG] Compilation failed (5193a3b0)
2023-11-25T11:11:37.6200583+01:00  [DBG] "Use of unassigned local variable 'typedMessage'", "SourceFile(C:\git\stryker_bug\StrykerBug\ClassLibrary1\MessageHandlerBase.cs[600..612))" (c689eebb)
2023-11-25T11:11:37.6200682+01:00  [DBG] RollBacking mutations from C:\git\stryker_bug\StrykerBug\ClassLibrary1\MessageHandlerBase.cs. (e1a517c6)
2023-11-25T11:11:37.6211865+01:00  [DBG] Found mutant 5 of type '"String"' controlled by '"ConditionalInstrumentationEngine"'. (ea1665cd)
2023-11-25T11:11:37.6212068+01:00  [DBG] Found mutant 4 of type '"Statement"' controlled by '"IfInstrumentationEngine"'. (ea1665cd)
2023-11-25T11:11:37.6212102+01:00  [DBG] Found mutant 3 of type '"Block"' controlled by '"IfInstrumentationEngine"'. (ea1665cd)
2023-11-25T11:11:37.6217306+01:00  [DBG] Trying compilation for the "4th" time. (f2d91e8b)
2023-11-25T11:11:37.6274439+01:00  [DBG] Compilation failed (5193a3b0)
2023-11-25T11:11:37.6274726+01:00  [DBG] "Use of unassigned local variable 'typedMessage'", "SourceFile(C:\git\stryker_bug\StrykerBug\ClassLibrary1\MessageHandlerBase.cs[520..532))" (c689eebb)
2023-11-25T11:11:37.6274866+01:00  [DBG] RollBacking mutations from C:\git\stryker_bug\StrykerBug\ClassLibrary1\MessageHandlerBase.cs. (e1a517c6)
2023-11-25T11:11:37.6275752+01:00  [DBG] Found mutant 5 of type '"String"' controlled by '"ConditionalInstrumentationEngine"'. (ea1665cd)
2023-11-25T11:11:37.6275961+01:00  [DBG] Found mutant 4 of type '"Statement"' controlled by '"IfInstrumentationEngine"'. (ea1665cd)
2023-11-25T11:11:37.6372096+01:00  [WRN] Stryker.NET encountered a compile error in "C:\git\stryker_bug\StrykerBug\ClassLibrary1\MessageHandlerBase.cs" (at 12:15) with message: "Use of unassigned local variable 'typedMessage'" (Source code: "typedMessage") (6841b705)
2023-11-25T11:11:37.6395676+01:00  [WRN] Safe Mode! Stryker will try to continue by rolling back all mutations in method. This should not happen, please report this as an issue on github with the previous error message. (2b631064)
2023-11-25T11:11:37.6405136+01:00  [DBG] Trying compilation for the "5th" time. (f2d91e8b)
2023-11-25T11:11:37.8651851+01:00  [DBG] Compilation successful (17bf6fae)
2023-11-25T11:11:37.8772074+01:00  [DBG] Injected the mutated assembly file into "C:\git\stryker_bug\StrykerBug\TestProject1\bin\Debug\net7.0\ClassLibrary1.dll" (92a3e3c8)
2023-11-25T11:11:37.8809142+01:00  [INF] 7 mutants created (edec1dee)
2023-11-25T11:11:37.8825488+01:00  [INF] Capture mutant coverage using 'CoverageBasedTest' mode. (133c36d4)
2023-11-25T11:11:38.6336595+01:00  [DBG] Runner 5: Received testrun complete. (c3662fd2)
2023-11-25T11:11:38.6338206+01:00  [DBG] Runner 5: Test session finished. (a1ab700a)
2023-11-25T11:11:38.6416157+01:00  [DBG] Mutant 0 will be tested against (0) tests. (792f364b)
2023-11-25T11:11:38.6416360+01:00  [DBG] Mutant 1 will be tested against (0) tests. (ad551661)
2023-11-25T11:11:38.6416396+01:00  [DBG] Mutant 2 will be tested against (0) tests. (9d5f5ae1)
2023-11-25T11:11:38.6416413+01:00  [DBG] Mutant 3 will be tested against (0) tests. (5c15c088)
2023-11-25T11:11:38.6416444+01:00  [DBG] Mutant 4 will be tested against (0) tests. (f308a01d)
2023-11-25T11:11:38.6416462+01:00  [DBG] Mutant 5 will be tested against (0) tests. (51d5366c)
2023-11-25T11:11:38.6416509+01:00  [DBG] Mutant 6 will be tested against (0) tests. (4099d3ab)
2023-11-25T11:11:38.6612597+01:00  [INF] 7     mutants got status CompileError. Reason: "Mutant caused compile errors" (3d197dbf)
2023-11-25T11:11:38.6694124+01:00  [INF] 7     total mutants are skipped for the above mentioned reasons (a2f29bc6)
2023-11-25T11:11:38.6761604+01:00  [INF] 0     total mutants will be tested (f78142b2)
2023-11-25T11:11:38.6831995+01:00  [WRN] It looks like all mutants resulted in compile errors. Mutants sure are strange! (22ff4a10)
2023-11-25T11:11:39.8703623+01:00  [INF] Time Elapsed 00:00:20.3789340 (f4427d77)
2023-11-25T11:11:39.8752832+01:00  [INF] Stryker was unable to calculate a mutation score (24fbed4d)

Expected behavior Stryker should not enter a state of "This should not happen".

Desktop (please complete the following information):

Additional context Could reproduce a minimal example in a fresh solution. Just use the class below (plus an empty MsgBase class and an empty test project).

namespace ClassLibrary1;
internal abstract class MessageHandlerBase<T>
    where T : MsgBase
{
    public void Handle(MsgBase message)
    {
        if (!(message is T typedMessage))
        {
            throw new ArgumentException(
                $@"Expected parameter to be of type {nameof(T)} but it was {message.GetType().Name}",
                nameof(message)
            );
        }

        Handle(typedMessage);
    }

    protected abstract void Handle(T message);
}
dupdob commented 7 months ago

Thanks for reporting this. Short answer is: this is not a problem. Actually this message should be disregarded now. It dates from a time when we hoped to be able to prevent/restore most compilation errors. This turns out to be far more difficult than planned; we are currently designing an alternate approach.

dupdob commented 7 months ago

fixed by PR #2783