`ReduceNestingTransform` increases nesting for long `if` chain

Aperta
#3,654 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
52/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
csharp

Direzione di ricerca

Inizia da ICSharpCode.Decompiler/IL/Transforms/ReduceNestingTransform.cs, in particolare da ImproveILOrdering alle righe indicate, e riproduci la trasformazione usando l’esempio allegato di System.Private.CoreLib. Confronta l’ILAst prima e dopo ReduceNestingTransform; il lavoro è completato quando la lunga catena di condizioni non viene convertita nella struttura if profondamente annidata mostrata nel report.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Bug Decompiler
Input code

System.Private.CoreLib from .NET 10.0.2 runtime.

Type: Microsoft.Win32.OAVariantLib
Method: ToOAVariant

System.Private.CoreLib.zip

Erroneous output

ILSpy produces an insane looking if chain for this method.

private static ComVariant ToOAVariant(object input)
{
	if (!(input is string value))
	{
		if (!(input is DateTime value2))
		{
			if (!(input is bool value3))
			{
				if (!(input is decimal value4))
				{
					if (!(input is sbyte value5))
					{
						if (!(input is byte value6))
						{
							if (!(input is short value7))
							{
								if (!(input is ushort value8))
								{
									if (!(input is int value9))
									{
										if (!(input is uint value10))
										{
											if (!(input is long value11))
											{
												if (!(input is ulong value12))
												{
													if (!(input is float value13))
													{
														if (!(input is double value14))
														{
															if (input != null)
															{
																if (!(input is Missing))
																{
																	if (input is DBNull)
																	{
																		return ComVariant.Null;
																	}
																	return Variant.GetIUnknownOrIDispatchFromObject(input);
																}
																throw new NotSupportedException(SR.NotSupported_ChangeType);
															}
															return default(ComVariant);
														}
														return ComVariant.Create(value14);
													}
													return ComVariant.Create(value13);
												}
												return ComVariant.Create(value12);
											}
											return ComVariant.Create(value11);
										}
										return ComVariant.Create(value10);
									}
									return ComVariant.Create(value9);
								}
								return ComVariant.Create(value8);
							}
							return ComVariant.Create(value7);
						}
						return ComVariant.Create(value6);
					}
					return ComVariant.Create(value5);
				}
				return ComVariant.Create(value4);
			}
			return ComVariant.Create(value3);
		}
		return ComVariant.Create(value2);
	}
	return ComVariant.Create(value);
}

The original code, which uses a switch on types that ILSpy does not support, can be found here:

https://github.com/dotnet/runtime/blob/8fafcb3251a3d2ccde36612a2eab47e9ae8ebe2b/src/coreclr/System.Private.CoreLib/src/Microsoft/Win32/OAVariantLib.cs#L132-L155

A better decompialtion of this code without the switch on type would be:

private static ComVariant ToOAVariant(object input)
{
	if (input is string value)
	{
		return ComVariant.Create(value);
	}
	if (input is DateTime value2)
	{
		return ComVariant.Create(value2);
	}
	if (input is bool value3)
	{
		return ComVariant.Create(value3);
	}
	if (input is decimal value4)
	{
		return ComVariant.Create(value4);
	}
	if (input is sbyte value5)
	{
		return ComVariant.Create(value5);
	}
	if (input is byte value6)
	{
		return ComVariant.Create(value6);
	}
	if (input is short value7)
	{
		return ComVariant.Create(value7);
	}
	if (input is ushort value8)
	{
		return ComVariant.Create(value8);
	}
	if (input is int value9)
	{
		return ComVariant.Create(value9);
	}
	if (input is uint value10)
	{
		return ComVariant.Create(value10);
	}
	if (input is long value11)
	{
		return ComVariant.Create(value11);
	}
	if (input is ulong value12)
	{
		return ComVariant.Create(value12);
	}
	if (input is float value13)
	{
		return ComVariant.Create(value13);
	}
	if (input is double value14)
	{
		return ComVariant.Create(value14);
	}
	if (input == null)
	{
		return default(ComVariant);
	}
	if (input is Missing)
	{
		throw new NotSupportedException(SR.NotSupported_ChangeType);
	}
	if (input is DBNull)
	{
		return ComVariant.Null;
	}
	return Variant.GetIUnknownOrIDispatchFromObject(input);
}

I've debugged this using the ILAst debug steps feature and up until ReduceNestingTransform gets ran, the ILAst looks as follows:

BlockContainer {
		Block IL_0000 (incoming: 1) {
			if (match.notnull.type[System.String] (V_0 = ldloc input)) Block IL_013c {
				leave IL_0000 (call Create(ldloc V_0))
			}
			if (match.notnull.type[System.DateTime] (V_1 = ldloc input)) Block IL_014f {
				leave IL_0000 (call Create(ldloc V_1))
			}
			if (match.notnull.type[System.Boolean] (V_2 = ldloc input)) Block IL_015c {
				leave IL_0000 (call Create(ldloc V_2))
			}
			if (match.notnull.type[System.Decimal] (V_3 = ldloc input)) Block IL_0169 {
				leave IL_0000 (call Create(ldloc V_3))
			}
			if (match.notnull.type[System.SByte] (V_4 = ldloc input)) Block IL_0177 {
				leave IL_0000 (call Create(ldloc V_4))
			}
			if (match.notnull.type[System.Byte] (V_5 = ldloc input)) Block IL_0185 {
				leave IL_0000 (call Create(ldloc V_5))
			}
			if (match.notnull.type[System.Int16] (V_6 = ldloc input)) Block IL_0193 {
				leave IL_0000 (call Create(ldloc V_6))
			}
			if (match.notnull.type[System.UInt16] (V_7 = ldloc input)) Block IL_019e {
				leave IL_0000 (call Create(ldloc V_7))
			}
			if (match.notnull.type[System.Int32] (V_8 = ldloc input)) Block IL_01a9 {
				leave IL_0000 (call Create(ldloc V_8))
			}
			if (match.notnull.type[System.UInt32] (V_9 = ldloc input)) Block IL_01b4 {
				leave IL_0000 (call Create(ldloc V_9))
			}
			if (match.notnull.type[System.Int64] (V_10 = ldloc input)) Block IL_01bf {
				leave IL_0000 (call Create(ldloc V_10))
			}
			if (match.notnull.type[System.UInt64] (V_11 = ldloc input)) Block IL_01ca {
				leave IL_0000 (call Create(ldloc V_11))
			}
			if (match.notnull.type[System.Single] (V_12 = ldloc input)) Block IL_01d5 {
				leave IL_0000 (call Create(ldloc V_12))
			}
			if (match.notnull.type[System.Double] (V_13 = ldloc input)) Block IL_01e0 {
				leave IL_0000 (call Create(ldloc V_13))
			}
			if (comp.o(ldloc input == ldnull)) Block IL_01e4 {
				leave IL_0000 (default.value System.Runtime.InteropServices.Marshalling.ComVariant)
			}
			if (comp.o(isinst System.Reflection.Missing(ldloc input) != ldnull)) Block IL_01f2 {
				throw(newobj NotSupportedException..ctor(call get_NotSupported_ChangeType()))
			}
			if (comp.o(isinst System.DBNull(ldloc input) != ldnull)) Block IL_01fd {
				leave IL_0000 (call get_Null())
			}
			leave IL_0000 (call GetIUnknownOrIDispatchFromObject(ldloc input))
		}
	}

After ReduceNestingTransform the ILAst contains the massively nested if statements found in the final C# code.

BlockContainer {
		Block IL_0000 (incoming: 1) {
			if (logic.not(match.notnull.type[System.String] (V_0 = ldloc input))) Block IL_0013 {
				if (logic.not(match.notnull.type[System.DateTime] (V_1 = ldloc input))) Block IL_0027 {
					if (logic.not(match.notnull.type[System.Boolean] (V_2 = ldloc input))) Block IL_003b {
						if (logic.not(match.notnull.type[System.Decimal] (V_3 = ldloc input))) Block IL_004f {
							if (logic.not(match.notnull.type[System.SByte] (V_4 = ldloc input))) Block IL_0064 {
								if (logic.not(match.notnull.type[System.Byte] (V_5 = ldloc input))) Block IL_0079 {
									if (logic.not(match.notnull.type[System.Int16] (V_6 = ldloc input))) Block IL_008e {
										if (logic.not(match.notnull.type[System.UInt16] (V_7 = ldloc input))) Block IL_00a3 {
											if (logic.not(match.notnull.type[System.Int32] (V_8 = ldloc input))) Block IL_00b8 {
												if (logic.not(match.notnull.type[System.UInt32] (V_9 = ldloc input))) Block IL_00cd {
													if (logic.not(match.notnull.type[System.Int64] (V_10 = ldloc input))) Block IL_00e2 {
														if (logic.not(match.notnull.type[System.UInt64] (V_11 = ldloc input))) Block IL_00f7 {
															if (logic.not(match.notnull.type[System.Single] (V_12 = ldloc input))) Block IL_010c {
																if (logic.not(match.notnull.type[System.Double] (V_13 = ldloc input))) Block IL_011c {
																	if (comp.o(ldloc input != ldnull)) Block IL_0127 {
																		if (comp.o(isinst System.Reflection.Missing(ldloc input) == ldnull)) Block IL_0132 {
																			if (comp.o(isinst System.DBNull(ldloc input) != ldnull)) Block IL_01fd {
																				leave IL_0000 (call get_Null())
																			}
																			leave IL_0000 (call GetIUnknownOrIDispatchFromObject(ldloc input))
																		}
																		throw(newobj NotSupportedException..ctor(call get_NotSupported_ChangeType()))
																	}
																	leave IL_0000 (default.value System.Runtime.InteropServices.Marshalling.ComVariant)
																}
																leave IL_0000 (call Create(ldloc V_13))
															}
															leave IL_0000 (call Create(ldloc V_12))
														}
														leave IL_0000 (call Create(ldloc V_11))
													}
													leave IL_0000 (call Create(ldloc V_10))
												}
												leave IL_0000 (call Create(ldloc V_9))
											}
											leave IL_0000 (call Create(ldloc V_8))
										}
										leave IL_0000 (call Create(ldloc V_7))
									}
									leave IL_0000 (call Create(ldloc V_6))
								}
								leave IL_0000 (call Create(ldloc V_5))
							}
							leave IL_0000 (call Create(ldloc V_4))
						}
						leave IL_0000 (call Create(ldloc V_3))
					}
					leave IL_0000 (call Create(ldloc V_2))
				}
				leave IL_0000 (call Create(ldloc V_1))
			}
			leave IL_0000 (call Create(ldloc V_0))
		}

	}

After some testing, I have pinpointed this to the ImproveILOrdering step in ReduceNestingTransform.
https://github.com/icsharpcode/ILSpy/blob/4a29de5a4e95630982730da0dd8081ced4a58eeb/ICSharpCode.Decompiler/IL/Transforms/ReduceNestingTransform.cs#L173-L202

Details
  • Product in use: e.g. ILSpy commit 4a29de5a4e95630982730da0dd8081ced4a58eeb
Lingua principale
C#
Stelle
27.1k
Fork
3.8k
Merge medio
1g 23h
PR unite (30g)
44

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di icsharpcode/ILSpy

Tutte le issue di icsharpcode/ILSpy

Issue simili

Altre issue su C#

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.