Skip to content

Commit

Permalink
tweak
Browse files Browse the repository at this point in the history
  • Loading branch information
jcouv committed May 8, 2020
1 parent 5af14a6 commit 0bfc351
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 9 deletions.
100 changes: 94 additions & 6 deletions src/Compilers/CSharp/Test/Semantic/Semantics/InitOnlyMemberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ public class InitOnlyMemberTests : CompilingTestBase
// Spec: https://github.com/dotnet/csharplang/blob/master/proposals/init.md

// PROTOTYPE(init-only): test allowed from 'with' expression
// PROTOTYPE(init-only): public API, confirm behavior of IsInitOnly and test on each leaf type of wrapped symbol

// PROTOTYPE(init-only): open issues:
// PROTOTYPE(init-only): queue discussion on init methods (`init void Init()`) and collection initializers (`init void Add()`)

// PROTOTYPE(init-only): test dynamic scenario
// PROTOTYPE(init-only): test whether reflection use property despite modreq?
// PROTOTYPE(init-only): test behavior of old compiler with modreq. For example VB
Expand Down Expand Up @@ -603,6 +598,74 @@ public string RegularProperty2
Assert.True(property.GetPublicSymbol().SetMethod.IsInitOnly);
}

[Fact(Skip = "PROTOTYPE(init-only) Not yet supported")]
public void InitOnlyPropertyAssignmentAllowedInWithInitializer()
{
string source = @"
public class C
{
public int Property { get; init; }
void M(C c)
{
_ = c with { Property = null };
}
public C Clone() => throw null;
}
class Derived : C
{
}
class Derived2 : Derived
{
void M(C c)
{
_ = c with { Property = null };
_ = this with { Property = null };
}
}
class Other
{
void M()
{
var c = new C() with { Property = 42 };
System.Console.Write($""{c.Property}"");
}
}
";
var comp = CreateCompilation(new[] { source, IsExternalInitTypeDefinition }, parseOptions: TestOptions.RegularPreview);
comp.VerifyDiagnostics();
}

[Fact(Skip = "PROTOTYPE(init-only) Not yet supported")]
public void InitOnlyPropertyAssignmentAllowedInWithInitializer_Evaluation()
{
string source = @"
public class C
{
private int field;
public int Property { get { return field; } init { field = value; System.Console.Write(""set ""); } }
public C Clone() { System.Console.Write(""clone ""); return this; }
}
class Other
{
public static void Main()
{
var c = new C() with { Property = 42 };
System.Console.Write($""{c.Property}"");
}
}
";
var comp = CreateCompilation(new[] { source, IsExternalInitTypeDefinition }, parseOptions: TestOptions.RegularPreview, options: TestOptions.DebugExe);
comp.VerifyDiagnostics();
CompileAndVerify(comp, expectedOutput: "clone set 42");
}

[Fact]
public void EvaluationInitOnlySetter()
{
Expand Down Expand Up @@ -2025,7 +2088,7 @@ public event System.Action Event
}

[Fact]
public void ConstructorsAreNotInitOnly()
public void ConstructorAndDestructorAreNotInitOnly()
{
string source = @"
public class C
Expand All @@ -2046,6 +2109,31 @@ public C() { }
Assert.False(destructor.GetPublicSymbol().IsInitOnly);
}

[Fact]
public void ConstructedMethodsAreNotInitOnly()
{
string source = @"
public class C
{
void M<T>()
{
M<string>();
}
}
";
var comp = CreateCompilation(new[] { source, IsExternalInitTypeDefinition }, parseOptions: TestOptions.RegularPreview);
comp.VerifyDiagnostics();

var tree = comp.SyntaxTrees[0];
var root = tree.GetCompilationUnitRoot();
var model = comp.GetSemanticModel(tree, ignoreAccessibility: true);

var invocation = root.DescendantNodes().OfType<InvocationExpressionSyntax>().Single();
var method = (IMethodSymbol)model.GetSymbolInfo(invocation).Symbol;
Assert.Equal("void C.M<System.String>()", method.ToTestDisplayString());
Assert.False(method.IsInitOnly);
}

