-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Put record parameters in scope withing instance initializers #44906
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
544916f
Put record parameters in scope withing instance initializers
AlekseyTs edab8c7
Merge remote-tracking branch 'dotnet/features/records' into Records_04
AlekseyTs f96f815
Follow-up on merge
AlekseyTs 1cc4a0a
Share closure across initializers and the base call.
AlekseyTs 2b0ca70
Merge remote-tracking branch 'dotnet/features/records' into Records_04
AlekseyTs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -341,15 +341,7 @@ record C1(object O1) | |
public object O1 { get; } = O1; | ||
public object O2 { get; } = O1; | ||
}"); | ||
// PROTOTYPE: primary ctor parameters not currently in scope | ||
comp.VerifyDiagnostics( | ||
// (4,33): error CS0236: A field initializer cannot reference the non-static field, method, or property 'C1.O1' | ||
// public object O1 { get; } = O1; | ||
Diagnostic(ErrorCode.ERR_FieldInitRefNonstatic, "O1").WithArguments("C1.O1").WithLocation(4, 33), | ||
// (5,33): error CS0236: A field initializer cannot reference the non-static field, method, or property 'C1.O1' | ||
// public object O2 { get; } = O1; | ||
Diagnostic(ErrorCode.ERR_FieldInitRefNonstatic, "O1").WithArguments("C1.O1").WithLocation(5, 33) | ||
); | ||
comp.VerifyDiagnostics(); | ||
} | ||
|
||
[Fact] | ||
|
@@ -389,15 +381,10 @@ public void RecordProperties_10() | |
}"; | ||
var comp = CreateCompilation(src); | ||
comp.VerifyDiagnostics( | ||
// (1,1): warning CS1717: Assignment made to same variable; did you mean to assign something else? | ||
// record C(object P) | ||
Diagnostic(ErrorCode.WRN_AssignmentToSelf, @"record C(object P) | ||
{ | ||
const int P = 4; | ||
}").WithLocation(1, 1), | ||
// (1,17): error CS0102: The type 'C' already contains a definition for 'P' | ||
// record C(object P) | ||
Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "P").WithArguments("C", "P").WithLocation(1, 17)); | ||
Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "P").WithArguments("C", "P").WithLocation(1, 17) | ||
); | ||
} | ||
|
||
[Fact] | ||
|
@@ -2936,17 +2923,7 @@ public object P3 { set { } } | |
public ref object P5 => throw null; | ||
}"; | ||
var comp = CreateCompilation(source); | ||
comp.VerifyDiagnostics( | ||
// (1,1): warning CS1717: Assignment made to same variable; did you mean to assign something else? | ||
// record C(object P1, object P2, object P3, object P4, object P5) | ||
Diagnostic(ErrorCode.WRN_AssignmentToSelf, @"record C(object P1, object P2, object P3, object P4, object P5) | ||
{ | ||
public object P1 { get { return null; } set { } } | ||
public object P2 { get; } | ||
public object P3 { set { } } | ||
public static object P4 { get; set; } | ||
public ref object P5 => throw null; | ||
}").WithLocation(1, 1)); | ||
comp.VerifyDiagnostics(); | ||
|
||
var actualMembers = GetProperties(comp, "C").ToTestDisplayStrings(); | ||
var expectedMembers = new[] | ||
|
@@ -2958,6 +2935,22 @@ public object P3 { set { } } | |
"ref System.Object C.P5 { get; }", | ||
}; | ||
AssertEx.Equal(expectedMembers, actualMembers); | ||
|
||
var verifier = CompileAndVerify(source); | ||
|
||
verifier.VerifyIL("C..ctor(C)", @" | ||
{ | ||
// Code size 19 (0x13) | ||
.maxstack 2 | ||
IL_0000: ldarg.0 | ||
IL_0001: call ""object..ctor()"" | ||
IL_0006: ldarg.0 | ||
IL_0007: ldarg.1 | ||
IL_0008: ldfld ""object C.<P2>k__BackingField"" | ||
IL_000d: stfld ""object C.<P2>k__BackingField"" | ||
IL_0012: ret | ||
} | ||
"); | ||
} | ||
|
||
[Fact] | ||
|
@@ -4298,5 +4291,149 @@ interface C : Base(X) | |
Assert.Empty(model.LookupSymbols(x.SpanStart, name: "X")); | ||
Assert.DoesNotContain("X", model.LookupNames(x.SpanStart)); | ||
} | ||
|
||
[Fact] | ||
public void BaseArguments_15() | ||
{ | ||
var src = @" | ||
using System; | ||
|
||
record Base | ||
{ | ||
public Base(int X, int Y) | ||
{ | ||
Console.WriteLine(X); | ||
Console.WriteLine(Y); | ||
} | ||
|
||
public Base() {} | ||
} | ||
|
||
partial record C | ||
{ | ||
} | ||
|
||
partial record C(int X, int Y) : Base(X, Y) | ||
{ | ||
int Z = 123; | ||
public static void Main() | ||
{ | ||
var c = new C(1, 2); | ||
Console.WriteLine(c.Z); | ||
} | ||
} | ||
|
||
partial record C | ||
{ | ||
} | ||
"; | ||
var verifier = CompileAndVerify(src, expectedOutput: @" | ||
1 | ||
2 | ||
123"); | ||
verifier.VerifyIL("C..ctor(int, int)", @" | ||
|
||
{ | ||
// Code size 31 (0x1f) | ||
.maxstack 3 | ||
IL_0000: ldarg.0 | ||
IL_0001: ldarg.1 | ||
IL_0002: stfld ""int C.<X>k__BackingField"" | ||
IL_0007: ldarg.0 | ||
IL_0008: ldarg.2 | ||
IL_0009: stfld ""int C.<Y>k__BackingField"" | ||
IL_000e: ldarg.0 | ||
IL_000f: ldc.i4.s 123 | ||
IL_0011: stfld ""int C.Z"" | ||
IL_0016: ldarg.0 | ||
IL_0017: ldarg.1 | ||
IL_0018: ldarg.2 | ||
IL_0019: call ""Base..ctor(int, int)"" | ||
IL_001e: ret | ||
} | ||
"); | ||
|
||
var comp = CreateCompilation(src); | ||
|
||
var tree = comp.SyntaxTrees.First(); | ||
var model = comp.GetSemanticModel(tree); | ||
|
||
var x = tree.GetRoot().DescendantNodes().OfType<IdentifierNameSyntax>().Where(id => id.Identifier.ValueText == "X").ElementAt(1); | ||
Assert.Equal("Base(X, Y)", x.Parent!.Parent!.Parent!.ToString()); | ||
|
||
var symbol = model.GetSymbolInfo(x).Symbol; | ||
Assert.Equal(SymbolKind.Parameter, symbol!.Kind); | ||
Assert.Equal("System.Int32 X", symbol.ToTestDisplayString()); | ||
Assert.Equal("C..ctor(System.Int32 X, System.Int32 Y)", symbol.ContainingSymbol.ToTestDisplayString()); | ||
Assert.Same(symbol.ContainingSymbol, model.GetEnclosingSymbol(x.SpanStart)); | ||
Assert.Contains(symbol, model.LookupSymbols(x.SpanStart, name: "X")); | ||
Assert.Contains("X", model.LookupNames(x.SpanStart)); | ||
} | ||
|
||
[Fact] | ||
public void Initializers_01() | ||
{ | ||
var src = @" | ||
using System; | ||
|
||
record C(int X) | ||
{ | ||
int Z = X + 1; | ||
|
||
public static void Main() | ||
{ | ||
var c = new C(1); | ||
Console.WriteLine(c.Z); | ||
} | ||
}"; | ||
var verifier = CompileAndVerify(src, expectedOutput: @"2"); | ||
|
||
var comp = CreateCompilation(src); | ||
|
||
var tree = comp.SyntaxTrees.First(); | ||
var model = comp.GetSemanticModel(tree); | ||
|
||
var x = tree.GetRoot().DescendantNodes().OfType<IdentifierNameSyntax>().Where(id => id.Identifier.ValueText == "X").First(); | ||
Assert.Equal("= X + 1", x.Parent!.Parent!.ToString()); | ||
|
||
var symbol = model.GetSymbolInfo(x).Symbol; | ||
Assert.Equal(SymbolKind.Parameter, symbol!.Kind); | ||
Assert.Equal("System.Int32 X", symbol.ToTestDisplayString()); | ||
Assert.Equal("C..ctor(System.Int32 X)", symbol.ContainingSymbol.ToTestDisplayString()); | ||
Assert.Equal("System.Int32 C.Z", model.GetEnclosingSymbol(x.SpanStart).ToTestDisplayString()); | ||
Assert.Contains(symbol, model.LookupSymbols(x.SpanStart, name: "X")); | ||
Assert.Contains("X", model.LookupNames(x.SpanStart)); | ||
} | ||
|
||
[Fact] | ||
public void Initializers_02() | ||
{ | ||
var src = @" | ||
record C(int X) | ||
{ | ||
static int Z = X + 1; | ||
}"; | ||
|
||
var comp = CreateCompilation(src); | ||
comp.VerifyDiagnostics( | ||
// (4,20): error CS0236: A field initializer cannot reference the non-static field, method, or property 'C.X' | ||
// static int Z = X + 1; | ||
Diagnostic(ErrorCode.ERR_FieldInitRefNonstatic, "X").WithArguments("C.X").WithLocation(4, 20) | ||
); | ||
|
||
var tree = comp.SyntaxTrees.First(); | ||
var model = comp.GetSemanticModel(tree); | ||
|
||
var x = tree.GetRoot().DescendantNodes().OfType<IdentifierNameSyntax>().Where(id => id.Identifier.ValueText == "X").First(); | ||
Assert.Equal("= X + 1", x.Parent!.Parent!.ToString()); | ||
|
||
var symbol = model.GetSymbolInfo(x).Symbol; | ||
Assert.Equal(SymbolKind.Property, symbol!.Kind); | ||
Assert.Equal("System.Int32 C.X { get; init; }", symbol.ToTestDisplayString()); | ||
Assert.Equal("C", symbol.ContainingSymbol.ToTestDisplayString()); | ||
Assert.Equal("System.Int32 C.Z", model.GetEnclosingSymbol(x.SpanStart).ToTestDisplayString()); | ||
Assert.Contains(symbol, model.LookupSymbols(x.SpanStart, name: "X")); | ||
Assert.Contains("X", model.LookupNames(x.SpanStart)); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider testing capturing: record C(int X)
{
Func<int> F = () => X;
}
``` #Resolved |
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider testing a
const
field as well. #ResolvedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Const fields are static fields, so I am pretty confident this test is fairly equivalent to verify scoping rules. I will add a test like that for completeness sake in a separate PR
In reply to: 436281327 [](ancestors = 436281327)