Skip to content

Commit 1d67ba7

Browse files
author
Julien Couvreur
authored
Extensions: throw NotSupportedException instead of null (#80420)
1 parent b004967 commit 1d67ba7

File tree

4 files changed

+566
-417
lines changed

4 files changed

+566
-417
lines changed

src/Compilers/CSharp/Portable/Compiler/MethodCompiler.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,11 +1418,19 @@ private void EmitSkeletonMethodInExtension(MethodSymbol methodSymbol)
14181418
Debug.Assert(_moduleBeingBuiltOpt != null);
14191419

14201420
ILBuilder builder = new ILBuilder(_moduleBeingBuiltOpt, new LocalSlotManager(slotAllocator: null), _diagnostics.DiagnosticBag, OptimizationLevel.Release, areLocalsZeroed: false);
1421+
CSharpSyntaxNode syntax = methodSymbol.GetNonNullSyntaxNode();
1422+
var ctor = (MethodSymbol)Binder.GetWellKnownTypeMember(_compilation, WellKnownMember.System_NotSupportedException__ctor, _diagnostics, syntax: syntax, isOptional: false);
1423+
1424+
if (ctor is not null)
1425+
{
1426+
builder.EmitOpCode(System.Reflection.Metadata.ILOpCode.Newobj, stackAdjustment: 1);
1427+
builder.EmitToken(_moduleBeingBuiltOpt.Translate(ctor, syntax, _diagnostics.DiagnosticBag, optArgList: null, needDeclaration: false), syntax, 0);
1428+
}
1429+
else
1430+
{
1431+
builder.EmitOpCode(System.Reflection.Metadata.ILOpCode.Ldnull);
1432+
}
14211433

1422-
// Emit methods in extensions as skeletons:
1423-
// => throw null;
1424-
// Tracked by https://github.com/dotnet/roslyn/issues/78827 : MQ, Should throw NotSupportedException instead
1425-
builder.EmitOpCode(System.Reflection.Metadata.ILOpCode.Ldnull);
14261434
builder.EmitThrow(isRethrow: false);
14271435
builder.Realize();
14281436

src/Compilers/CSharp/Test/Emit3/Semantics/ExtensionOperatorsTests.cs

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3667,7 +3667,7 @@ class C2 ''
36673667
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
36683668
01 00 00 00
36693669
)
3670-
// Method begins at RVA 0x2072
3670+
// Method begins at RVA 0x2076
36713671
// Code size 1 (0x1)
36723672
.maxstack 8
36733673
IL_0000: ret
@@ -3685,10 +3685,10 @@ 33 33 46 39 34 36 30 42 36 46 31 38 36 45 45 43
36853685
32 31 43 45 33 42 30 00 00
36863686
)
36873687
// Method begins at RVA 0x206f
3688-
// Code size 2 (0x2)
3688+
// Code size 6 (0x6)
36893689
.maxstack 8
3690-
IL_0000: ldnull
3691-
IL_0001: throw
3690+
IL_0000: newobj instance void [mscorlib]System.NotSupportedException::.ctor()
3691+
IL_0005: throw
36923692
} // end of method '<G>$3D0C2090833F9460B6F186EEC21CE3B0'::op_UnaryNegation
36933693
} // end of class <G>$3D0C2090833F9460B6F186EEC21CE3B0
36943694
// Methods
@@ -3755,7 +3755,7 @@ class C2 ''
37553755
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
37563756
01 00 00 00
37573757
)
3758-
// Method begins at RVA 0x2072
3758+
// Method begins at RVA 0x2076
37593759
// Code size 1 (0x1)
37603760
.maxstack 8
37613761
IL_0000: ret
@@ -3773,10 +3773,10 @@ 33 33 46 39 34 36 30 42 36 46 31 38 36 45 45 43
37733773
32 31 43 45 33 42 30 00 00
37743774
)
37753775
// Method begins at RVA 0x206f
3776-
// Code size 2 (0x2)
3776+
// Code size 6 (0x6)
37773777
.maxstack 8
3778-
IL_0000: ldnull
3779-
IL_0001: throw
3778+
IL_0000: newobj instance void [mscorlib]System.NotSupportedException::.ctor()
3779+
IL_0005: throw
37803780
} // end of method '<G>$3D0C2090833F9460B6F186EEC21CE3B0'::op_UnaryNegation
37813781
} // end of class <G>$3D0C2090833F9460B6F186EEC21CE3B0
37823782
// Methods
@@ -3835,7 +3835,7 @@ class C2 ''
38353835
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
38363836
01 00 00 00
38373837
)
3838-
// Method begins at RVA 0x2072
3838+
// Method begins at RVA 0x2076
38393839
// Code size 1 (0x1)
38403840
.maxstack 8
38413841
IL_0000: ret
@@ -3853,10 +3853,10 @@ 33 33 46 39 34 36 30 42 36 46 31 38 36 45 45 43
38533853
32 31 43 45 33 42 30 00 00
38543854
)
38553855
// Method begins at RVA 0x206f
3856-
// Code size 2 (0x2)
3856+
// Code size 6 (0x6)
38573857
.maxstack 8
3858-
IL_0000: ldnull
3859-
IL_0001: throw
3858+
IL_0000: newobj instance void [mscorlib]System.NotSupportedException::.ctor()
3859+
IL_0005: throw
38603860
} // end of method '<G>$3D0C2090833F9460B6F186EEC21CE3B0'::op_UnaryNegation
38613861
} // end of class <G>$3D0C2090833F9460B6F186EEC21CE3B0
38623862
// Methods
@@ -9929,7 +9929,7 @@ class C2 x
99299929
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
99309930
01 00 00 00
99319931
)
9932-
// Method begins at RVA 0x209a
9932+
// Method begins at RVA 0x209e
99339933
// Code size 1 (0x1)
99349934
.maxstack 8
99359935
IL_0000: ret
@@ -9950,10 +9950,10 @@ 38 37 42 36 45 42 42 36 35 37 36 46 43 35 37 33
99509950
42 31 34 35 39 36 39 00 00
99519951
)
99529952
// Method begins at RVA 0x2097
9953-
// Code size 2 (0x2)
9953+
// Code size 6 (0x6)
99549954
.maxstack 8
9955-
IL_0000: ldnull
9956-
IL_0001: throw
9955+
IL_0000: newobj instance void [mscorlib]System.NotSupportedException::.ctor()
9956+
IL_0005: throw
99579957
} // end of method '<G>$3D0C2090833F9460B6F186EEC21CE3B0'::op_DecrementAssignment
99589958
} // end of class <G>$3D0C2090833F9460B6F186EEC21CE3B0
99599959
// Methods
@@ -10022,7 +10022,7 @@ class C2 x
1002210022
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
1002310023
01 00 00 00
1002410024
)
10025-
// Method begins at RVA 0x209a
10025+
// Method begins at RVA 0x209e
1002610026
// Code size 1 (0x1)
1002710027
.maxstack 8
1002810028
IL_0000: ret
@@ -10043,10 +10043,10 @@ 38 37 42 36 45 42 42 36 35 37 36 46 43 35 37 33
1004310043
42 31 34 35 39 36 39 00 00
1004410044
)
1004510045
// Method begins at RVA 0x2097
10046-
// Code size 2 (0x2)
10046+
// Code size 6 (0x6)
1004710047
.maxstack 8
10048-
IL_0000: ldnull
10049-
IL_0001: throw
10048+
IL_0000: newobj instance void [mscorlib]System.NotSupportedException::.ctor()
10049+
IL_0005: throw
1005010050
} // end of method '<G>$3D0C2090833F9460B6F186EEC21CE3B0'::op_DecrementAssignment
1005110051
} // end of class <G>$3D0C2090833F9460B6F186EEC21CE3B0
1005210052
// Methods
@@ -10107,7 +10107,7 @@ class C2 x
1010710107
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
1010810108
01 00 00 00
1010910109
)
10110-
// Method begins at RVA 0x209a
10110+
// Method begins at RVA 0x209e
1011110111
// Code size 1 (0x1)
1011210112
.maxstack 8
1011310113
IL_0000: ret
@@ -10128,10 +10128,10 @@ 38 37 42 36 45 42 42 36 35 37 36 46 43 35 37 33
1012810128
42 31 34 35 39 36 39 00 00
1012910129
)
1013010130
// Method begins at RVA 0x2097
10131-
// Code size 2 (0x2)
10131+
// Code size 6 (0x6)
1013210132
.maxstack 8
10133-
IL_0000: ldnull
10134-
IL_0001: throw
10133+
IL_0000: newobj instance void [mscorlib]System.NotSupportedException::.ctor()
10134+
IL_0005: throw
1013510135
} // end of method '<G>$3D0C2090833F9460B6F186EEC21CE3B0'::op_DecrementAssignment
1013610136
} // end of class <G>$3D0C2090833F9460B6F186EEC21CE3B0
1013710137
// Methods
@@ -18487,7 +18487,7 @@ class C2 ''
1848718487
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
1848818488
01 00 00 00
1848918489
)
18490-
// Method begins at RVA 0x2072
18490+
// Method begins at RVA 0x2076
1849118491
// Code size 1 (0x1)
1849218492
.maxstack 8
1849318493
IL_0000: ret
@@ -18506,10 +18506,10 @@ 33 33 46 39 34 36 30 42 36 46 31 38 36 45 45 43
1850618506
32 31 43 45 33 42 30 00 00
1850718507
)
1850818508
// Method begins at RVA 0x206f
18509-
// Code size 2 (0x2)
18509+
// Code size 6 (0x6)
1851018510
.maxstack 8
18511-
IL_0000: ldnull
18512-
IL_0001: throw
18511+
IL_0000: newobj instance void [mscorlib]System.NotSupportedException::.ctor()
18512+
IL_0005: throw
1851318513
} // end of method '<G>$3D0C2090833F9460B6F186EEC21CE3B0'::op_Subtraction
1851418514
} // end of class <G>$3D0C2090833F9460B6F186EEC21CE3B0
1851518515
// Methods
@@ -18577,7 +18577,7 @@ class C2 ''
1857718577
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
1857818578
01 00 00 00
1857918579
)
18580-
// Method begins at RVA 0x2072
18580+
// Method begins at RVA 0x2076
1858118581
// Code size 1 (0x1)
1858218582
.maxstack 8
1858318583
IL_0000: ret
@@ -18596,10 +18596,10 @@ 33 33 46 39 34 36 30 42 36 46 31 38 36 45 45 43
1859618596
32 31 43 45 33 42 30 00 00
1859718597
)
1859818598
// Method begins at RVA 0x206f
18599-
// Code size 2 (0x2)
18599+
// Code size 6 (0x6)
1860018600
.maxstack 8
18601-
IL_0000: ldnull
18602-
IL_0001: throw
18601+
IL_0000: newobj instance void [mscorlib]System.NotSupportedException::.ctor()
18602+
IL_0005: throw
1860318603
} // end of method '<G>$3D0C2090833F9460B6F186EEC21CE3B0'::op_Subtraction
1860418604
} // end of class <G>$3D0C2090833F9460B6F186EEC21CE3B0
1860518605
// Methods
@@ -18659,7 +18659,7 @@ class C2 ''
1865918659
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
1866018660
01 00 00 00
1866118661
)
18662-
// Method begins at RVA 0x2072
18662+
// Method begins at RVA 0x2076
1866318663
// Code size 1 (0x1)
1866418664
.maxstack 8
1866518665
IL_0000: ret
@@ -18678,10 +18678,10 @@ 33 33 46 39 34 36 30 42 36 46 31 38 36 45 45 43
1867818678
32 31 43 45 33 42 30 00 00
1867918679
)
1868018680
// Method begins at RVA 0x206f
18681-
// Code size 2 (0x2)
18681+
// Code size 6 (0x6)
1868218682
.maxstack 8
18683-
IL_0000: ldnull
18684-
IL_0001: throw
18683+
IL_0000: newobj instance void [mscorlib]System.NotSupportedException::.ctor()
18684+
IL_0005: throw
1868518685
} // end of method '<G>$3D0C2090833F9460B6F186EEC21CE3B0'::op_Subtraction
1868618686
} // end of class <G>$3D0C2090833F9460B6F186EEC21CE3B0
1868718687
// Methods
@@ -26613,7 +26613,7 @@ class C2 x
2661326613
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
2661426614
01 00 00 00
2661526615
)
26616-
// Method begins at RVA 0x209a
26616+
// Method begins at RVA 0x209e
2661726617
// Code size 1 (0x1)
2661826618
.maxstack 8
2661926619
IL_0000: ret
@@ -26636,10 +26636,10 @@ 38 37 42 36 45 42 42 36 35 37 36 46 43 35 37 33
2663626636
42 31 34 35 39 36 39 00 00
2663726637
)
2663826638
// Method begins at RVA 0x2097
26639-
// Code size 2 (0x2)
26639+
// Code size 6 (0x6)
2664026640
.maxstack 8
26641-
IL_0000: ldnull
26642-
IL_0001: throw
26641+
IL_0000: newobj instance void [mscorlib]System.NotSupportedException::.ctor()
26642+
IL_0005: throw
2664326643
} // end of method '<G>$3D0C2090833F9460B6F186EEC21CE3B0'::op_SubtractionAssignment
2664426644
} // end of class <G>$3D0C2090833F9460B6F186EEC21CE3B0
2664526645
// Methods
@@ -26709,7 +26709,7 @@ class C2 x
2670926709
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
2671026710
01 00 00 00
2671126711
)
26712-
// Method begins at RVA 0x209a
26712+
// Method begins at RVA 0x209e
2671326713
// Code size 1 (0x1)
2671426714
.maxstack 8
2671526715
IL_0000: ret
@@ -26732,10 +26732,10 @@ 38 37 42 36 45 42 42 36 35 37 36 46 43 35 37 33
2673226732
42 31 34 35 39 36 39 00 00
2673326733
)
2673426734
// Method begins at RVA 0x2097
26735-
// Code size 2 (0x2)
26735+
// Code size 6 (0x6)
2673626736
.maxstack 8
26737-
IL_0000: ldnull
26738-
IL_0001: throw
26737+
IL_0000: newobj instance void [mscorlib]System.NotSupportedException::.ctor()
26738+
IL_0005: throw
2673926739
} // end of method '<G>$3D0C2090833F9460B6F186EEC21CE3B0'::op_SubtractionAssignment
2674026740
} // end of class <G>$3D0C2090833F9460B6F186EEC21CE3B0
2674126741
// Methods
@@ -26797,7 +26797,7 @@ class C2 x
2679726797
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
2679826798
01 00 00 00
2679926799
)
26800-
// Method begins at RVA 0x209a
26800+
// Method begins at RVA 0x209e
2680126801
// Code size 1 (0x1)
2680226802
.maxstack 8
2680326803
IL_0000: ret
@@ -26820,10 +26820,10 @@ 38 37 42 36 45 42 42 36 35 37 36 46 43 35 37 33
2682026820
42 31 34 35 39 36 39 00 00
2682126821
)
2682226822
// Method begins at RVA 0x2097
26823-
// Code size 2 (0x2)
26823+
// Code size 6 (0x6)
2682426824
.maxstack 8
26825-
IL_0000: ldnull
26826-
IL_0001: throw
26825+
IL_0000: newobj instance void [mscorlib]System.NotSupportedException::.ctor()
26826+
IL_0005: throw
2682726827
} // end of method '<G>$3D0C2090833F9460B6F186EEC21CE3B0'::op_SubtractionAssignment
2682826828
} // end of class <G>$3D0C2090833F9460B6F186EEC21CE3B0
2682926829
// Methods

0 commit comments

Comments
 (0)