Skip to content

Internal extension property + InternalsVisibleTo triggers CS0570 (not supported by the language) in consumer assembly #81180

@nil4

Description

@nil4

Version Used:

.NET 10 SDK (10.0.100)
Compiler version: '5.0.0-2.25523.111 (b0f34d51)'. Language version: 14.0

Steps to Reproduce:

Create a class library project and a console app referencing it:

> mkdir repro && cd repro
> mkdir ReproLib && pushd ReproLib && dotnet new classlib && popd
> mkdir TestConsumer && pushd TestConsumer && dotnet new console && dotnet add reference ../ReproLib && popd

Ref. https://learn.microsoft.com/dotnet/csharp/language-reference/proposals/csharp-14.0/extensions#nullability-and-attributes edit ReproLib/Class1.cs and replace its content with:

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("TestConsumer")]

namespace ReproLib;

internal static class Extensions {
    extension([NotNullWhen(false)] string? text) {
        internal bool IsEmpty => string.IsNullOrEmpty(text);
        internal bool IsWhitespace => string.IsNullOrWhiteSpace(text);
    }
}

This defines a couple of internal extension properties, but note also makes internals visible to the TestConsumer assembly.

Edit TestConsumer/Program.cs and replace its content with:

using System.Diagnostics;

using ReproLib;

static class Program {
    static void Main() {
        Debug.Assert((null as string).IsEmpty);
        Debug.Assert("".IsEmpty);
        Debug.Assert("\t".IsEmpty is false);

        Debug.Assert((null as string).IsWhitespace);
        Debug.Assert("".IsWhitespace);
        Debug.Assert("\t".IsWhitespace);
    }
}

Build the TestConsumer project:

> cd TestConsumer && dotnet build

Expected Behavior:

The compilation to succeed since internal types and members were explicitly exposed to TestConsumer.

Actual Behavior:

Every use of internal extension properties defined in ReproLib is flagged with error CS0570 in TestConsumer:

Restore complete (0.2s)
  ReproLib net10.0 succeeded (0.9s) → ~/repro/ReproLib/bin/Debug/net10.0/ReproLib.dll
  TestConsumer net10.0 failed with 8 error(s) (0.1s)
    ~/repro/TestConsumer/Program.cs(9,39): error CS0570: 'Extensions.extension(string?).IsEmpty.get' is not supported by the language
    ~/repro/TestConsumer/Program.cs(10,25): error CS0570: 'Extensions.extension(string?).IsEmpty.get' is not supported by the language
    ~/repro/TestConsumer/Program.cs(11,27): error CS0570: 'Extensions.extension(string?).IsEmpty.get' is not supported by the language
    ~/repro/TestConsumer/Program.cs(13,39): error CS0570: 'Extensions.extension(string?).IsWhitespace.get' is not supported by the language
    ~/repro/TestConsumer/Program.cs(14,25): error CS0570: 'Extensions.extension(string?).IsWhitespace.get' is not supported by the language
    ~/repro/TestConsumer/Program.cs(15,27): error CS0570: 'Extensions.extension(string?).IsWhitespace.get' is not supported by the language
    ~/repro/TestConsumer/Program.cs(16,27): error CS0570: 'Extensions.extension(string?).IsWhitespace.get' is not supported by the language

This is unexpected; using classic extension methods here would compile without issue.

Is this a bug or a missing feature, or is C# 14 designed not to allow internal extension properties to be used from other assemblies despite InternalsVisibleTo?

Metadata

Metadata

Assignees

Type

No fields configured for Bug.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions