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

Readonly fields have an accessible home when accessed from init-only setter #46323

Merged
merged 4 commits into from
Aug 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/Compilers/CSharp/Portable/Binder/Binder.ValueChecks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3360,19 +3360,18 @@ private static bool HasHome(
}

// while readonly fields have home it is not valid to refer to it when not constructing.
if (!TypeSymbol.Equals(field.ContainingType, method.ContainingType, TypeCompareKind.ConsiderEverything2))
if (!TypeSymbol.Equals(field.ContainingType, method.ContainingType, TypeCompareKind.ConsiderEverything))
Copy link
Contributor

@AlekseyTs AlekseyTs Jul 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ConsiderEverything [](start = 96, length = 18)

This option doesn't look right, why differences in nullable annotations, or dynamic, or native int, or tuple names, should make a difference? I think we should ignore all options that could be ignored. #Closed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the problem can be demonstrated with this failing test:

        [Fact]
        public void Test()
        {
            string source = @"
public class C
{
    public static void Main()
    {
        System.Console.WriteLine(C1<int>.F1.content);
        System.Console.WriteLine(C2<int>.F1.content);
    }
}

public struct Container
{
    public int content;
}

class C1<T>
{
    public static readonly Container F1;
    
    static C1()
    {
        C1<T>.F1.content = 2;
    }
}


#nullable enable

class C2<T>
{
    public static readonly Container F1;
    
    static C2()
    {
        C2<T>.F1.content = 2;
    }
}
";
            var comp = CreateCompilation(new[] { source, IsExternalInitTypeDefinition }, parseOptions: TestOptions.RegularPreview, options: TestOptions.DebugExe);
            comp.VerifyEmitDiagnostics();
            CompileAndVerify(comp, expectedOutput: 
@"
2
2
");
        }

In reply to: 460448404 [](ancestors = 460448404)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks much for the test scenario. Added and fixed.

{
return false;
}


if (field.IsStatic)
{
return method.MethodKind == MethodKind.StaticConstructor;
}
else
{
return method.MethodKind == MethodKind.Constructor &&
return (method.MethodKind == MethodKind.Constructor || method.IsInitOnly) &&
fieldAccess.ReceiverOpt.Kind == BoundKind.ThisReference;
}
}
Expand Down
Loading