Skip to content

Commit

Permalink
Mark ISourceGenerator as obsolete for implementation
Browse files Browse the repository at this point in the history
Analyzer that will flag implementation is dotnet/roslyn-analyzers#7419. We don't just mark `ISourceGenerator` as obsolete directly because we do use it in public APIs like `AnalyzerFileReference` and `GeneratorDriver`, and we can't remove it from there.
  • Loading branch information
333fred committed Sep 24, 2024
1 parent 5ef52ae commit 5fc1cc9
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions docs/features/incremental-generators.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

## Summary

Incremental generators are a new API that exists alongside
[source generators](source-generators.md) to allow users to specify generation
Incremental generators are a new API that replaces
[v1 source generators](source-generators.md) to allow users to specify generation
strategies that can be applied in a high performance way by the hosting layer.

### High Level Design Goals
Expand Down
11 changes: 11 additions & 0 deletions src/Compilers/Core/Portable/ImplementationIsObsoleteAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace System.Runtime.CompilerServices;

[AttributeUsage(AttributeTargets.Interface, AllowMultiple = false)]
internal sealed class ImplementationObsoleteAttribute(string url) : Attribute
{
public string Url { get; } = url;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Collections.Immutable;
using System.Runtime.CompilerServices;
using System.Threading;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.PooledObjects;
Expand All @@ -19,13 +20,15 @@ namespace Microsoft.CodeAnalysis
/// is no guarantee that the same instance will be used on a
/// subsequent generation pass.
/// </remarks>
[ImplementationObsolete(url: "https://github.com/dotnet/roslyn/blob/main/docs/features/incremental-generators.md")]
public interface ISourceGenerator
{
/// <summary>
/// Called before generation occurs. A generator can use the <paramref name="context"/>
/// to register callbacks required to perform generation.
/// </summary>
/// <param name="context">The <see cref="GeneratorInitializationContext"/> to register callbacks on</param>
[Obsolete("ISourceGenerator is deprecated and should not be implemented. Please implement IIncrementalGenerator instead. See https://github.com/dotnet/roslyn/blob/main/docs/features/incremental-generators.md.")]
void Initialize(GeneratorInitializationContext context);

/// <summary>
Expand All @@ -42,6 +45,7 @@ public interface ISourceGenerator
/// discover information about the users compilation and make decisions on what source to
/// provide.
/// </remarks>
[Obsolete("ISourceGenerator is deprecated and should not be implemented. Please implement IIncrementalGenerator instead. See https://github.com/dotnet/roslyn/blob/main/docs/features/incremental-generators.md.")]
void Execute(GeneratorExecutionContext context);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -749,11 +749,13 @@ End Class

Public _receiver As Receiver = New Receiver()

#Disable Warning BC40000
Public Sub Initialize(context As GeneratorInitializationContext) Implements ISourceGenerator.Initialize
context.RegisterForSyntaxNotifications(Function() _receiver)
End Sub

Public Sub Execute(context As GeneratorExecutionContext) Implements ISourceGenerator.Execute
#Enable Warning BC40000
context.AddSource("source.vb", "
Public Class D
End Class
Expand Down Expand Up @@ -801,13 +803,15 @@ End Class
_initialized = True
End Sub

#Disable Warning BC40000
Public Sub Initialize(context As GeneratorInitializationContext) Implements ISourceGenerator.Initialize
_sourceInitialized = True
End Sub

Public Sub Execute(context As GeneratorExecutionContext) Implements ISourceGenerator.Execute
_sourceExecuted = True
End Sub
#Enable Warning BC40000

End Class

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,12 @@ public partial class GeneratedClass : IInterface { }
Private Class GeneratorThatImplementsInterfaceMethod
Implements ISourceGenerator

#Disable Warning BC40000
Public Sub Initialize(context As GeneratorInitializationContext) Implements ISourceGenerator.Initialize
End Sub

Public Sub Execute(context As GeneratorExecutionContext) Implements ISourceGenerator.Execute
#Enable Warning BC40000
Dim [interface] = context.Compilation.GetTypeByMetadataName("IInterface")
Dim memberName = [interface].MemberNames.Single()

Expand Down

0 comments on commit 5fc1cc9

Please sign in to comment.