Skip to content

Commit 6b0dc9f

Browse files
committed
Remove init and usage of InterceptableAttribute. Update docs and tests.
1 parent 88bd1a0 commit 6b0dc9f

File tree

4 files changed

+41
-253
lines changed

4 files changed

+41
-253
lines changed

docs/features/interceptors.md

Lines changed: 4 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ c.InterceptableMethod(1); // prints "interceptable 1"
1919
2020
class C
2121
{
22-
[Interceptable]
2322
public void InterceptableMethod(int param)
2423
{
2524
Console.WriteLine($"interceptable {param}");
@@ -47,49 +46,6 @@ static class D
4746
## Detailed design
4847
[design]: #detailed-design
4948

50-
### InterceptableAttribute
51-
52-
A method can indicate that its calls can be *intercepted* by including `[Interceptable]` on its declaration.
53-
54-
PROTOTYPE(ic): For now, if a call is intercepted to a method which lacks this attribute, a warning is reported, and interception still occurs. This may be changed to an error in the future.
55-
56-
```cs
57-
namespace System.Runtime.CompilerServices
58-
{
59-
[AttributeUsage(AttributeTargets.Method)]
60-
public sealed class InterceptableAttribute : Attribute { }
61-
}
62-
```
63-
64-
#### PROTOTYPE(ic): Use an assembly attribute instead?
65-
The main reason we want something like `[Interceptable]` is so that users can better reason about *which calls might be getting intercepted or not*. We don't think there's a meaningful *security* concern which is addressed by `[Interceptable]`--if something untrusted has the ability to add source files to your build, taking away the ability for them to intercept certain calls doesn't really make a difference.
66-
67-
It's been suggested that `[Interceptable]` on method declarations is the wrong point of control. Some SG authors will have good reason to want to intercept a call to a method from a library they don't own--for example, EF wanting to intercept calls to IQueryable extensions. It shouldn't be necessary for those authors to have to request the library authors to add an attribute before they can start doing it.
68-
69-
One option to address this is to remove `[Interceptable]` from the design and not provide any mechanism for limiting which calls can be intercepted or not.
70-
71-
Another option would be to expose an assembly attribute instead.
72-
73-
```cs
74-
namespace System.Runtime.CompilerServices
75-
{
76-
/// <summary>If present, indicates the set of types whose methods can have their calls intercepted in the current compilation.</summary>
77-
/// <remarks>When this attribute is not present, methods on any type can have their calls intercepted.</remarks>
78-
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = false)]
79-
public sealed class AllowInterception(params Type[] types) : Attribute { }
80-
}
81-
82-
[assembly: AllowInterception(typeof(EndpointRouteBuilderExtensions))]
83-
```
84-
85-
In order to intercept a classic extension method call, the extension type must be provided to this attribute, not the `this` parameter type.
86-
87-
We specify `AllowMultiple = false` here to imply that the attribute is supposed to be hand-authored by the user. It's not meant for generators to use this to *opt themselves in* to being able to intercept things--it's for the user to state once and comprehensively which things can be intercepted.
88-
89-
In such a scheme, it's not clear how often users would actually want to go to the trouble of writing the attribute--particularly since the process of writing and maintaining it would simply be to run source generators, review the "not interceptable" errors, and add the related types to this attribute. Is that useful to users in practice?
90-
91-
Another alternative here might be to use `AllowMultiple = true`, require in practice that generators produce these attributes (i.e. disallow intercepting in the current compilation if this attribute isn't used in it), and let users search for usages of the attribute to get a sense of which calls may be getting intercepted in their project.
92-
9349
### InterceptsLocationAttribute
9450

9551
A method indicates that it is an *interceptor* by adding one or more `[InterceptsLocation]` attributes. These attributes refer to the source locations of the calls it intercepts.
@@ -104,10 +60,12 @@ namespace System.Runtime.CompilerServices
10460
}
10561
```
10662

63+
Any "ordinary method" (i.e. with `MethodKind.Ordinary`) can have its calls intercepted.
64+
10765
`[InterceptsLocation]` attributes included in source are emitted to the resulting assembly, just like other custom attributes.
10866

10967
PROTOTYPE(ic): We may want to recognize `file class InterceptsLocationAttribute` as a valid declaration of the attribute, to allow generators to bring the attribute in without conflicting with other generators which may also be bringing the attribute in. See open question in [User opt-in](#user-opt-in).
110-
https://github.com/dotnet/roslyn/issues/67079 is a bug which causes file-local source declarations of well-known attributes to be generally treated as known. When that bug is fixed, we may want to single out one or both of `InterceptableAttribute` and `InterceptsLocationAttribute` as "recognized, even though they are file-local".
68+
https://github.com/dotnet/roslyn/issues/67079 is a bug which causes file-local source declarations of well-known attributes to be generally treated as known. When that bug is fixed, we may want to single out `InterceptsLocationAttribute` as "recognized, even though they are file-local".
11169

11270
#### File paths
11371

@@ -146,7 +104,6 @@ using System.Runtime.CompilerServices;
146104

147105
class C
148106
{
149-
[Interceptable]
150107
public static void InterceptableMethod<T1>(T1 t) => throw null!;
151108
}
152109

@@ -160,7 +117,7 @@ static class Program
160117

161118
static class D
162119
{
163-
[InterceptsLocation("Program.cs", 13, 11)]
120+
[InterceptsLocation("Program.cs", 12, 11)]
164121
public static void Interceptor1(object s) => throw null!;
165122
}
166123
```

src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter_Call.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,6 @@ private void InterceptCallAndAdjustArguments(
154154
Debug.Assert(interceptableLocation != null);
155155
Debug.Assert(interceptor.Arity == 0);
156156

157-
if (!method.IsInterceptable)
158-
{
159-
// PROTOTYPE(ic): Eventually we may want this to be an error.
160-
// For now it's convenient to just warn so we can experiment with intercepting APIs that haven't yet been marked.
161-
this._diagnostics.Add(ErrorCode.WRN_CallNotInterceptable, attributeLocation, method);
162-
}
163-
164157
var containingMethod = this._factory.CurrentFunction;
165158
Debug.Assert(containingMethod is not null);
166159

src/Compilers/CSharp/Portable/Symbols/Source/SourceMethodSymbolWithAttributes.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -597,14 +597,6 @@ private void DecodeWellKnownAttributeAppliedToMethod(ref DecodeWellKnownAttribut
597597
diagnostics.Add(ErrorCode.ERR_UnscopedRefAttributeUnsupportedMemberTarget, arguments.AttributeSyntaxOpt.Location);
598598
}
599599
}
600-
else if (attribute.IsTargetAttribute(this, AttributeDescription.InterceptableAttribute))
601-
{
602-
if (MethodKind != MethodKind.Ordinary)
603-
{
604-
diagnostics.Add(ErrorCode.ERR_InterceptableMethodMustBeOrdinary, arguments.AttributeSyntaxOpt.Location);
605-
}
606-
arguments.GetOrCreateData<MethodWellKnownAttributeData>().HasInterceptableAttribute = true;
607-
}
608600
else if (attribute.IsTargetAttribute(this, AttributeDescription.InterceptsLocationAttribute))
609601
{
610602
DecodeInterceptsLocationAttribute(arguments);

0 commit comments

Comments
 (0)