-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix #3382: Support compiler-generated throw-helper invocations in switch-expression implicit default-case. #3404
Open
siegfriedpammer
wants to merge
1
commit into
master
Choose a base branch
from
switch-expression-default-case
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
ICSharpCode.Decompiler/IL/Transforms/SwitchExpressionDefaultCaseTransform.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// Copyright (c) 2025 Siegfried Pammer | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy of this | ||
// software and associated documentation files (the "Software"), to deal in the Software | ||
// without restriction, including without limitation the rights to use, copy, modify, merge, | ||
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons | ||
// to whom the Software is furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all copies or | ||
// substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | ||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE | ||
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | ||
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
// DEALINGS IN THE SOFTWARE. | ||
|
||
#nullable enable | ||
|
||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq; | ||
|
||
using ICSharpCode.Decompiler.TypeSystem; | ||
using ICSharpCode.Decompiler.Util; | ||
|
||
namespace ICSharpCode.Decompiler.IL.Transforms | ||
{ | ||
// call ThrowInvalidOperationException(...) | ||
// leave IL_0000 (ldloc temp) | ||
// | ||
// -or- | ||
// | ||
// call ThrowSwitchExpressionException(...) | ||
// leave IL_0000 (ldloc temp) | ||
// | ||
// -to- | ||
// | ||
// throw(newobj SwitchExpressionException(...)) | ||
class SwitchExpressionDefaultCaseTransform : IILTransform | ||
{ | ||
void IILTransform.Run(ILFunction function, ILTransformContext context) | ||
{ | ||
IMethod? FindConstructor(string fullTypeName, params Type[] argumentTypes) | ||
{ | ||
IType exceptionType = context.TypeSystem.FindType(new FullTypeName(fullTypeName)); | ||
var types = argumentTypes.SelectArray(context.TypeSystem.FindType); | ||
|
||
foreach (var ctor in exceptionType.GetConstructors(m => !m.IsStatic && m.Parameters.Count == argumentTypes.Length)) | ||
{ | ||
bool found = true; | ||
foreach (var pair in ctor.Parameters.Select(p => p.Type).Zip(types)) | ||
{ | ||
if (!NormalizeTypeVisitor.IgnoreNullability.EquivalentTypes(pair.Item1, pair.Item2)) | ||
{ | ||
found = false; | ||
break; | ||
} | ||
} | ||
if (found) | ||
return ctor; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
IMethod[] exceptionCtorTable = new IMethod[2]; | ||
|
||
exceptionCtorTable[0] = FindConstructor("System.InvalidOperationException")!; | ||
exceptionCtorTable[1] = FindConstructor("System.Runtime.CompilerServices.SwitchExpressionException", typeof(object))!; | ||
|
||
if (exceptionCtorTable[0] == null && exceptionCtorTable[1] == null) | ||
return; | ||
|
||
bool MatchThrowHelperCall(ILInstruction inst, [NotNullWhen(true)] out IMethod? exceptionCtor, out ILInstruction? value) | ||
{ | ||
exceptionCtor = null; | ||
value = null; | ||
if (inst is not Call call) | ||
return false; | ||
if (call.Method.DeclaringType.FullName != "<PrivateImplementationDetails>") | ||
return false; | ||
switch (call.Arguments.Count) | ||
{ | ||
case 0: | ||
if (call.Method.Name != "ThrowInvalidOperationException") | ||
return false; | ||
exceptionCtor = exceptionCtorTable[0]; | ||
break; | ||
case 1: | ||
if (call.Method.Name != "ThrowSwitchExpressionException") | ||
return false; | ||
exceptionCtor = exceptionCtorTable[1]; | ||
value = call.Arguments[0]; | ||
break; | ||
default: | ||
return false; | ||
} | ||
return exceptionCtor != null; | ||
} | ||
|
||
foreach (var block in function.Descendants.OfType<Block>()) | ||
{ | ||
if (block.Parent is not BlockContainer container) | ||
continue; | ||
if (container.EntryPoint is not { IncomingEdgeCount: 1, Instructions: [SwitchInstruction inst] }) | ||
continue; | ||
if (block.Instructions.Count != 2 || block.IncomingEdgeCount != 1 || !block.FinalInstruction.MatchNop()) | ||
continue; | ||
var defaultSection = inst.GetDefaultSection(); | ||
if (defaultSection.Body != block && (defaultSection.Body is not Branch b || b.TargetBlock != block)) | ||
continue; | ||
if (!MatchThrowHelperCall(block.Instructions[0], out IMethod? exceptionCtor, out ILInstruction? value)) | ||
continue; | ||
if (block.Instructions[1] is not Leave { Value: LdLoc { Variable: { Kind: VariableKind.Local or VariableKind.StackSlot, LoadCount: 1, InitialValueIsInitialized: true } } }) | ||
{ | ||
continue; | ||
} | ||
context.Step("SwitchExpressionDefaultCaseTransform", block.Instructions[0]); | ||
var newObj = new NewObj(exceptionCtor); | ||
if (value != null) | ||
newObj.Arguments.Add(value); | ||
block.Instructions[0] = new Throw(newObj).WithILRange(block.Instructions[0]).WithILRange(block.Instructions[1]); | ||
block.Instructions.RemoveAt(1); | ||
defaultSection.IsCompilerGeneratedDefaultSection = true; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that doing this as a post-processing transform of switch detection is a problem. At this point there is no separate block container for
switch
(BlockContainerKind.Switch
is not yet set)... this transformation only happens later inLoopDetection
. So this transform would have to perform all the validation that happens in the otherswitch
detection transforms again.