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

Restored requirement for struct constructors to always have formal param... #1106

Merged
merged 1 commit into from
Mar 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 0 additions & 18 deletions src/Compilers/CSharp/Portable/CSharpResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1550,9 +1550,6 @@ If such a class is used as a base class and if the deriving class defines a dest
<data name="ERR_EnumsCantContainDefaultConstructor" xml:space="preserve">
<value>Enums cannot contain explicit parameterless constructors</value>
</data>
<data name="ERR_ParameterlessStructCtorsMustBePublic" xml:space="preserve">
<value>Parameterless instance constructors in structs must be public</value>
</data>
<data name="ERR_CantOverrideBogusMethod" xml:space="preserve">
<value>'{0}': cannot override '{1}' because it is not supported by the language</value>
</data>
Expand Down Expand Up @@ -4553,9 +4550,6 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="IDS_FeatureDictionaryInitializer" xml:space="preserve">
<value>dictionary initializer</value>
</data>
<data name="IDS_FeatureStructParameterlessConstructors" xml:space="preserve">
<value>struct instance parameterless constructors</value>
</data>
<data name="ERR_UnclosedExpressionHole" xml:space="preserve">
<value>Missing close delimiter '}' for interpolated expression started with '{'.</value>
</data>
Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1289,7 +1289,7 @@ internal enum ErrorCode
ERR_NullPropagatingOpInExpressionTree = 8072,
WRN_NubExprIsConstBool2 = 8073,
ERR_DictionaryInitializerInExpressionTree = 8074,
ERR_ParameterlessStructCtorsMustBePublic = 8075,
// available: 8075,
ERR_UnclosedExpressionHole = 8076,
ERR_SingleLineCommentInExpressionHole = 8077,
ERR_InsufficientStack = 8078,
Expand Down
3 changes: 1 addition & 2 deletions src/Compilers/CSharp/Portable/Errors/MessageID.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ internal enum MessageID
// IDS_VersionExperimental = MessageBase + 12694,
IDS_FeatureNameof = MessageBase + 12695,
IDS_FeatureDictionaryInitializer = MessageBase + 12696,
IDS_FeatureStructParameterlessConstructors = MessageBase + 12697,
// available: MessageBase + 12697,

IDS_LogoLine1 = MessageBase + 12698,
IDS_LogoLine2 = MessageBase + 12699,
Expand Down Expand Up @@ -158,7 +158,6 @@ internal static LanguageVersion RequiredVersion(this MessageID feature)
case MessageID.IDS_FeatureExpressionBodiedIndexer:
case MessageID.IDS_FeatureNameof:
case MessageID.IDS_FeatureDictionaryInitializer:
case MessageID.IDS_FeatureStructParameterlessConstructors:
case MessageID.IDS_FeatureUsingStatic:
case MessageID.IDS_FeatureInterpolatedStrings:
return LanguageVersion.CSharp6;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2723,13 +2723,9 @@ private static void CheckForStructDefaultConstructors(
{
diagnostics.Add(ErrorCode.ERR_EnumsCantContainDefaultConstructor, m.Locations[0]);
}
else if (m.DeclaredAccessibility != Accessibility.Public)
{
diagnostics.Add(ErrorCode.ERR_ParameterlessStructCtorsMustBePublic, m.Locations[0]);
}
else
{
Binder.CheckFeatureAvailability(m.Locations[0], MessageID.IDS_FeatureStructParameterlessConstructors, diagnostics);
diagnostics.Add(ErrorCode.ERR_StructsCantContainDefaultConstructor, m.Locations[0]);
}
}
}
Expand Down
156 changes: 2 additions & 154 deletions src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenConstructorInitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,158 +300,6 @@ static void Main(string[] args)
CompileAndVerify(text, expectedOutput: expectedOutput);
}


[Fact]
public void ParameterlessConstructorStruct001()
{
var source = @"
class C
{
struct S1
{
public readonly int x;
public S1()
{
x = 42;
}
}

static void Main()
{
var s = new S1();
System.Console.WriteLine(s.x);
}
}
";
CompileAndVerify(source, expectedOutput: "42").
VerifyIL("C.S1..ctor", @"
{
// Code size 9 (0x9)
.maxstack 2
IL_0000: ldarg.0
IL_0001: ldc.i4.s 42
IL_0003: stfld ""int C.S1.x""
IL_0008: ret
}
");
}

[Fact]
public void ParameterlessConstructorStruct002()
{
var source = @"
class C
{
struct S1
{
public readonly int x;
public S1()
{
x = 42;
}

public S1(int a):this()
{
}
}

static void Main()
{
var s = new S1(123);
System.Console.WriteLine(s.x);
}
}
";
CompileAndVerify(source, expectedOutput: "42").
VerifyIL("C.S1..ctor(int)", @"
{
// Code size 7 (0x7)
.maxstack 1
IL_0000: ldarg.0
IL_0001: call ""C.S1..ctor()""
IL_0006: ret
}
");
}

[Fact]
public void ParameterlessConstructorStruct003()
{
var source = @"
class C
{
struct S1
{
public readonly int x;
public S1(): this(42)
{
}

public S1(int a)
{
x = a;
}
}

static void Main()
{
var s = new S1();
System.Console.WriteLine(s.x);
}
}
";
CompileAndVerify(source, expectedOutput: "42").
VerifyIL("C.S1..ctor(int)", @"
{
// Code size 8 (0x8)
.maxstack 2
IL_0000: ldarg.0
IL_0001: ldarg.1
IL_0002: stfld ""int C.S1.x""
IL_0007: ret
}
").
VerifyIL("C.S1..ctor()", @"
{
// Code size 9 (0x9)
.maxstack 2
IL_0000: ldarg.0
IL_0001: ldc.i4.s 42
IL_0003: call ""C.S1..ctor(int)""
IL_0008: ret
}
");
}

[Fact]
public void ParameterlessInstCtorInStructExprTree()
{
var source = @"

using System;
using System.Linq.Expressions;

class C
{
struct S1
{
public int x;
public S1()
{
x = 42;
}
}

static void Main()
{
Expression<Func<S1>> testExpr = () => new S1();
System.Console.Write(testExpr.Compile()().x);
}
}
";
CompileAndVerify(source, additionalRefs: new[] { ExpressionAssemblyRef }, expectedOutput: "42");
}

[Fact]
public void TestInitializerInCtor001()
{
Expand Down Expand Up @@ -496,15 +344,15 @@ public struct S
public int X{get;}
public int Y{get;}

public S()
public S(int dummy)
{
X = 42;
Y = X;
}

public static void Main()
{
S s = new S();
S s = new S(1);
System.Console.WriteLine(s.Y);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1315,7 +1315,7 @@ struct S1
S1 x2 { get; }


public Program()
public Program(int dummy)
{
x.i = 1;
System.Console.WriteLine(x2.ii);
Expand Down Expand Up @@ -1364,7 +1364,7 @@ struct S1
S1 x2 { get; set;}


public Program()
public Program(int dummy)
{
x.i = 1;
System.Console.WriteLine(x2.ii);
Expand Down Expand Up @@ -1413,7 +1413,7 @@ struct S1
S1 x2 { get;}


public Program()
public Program(int dummy)
{
x = new S1();
x.i += 1;
Expand Down Expand Up @@ -1459,7 +1459,7 @@ struct S1
S1 x2 { get;}


public Program()
public Program(int dummy)
{
this = default(Program);

Expand Down
3 changes: 3 additions & 0 deletions src/Compilers/CSharp/Test/Semantic/Semantics/ConstantTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ static void Main(string[] args)
";
var comp = CreateExperimentalCompilationWithMscorlib45(source);
comp.VerifyDiagnostics(
// (10,12): error CS0568: Structs cannot contain explicit parameterless constructors
// public S2()
Diagnostic(ErrorCode.ERR_StructsCantContainDefaultConstructor, "S2").WithLocation(10, 12),
// (26,28): error CS1736: Default parameter value for 's' must be a compile-time constant
// static void Foo(S2 s = new S2())
Diagnostic(ErrorCode.ERR_DefaultValueMustBeConstant, "new S2()").WithArguments("s").WithLocation(26, 28)
Expand Down
10 changes: 5 additions & 5 deletions src/Compilers/CSharp/Test/Semantic/Semantics/StructsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -603,12 +603,12 @@ public struct X1

";
CreateExperimentalCompilationWithMscorlib45(source).VerifyDiagnostics(
// (4,13): error CS8075: Parameterless struct constructors must be public
// private X()
Diagnostic(ErrorCode.ERR_ParameterlessStructCtorsMustBePublic, "X").WithLocation(4, 13),
// (11,5): error CS8075: Parameterless struct constructors must be public
// (11,5): error CS0568: Structs cannot contain explicit parameterless constructors
// X1()
Diagnostic(ErrorCode.ERR_ParameterlessStructCtorsMustBePublic, "X1").WithLocation(11, 5)
Diagnostic(ErrorCode.ERR_StructsCantContainDefaultConstructor, "X1").WithLocation(11, 5),
// (4,13): error CS0568: Structs cannot contain explicit parameterless constructors
// private X()
Diagnostic(ErrorCode.ERR_StructsCantContainDefaultConstructor, "X").WithLocation(4, 13)
);
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/Compilers/CSharp/Test/Symbol/Symbols/Source/PropertyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,15 @@ public void M()
}

").VerifyDiagnostics(
// (27,9): error CS0200: Property or indexer 'S.Ps' cannot be assigned to -- it is read only
// Ps = 5;
Diagnostic(ErrorCode.ERR_AssgReadonlyProp, "Ps").WithArguments("S.Ps").WithLocation(27, 9),
// (24,12): error CS0568: Structs cannot contain explicit parameterless constructors
// public S()
Diagnostic(ErrorCode.ERR_StructsCantContainDefaultConstructor, "S").WithLocation(24, 12),
// (9,9): error CS0200: Property or indexer 'C.Ps' cannot be assigned to -- it is read only
// Ps = 3;
Diagnostic(ErrorCode.ERR_AssgReadonlyProp, "Ps").WithArguments("C.Ps").WithLocation(9, 9),
// (27,9): error CS0200: Property or indexer 'S.Ps' cannot be assigned to -- it is read only
// Ps = 5;
Diagnostic(ErrorCode.ERR_AssgReadonlyProp, "Ps").WithArguments("S.Ps").WithLocation(27, 9),
// (14,9): error CS0200: Property or indexer 'C.P' cannot be assigned to -- it is read only
// P = 10;
Diagnostic(ErrorCode.ERR_AssgReadonlyProp, "P").WithArguments("C.P").WithLocation(14, 9),
Expand All @@ -136,6 +139,7 @@ public void M()
// (33,9): error CS0200: Property or indexer 'S.Ps' cannot be assigned to -- it is read only
// S.Ps = 1;
Diagnostic(ErrorCode.ERR_AssgReadonlyProp, "S.Ps").WithArguments("S.Ps").WithLocation(33, 9)

);
}

Expand Down
Loading