[Fact]
public void InitOnlyOnMembersOfRecords()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8437,7 +8437,7 @@ interface I
class C : [|I|]
{
public int Property { get => throw new System.NotImplementedException(); set => throw new System.NotImplementedException(); }
public int Property { get => throw new System.NotImplementedException(); init => throw new System.NotImplementedException(); }
}");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ private IMethodSymbol GetMethodSymbol(
modifiers: new DeclarationModifiers(isStatic: eventHookupExpression.IsInStaticContext()),
returnType: delegateInvokeMethod.ReturnType,
refKind: delegateInvokeMethod.RefKind,
isInitOnly: false,
explicitInterfaceImplementations: default,
name: eventHandlerMethodName,
typeParameters: default,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ public static Tuple<string, string, VsTextSpan> EnsureEventHandler(
modifiers: new DeclarationModifiers(),
returnType: targetDocument.Project.GetCompilationAsync(cancellationToken).WaitAndGetResult_Venus(cancellationToken).GetSpecialType(SpecialType.System_Void),
refKind: RefKind.None,
isInitOnly: false,
explicitInterfaceImplementations: default,
name: eventHandlerName,
typeParameters: default,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ protected SyntaxNode CreateEventDeclaration(SyntaxNode containerNode, string nam
modifiers: new DeclarationModifiers(),
returnType: null,
refKind: RefKind.None,
isInitOnly: false,
explicitInterfaceImplementations: default,
name: "add_" + name,
typeParameters: default,
Expand All @@ -96,6 +97,7 @@ protected SyntaxNode CreateEventDeclaration(SyntaxNode containerNode, string nam
modifiers: new DeclarationModifiers(),
returnType: null,
refKind: RefKind.None,
isInitOnly: false,
explicitInterfaceImplementations: default,
name: "remove_" + name,
typeParameters: default,
Expand Down Expand Up @@ -143,6 +145,7 @@ protected SyntaxNode CreateMethodDeclaration(SyntaxNode containerNode, string na
modifiers: new DeclarationModifiers(),
returnType: returnType,
refKind: RefKind.None,
isInitOnly: false,
explicitInterfaceImplementations: default,
name: name,
typeParameters: default,
Expand Down Expand Up @@ -174,6 +177,7 @@ protected SyntaxNode CreatePropertyDeclaration(SyntaxNode containerNode, string
modifiers: new DeclarationModifiers(),
returnType: null,
refKind: RefKind.None,
isInitOnly: false,
explicitInterfaceImplementations: default,
name: "get_" + name,
typeParameters: default,
Expand All @@ -190,6 +194,7 @@ protected SyntaxNode CreatePropertyDeclaration(SyntaxNode containerNode, string
modifiers: new DeclarationModifiers(),
returnType: null,
refKind: RefKind.None,
isInitOnly: false,
explicitInterfaceImplementations: default,
name: "set_" + name,
typeParameters: default,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,8 @@ internal static IMethodSymbol CreateAccessorSymbol(
accessor.TypeParameters,
accessor.Parameters,
statements,
returnTypeAttributes: accessor.GetReturnTypeAttributes());
returnTypeAttributes: accessor.GetReturnTypeAttributes(),
methodKind: accessor.MethodKind);
}

/// <summary>
Expand Down Expand Up @@ -478,7 +479,8 @@ internal static IMethodSymbol CreateMethodSymbol(
method.TypeParameters,
parameters ?? method.Parameters,
statements,
returnTypeAttributes: returnTypeAttributes.HasValue ? returnTypeAttributes.Value : method.GetReturnTypeAttributes());
returnTypeAttributes: returnTypeAttributes.HasValue ? returnTypeAttributes.Value : method.GetReturnTypeAttributes(),
methodKind: method.MethodKind);
}

internal static IPropertySymbol CreatePropertySymbol(
Expand Down

0 comments on commit 0bfc351

Please sign in to comment.