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

Conversation

jcouv
Copy link
Member

@jcouv jcouv commented Jul 25, 2020

Fixes #45657

@jcouv jcouv self-assigned this Jul 25, 2020
@jcouv jcouv marked this pull request as ready for review July 25, 2020 20:31
@jcouv jcouv requested a review from a team as a code owner July 25, 2020 20:31
@@ -3360,19 +3360,18 @@ internal enum AddressKind
}

// 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.

@AlekseyTs
Copy link
Contributor

AlekseyTs commented Jul 25, 2020

Done with review pass (iteration 1) #Closed

";
var comp = CreateCompilation(new[] { source, IsExternalInitTypeDefinition }, parseOptions: TestOptions.RegularPreview, options: TestOptions.DebugExe);
comp.VerifyEmitDiagnostics();
CompileAndVerify(comp, expectedOutput: "2 3", verify: Verification.Skipped /* init-only */);
Copy link
Contributor

@AlekseyTs AlekseyTs Jul 27, 2020

Choose a reason for hiding this comment

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

verify: Verification.Skipped /* init-only */ [](start = 58, length = 44)

This isn't an init-only scenario, no need to skip verification, I think. #Closed

Copy link
Member Author

Choose a reason for hiding this comment

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

I think you're right. I did run into a PE verification error though.

As far as I can tell we're emitting the same IL already and in non-generic scenario too (which also fails PE verification).

Feels like a PE verification bug. Is there a set of IL verification rules that I could reference to confirm this?

            // [ : C::Main][mdToken=0x6000004][offset 0x00000001] Cannot change initonly field outside its .ctor.
            v.VerifyIL("C.Main", @"
{
  // Code size       45 (0x2d)
  .maxstack  1
  IL_0000:  nop
  IL_0001:  ldsflda    ""Container C1<int>.F1""
  IL_0006:  ldfld      ""int Container.content""
  IL_000b:  call       ""void System.Console.Write(int)""
  IL_0010:  nop
  IL_0011:  ldstr      "" ""
  IL_0016:  call       ""void System.Console.Write(string)""
  IL_001b:  nop
  IL_001c:  ldsflda    ""Container C2<int>.F1""
  IL_0021:  ldfld      ""int Container.content""
  IL_0026:  call       ""void System.Console.Write(int)""
  IL_002b:  nop
  IL_002c:  ret
}
");

Copy link
Contributor

@AlekseyTs AlekseyTs left a comment

Choose a reason for hiding this comment

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

LGTM (iteration 2), with minor comment for the test.

Copy link
Contributor

@AlekseyTs AlekseyTs left a comment

Choose a reason for hiding this comment

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

LGTM (iteration 3), assuming CI is passing

@jcouv
Copy link
Member Author

jcouv commented Jul 30, 2020

@dotnet/roslyn-compiler for second review. Thanks

@jcouv
Copy link
Member Author

jcouv commented Aug 3, 2020

@dotnet/roslyn-compiler for second review. Thanks

comp.VerifyDiagnostics(
// (9,9): error CS0191: A readonly field cannot be assigned to (except in a constructor or init-only setter of the type in which the field is defined or a variable initializer)
comp.VerifyEmitDiagnostics(
// (9,9): error CS0191: A readonly field cannot be assigned to (except in a constructor or init-only setter of the class in which the field is defined or a variable initializer))
Copy link
Member

@cston cston Aug 3, 2020

Choose a reason for hiding this comment

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

class in which the field is defined or a variable initializer)) [](start = 131, length = 63)

Is this comment stale?

Same comment in other locations.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is an artifact of the GitHub diff view, I think.

Locally, the file shows the correct comment:

                // (9,9): error CS0191: A readonly field cannot be assigned to (except in a constructor or init-only setter of the type in which the field is defined or a variable initializer)
                //         c.field = null; // 1
                Diagnostic(ErrorCode.ERR_AssgReadonly, "c.field").WithLocation(9, 9),

@jcouv jcouv merged commit 06c9048 into dotnet:master Aug 4, 2020
@ghost ghost added this to the Next milestone Aug 4, 2020
@jcouv jcouv deleted the init-home branch August 4, 2020 00:02
@RikkiGibson RikkiGibson modified the milestones: Next, 16.8.P2 Aug 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants