Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RequiresVariableReceiver should likely check for "not a reference type" instead of checking for "is a value type" #73003

Open
jcouv opened this issue Apr 12, 2024 · 0 comments

Comments

@jcouv
Copy link
Member

jcouv commented Apr 12, 2024

This is likely a bug, but needs confirmation on spec side: dotnet/csharpstandard#1078

        [Fact, WorkItem("")]
        public void CS0131ERR_AssgLvalueExpected03_UnconstrainedTypeParameter()
        {
            var source = """
struct S
{
    public object P { get; set; }
    public object this[object index] { get { return null; } set { } }
}
interface I
{
    object P { get; set; }
    object this[object index] { get; set; }
}
class C
{
    static void M<T>() where T : I
    {
        default(T).P = null; // missing diagnostic
        default(T)[0] = null; // missing diagnostic
    }
}
""";
            CreateCompilation(source).VerifyDiagnostics();
        }

Relevant logic:

        /// <summary>
        /// SPEC: When a property or indexer declared in a struct-type is the target of an 
        /// SPEC: assignment, the instance expression associated with the property or indexer 
        /// SPEC: access must be classified as a variable. If the instance expression is 
        /// SPEC: classified as a value, a compile-time error occurs. Because of 7.6.4, 
        /// SPEC: the same rule also applies to fields.
        /// </summary>
        /// <remarks>
        /// NOTE: The spec fails to impose the restriction that the event receiver must be classified
        /// as a variable (unlike for properties - 7.17.1).  This seems like a bug, but we have
        /// production code that won't build with the restriction in place (see DevDiv #15674).
        /// </remarks>
        private static bool RequiresVariableReceiver(BoundExpression receiver, Symbol symbol)
        {
            return symbol.RequiresInstanceReceiver()
                && symbol.Kind != SymbolKind.Event
                && receiver?.Type?.IsValueType == true;
        }

FYI @AlekseyTs

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged Issues and PRs which have not yet been triaged by a lead label Apr 12, 2024
@jaredpar jaredpar removed the untriaged Issues and PRs which have not yet been triaged by a lead label Apr 22, 2024
@jaredpar jaredpar added this to the Backlog milestone Apr 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants