Skip to content

Commit 07648a6

Browse files
committed
Strip '[Embedded]' when types are public
1 parent c4a8267 commit 07648a6

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/PolySharp.SourceGenerators/PolyfillsGenerator.Polyfills.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ where resourceName.StartsWith("PolySharp.SourceGenerators.EmbeddedResources.Runt
6161
/// </summary>
6262
private static readonly Regex ExcludeFromCodeCoverageRegex = new(@" *\[global::System\.Diagnostics\.CodeAnalysis\.ExcludeFromCodeCoverage\]\r?\n", RegexOptions.Compiled);
6363

64+
/// <summary>
65+
/// The <see cref="Regex"/> to find all <see cref="EmbeddedAttribute"/> uses.
66+
/// </summary>
67+
private static readonly Regex EmbeddedAttributeRegex = new(@" *\[global::Microsoft\.CodeAnalysis\.Embedded\]\r?\n", RegexOptions.Compiled);
68+
6469
/// <summary>
6570
/// The dictionary of cached sources to produce.
6671
/// </summary>
@@ -282,6 +287,9 @@ private void EmitGeneratedType(SourceProductionContext context, GeneratedType ty
282287
// rewriter, or just by retrieving the type declaration syntax and updating the modifier tokens, but since the
283288
// change is so minimal, it can very well just be done this way to keep things simple, that's fine in this case.
284289
adjustedSource = adjustedSource.Replace(" internal ", " public ");
290+
291+
// If types are public, we also need to strip the '[Embedded]' attributes, as it's only allowed on public types
292+
adjustedSource = EmbeddedAttributeRegex.Replace(adjustedSource, "");
285293
}
286294

287295
if ((type.FixupType & SyntaxFixupType.RemoveMethodImplAttributes) != 0)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
6+
namespace Microsoft.CodeAnalysis;
7+
8+
/// <summary>
9+
/// Marks a type as "embedded", meaning it won't ever be visible from other assemblies.
10+
/// </summary>
11+
[AttributeUsage(AttributeTargets.All)]
12+
internal sealed class EmbeddedAttribute : Attribute;

0 commit comments

Comments
 (0)