Skip to content

Commit 90fd263

Browse files
committed
Callbacks of the Switch methods are flagged with InstantHandleAttribute
1 parent e46c41b commit 90fd263

112 files changed

Lines changed: 1340 additions & 1191 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<Copyright>(c) $([System.DateTime]::Now.Year), Pawel Gerr. All rights reserved.</Copyright>
5-
<VersionPrefix>9.6.2</VersionPrefix>
5+
<VersionPrefix>9.7.0</VersionPrefix>
66
<Authors>Pawel Gerr</Authors>
77
<GenerateDocumentationFile>true</GenerateDocumentationFile>
88
<PackageProjectUrl>https://github.com/PawelGerr/Thinktecture.Runtime.Extensions</PackageProjectUrl>

docs

Submodule docs updated from 3ad2f68 to e4218f9

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/AdHocUnions/AdHocUnionCodeGenerator.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ private void GenerateSwitchForAction(bool withState, bool isPartially)
449449
if (isPartially)
450450
{
451451
_sb.Append(@"
452-
global::System.Action<");
452+
[global::JetBrains.Annotations.InstantHandleAttribute] global::System.Action<");
453453

454454
if (withState)
455455
_sb.Append("TState, ");
@@ -465,7 +465,7 @@ private void GenerateSwitchForAction(bool withState, bool isPartially)
465465
_sb.Append(",");
466466

467467
_sb.Append(@"
468-
global::System.Action<");
468+
[global::JetBrains.Annotations.InstantHandleAttribute] global::System.Action<");
469469

470470
if (withState)
471471
_sb.Append("TState, ");
@@ -607,7 +607,7 @@ private void GenerateSwitchForFunc(bool withState, bool isPartially)
607607
if (isPartially)
608608
{
609609
_sb.Append(@"
610-
global::System.Func<");
610+
[global::JetBrains.Annotations.InstantHandleAttribute] global::System.Func<");
611611

612612
if (withState)
613613
_sb.Append("TState, ");
@@ -625,7 +625,7 @@ private void GenerateSwitchForFunc(bool withState, bool isPartially)
625625
_sb.Append(@"
626626
");
627627

628-
_sb.Append("global::System.Func<");
628+
_sb.Append("[global::JetBrains.Annotations.InstantHandleAttribute] global::System.Func<");
629629

630630
if (withState)
631631
_sb.Append("TState, ");

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/AdHocUnions/AdHocUnionSourceGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Thinktecture.CodeAnalysis.AdHocUnions;
66
public class AdHocUnionSourceGenerator : ThinktectureSourceGeneratorBase, IIncrementalGenerator
77
{
88
public AdHocUnionSourceGenerator()
9-
: base(15_000)
9+
: base(20_000)
1010
{
1111
}
1212

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
namespace Thinktecture.CodeAnalysis.Annotations;
2+
3+
[Generator]
4+
public class AnnotationsSourceGenerator : ThinktectureSourceGeneratorBase, IIncrementalGenerator
5+
{
6+
public AnnotationsSourceGenerator()
7+
: base(1)
8+
{
9+
}
10+
11+
public void Initialize(IncrementalGeneratorInitializationContext context)
12+
{
13+
var options = GetGeneratorOptions(context);
14+
15+
var annotationsCheck = context.MetadataReferencesProvider
16+
.Select((reference, _) => HasJetbrainsAnnotations(reference))
17+
.Collect();
18+
19+
context.RegisterSourceOutput(
20+
annotationsCheck.Combine(options)
21+
.SelectMany((tuple, _) => tuple.Left.Any(hasAnnotations => hasAnnotations) || !tuple.Right.GenerateJetbrainsAnnotations ? ImmutableArray<bool>.Empty : [false]),
22+
(ctx, _) => AddAnnotations(ctx));
23+
}
24+
25+
private bool HasJetbrainsAnnotations(MetadataReference reference)
26+
{
27+
try
28+
{
29+
return reference.GetModules().Any(module => module.Name == "JetBrains.Annotations.dll");
30+
}
31+
catch
32+
{
33+
return false;
34+
}
35+
}
36+
37+
private void AddAnnotations(SourceProductionContext ctx)
38+
{
39+
ctx.AddSource("Thinktecture.Annotations.g.cs",
40+
"""
41+
/* MIT License
42+
43+
Copyright (c) 2025 JetBrains http://www.jetbrains.com
44+
45+
Permission is hereby granted, free of charge, to any person obtaining a copy
46+
of this software and associated documentation files (the "Software"), to deal
47+
in the Software without restriction, including without limitation the rights
48+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
49+
copies of the Software, and to permit persons to whom the Software is
50+
furnished to do so, subject to the following conditions:
51+
52+
The above copyright notice and this permission notice shall be included in all
53+
copies or substantial portions of the Software.
54+
55+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
56+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
57+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
58+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
59+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
60+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
61+
SOFTWARE. */
62+
63+
using System;
64+
65+
// ReSharper disable once CheckNamespace
66+
namespace JetBrains.Annotations;
67+
68+
/// <summary>
69+
/// Tells the code analysis engine if the parameter is completely handled when the invoked method is on stack.
70+
/// If the parameter is of the delegate type - indicates that the delegate can only be invoked during the method
71+
/// execution. The delegate can be invoked zero or multiple times, but not stored to some field and invoked later,
72+
/// when the containing method is no longer on the execution stack.
73+
/// If the parameter is of the enumerable type - indicates that it is enumerated while the method is executed.
74+
/// If <see cref="RequireAwait"/> is true - the attribute will only take effect if the method invocation
75+
/// is located under the <c>await</c> expression.
76+
/// </summary>
77+
[AttributeUsage(AttributeTargets.Parameter)]
78+
internal sealed class InstantHandleAttribute : Attribute
79+
{
80+
/// <summary>
81+
/// Requires the method invocation to be used under the <c>await</c> expression for this attribute to take effect.
82+
/// Can be used for delegate/enumerable parameters of <c>async</c> methods.
83+
/// </summary>
84+
public bool RequireAwait { get; set; }
85+
}
86+
"""
87+
);
88+
}
89+
}

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/Constants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public static class Methods
3636
public static class Configuration
3737
{
3838
public const string COUNTER = "build_property.ThinktectureRuntimeExtensions_SourceGenerator_Counter";
39+
public const string GENERATE_JETBRAINS_ANNOTATIONS = "build_property.ThinktectureRuntimeExtensions_SourceGenerator_GenerateJetBrainsAnnotations";
3940
public const string LOG_FILE_PATH = "build_property.ThinktectureRuntimeExtensions_SourceGenerator_LogFilePath";
4041
public const string LOG_FILE_PATH_UNIQUE = "build_property.ThinktectureRuntimeExtensions_SourceGenerator_LogFilePathMustBeUnique";
4142
public const string LOG_LEVEL = "build_property.ThinktectureRuntimeExtensions_SourceGenerator_LogLevel";

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/GeneratorOptions.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ namespace Thinktecture.CodeAnalysis;
33
public sealed class GeneratorOptions : IEquatable<GeneratorOptions>
44
{
55
public bool CounterEnabled { get; }
6+
public bool GenerateJetbrainsAnnotations { get; }
67
public LoggingOptions? Logging { get; }
78

8-
public GeneratorOptions(
9-
bool counterEnabled,
10-
LoggingOptions? logging)
9+
public GeneratorOptions(bool counterEnabled,
10+
bool generateJetbrainsAnnotations,
11+
LoggingOptions? logging)
1112
{
1213
CounterEnabled = counterEnabled;
14+
GenerateJetbrainsAnnotations = generateJetbrainsAnnotations;
1315
Logging = logging;
1416
}
1517

@@ -22,6 +24,7 @@ public bool Equals(GeneratorOptions? other)
2224
return true;
2325

2426
return CounterEnabled == other.CounterEnabled
27+
&& GenerateJetbrainsAnnotations == other.GenerateJetbrainsAnnotations
2528
&& Logging.EqualsTo(other.Logging);
2629
}
2730

@@ -34,7 +37,9 @@ public override int GetHashCode()
3437
{
3538
unchecked
3639
{
37-
return (CounterEnabled.GetHashCode() * 397) ^ Logging.GetHashCode();
40+
return (CounterEnabled.GetHashCode() * 397)
41+
^ (GenerateJetbrainsAnnotations.GetHashCode() * 397)
42+
^ Logging.GetHashCode();
3843
}
3944
}
4045
}

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/RegularUnions/RegularUnionCodeGenerator.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ private void GenerateSwitchForAction(bool withState, bool isPartially, IReadOnly
266266
if (isPartially)
267267
{
268268
_sb.Append(@"
269-
global::System.Action<");
269+
[global::JetBrains.Annotations.InstantHandleAttribute] global::System.Action<");
270270

271271
if (withState)
272272
_sb.Append("TState, ");
@@ -282,7 +282,7 @@ private void GenerateSwitchForAction(bool withState, bool isPartially, IReadOnly
282282
_sb.Append(",");
283283

284284
_sb.Append(@"
285-
global::System.Action<");
285+
[global::JetBrains.Annotations.InstantHandleAttribute] global::System.Action<");
286286

287287
if (withState)
288288
_sb.Append("TState, ");
@@ -425,7 +425,7 @@ private void GenerateSwitchForFunc(bool withState, bool isPartially, IReadOnlyLi
425425
if (isPartially)
426426
{
427427
_sb.Append(@"
428-
global::System.Func<");
428+
[global::JetBrains.Annotations.InstantHandleAttribute] global::System.Func<");
429429

430430
if (withState)
431431
_sb.Append("TState, ");
@@ -441,7 +441,7 @@ private void GenerateSwitchForFunc(bool withState, bool isPartially, IReadOnlyLi
441441
_sb.Append(",");
442442

443443
_sb.Append(@"
444-
global::System.Func<");
444+
[global::JetBrains.Annotations.InstantHandleAttribute] global::System.Func<");
445445

446446
if (withState)
447447
_sb.Append("TState, ");

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/RegularUnions/RegularUnionSourceGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Thinktecture.CodeAnalysis.RegularUnions;
66
public class RegularUnionSourceGenerator : ThinktectureSourceGeneratorBase, IIncrementalGenerator
77
{
88
public RegularUnionSourceGenerator()
9-
: base(20_000)
9+
: base(25_000)
1010
{
1111
}
1212

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/SmartEnums/SmartEnumCodeGenerator.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ private void GenerateSwitchForAction(bool withState, bool isPartially)
424424
if (isPartially)
425425
{
426426
_sb.Append(@"
427-
global::System.Action<");
427+
[global::JetBrains.Annotations.InstantHandleAttribute] global::System.Action<");
428428

429429
if (withState)
430430
_sb.Append("TState, ");
@@ -438,7 +438,7 @@ private void GenerateSwitchForAction(bool withState, bool isPartially)
438438
_sb.Append(",");
439439

440440
_sb.Append(@"
441-
global::System.Action");
441+
[global::JetBrains.Annotations.InstantHandleAttribute] global::System.Action");
442442

443443
if (withState)
444444
_sb.Append("<TState>");
@@ -568,7 +568,7 @@ private void GenerateSwitchForFunc(bool withState, bool isPartially)
568568
if (isPartially)
569569
{
570570
_sb.Append(@"
571-
global::System.Func<");
571+
[global::JetBrains.Annotations.InstantHandleAttribute] global::System.Func<");
572572

573573
if (withState)
574574
_sb.Append("TState, ");
@@ -584,7 +584,7 @@ private void GenerateSwitchForFunc(bool withState, bool isPartially)
584584
_sb.Append(@"
585585
");
586586

587-
_sb.Append("global::System.Func<");
587+
_sb.Append("[global::JetBrains.Annotations.InstantHandleAttribute] global::System.Func<");
588588

589589
if (withState)
590590
_sb.Append("TState, ");

0 commit comments

Comments
 (0)