--- /dev/null
+// CS1644: Feature `primary constructor' cannot be used because it is not part of the C# 5.0 language specification
+// Line: 7
+// Compiler options: -langversion:5
+
+class C (int arg)
+{
+}
\ No newline at end of file
--- /dev/null
+// CS9001: Only one part of a partial type can declare primary constructor parameters
+// Line: 8
+
+partial class Part(int arg)
+{
+}
+
+partial class Part(int arg)
+{
+}
\ No newline at end of file
--- /dev/null
+// CS9002: `S.S(long)': Instance constructor of type with primary constructor must specify `this' constructor initializer
+// Line: 6
+
+class S (int arg)
+{
+ public S (long l)
+ {
+ }
+}
--- /dev/null
+// CS9003: Primary constructor of type `Test<T>' has parameter of same name as type parameter `T'
+// Line: 4
+
+class Test<T>(T T)
+{
+}
--- /dev/null
+// CS9004: Primary constructor of type `Test' has parameter of same name as containing type
+// Line: 4
+
+class Test(object Test)
+{
+}
\ No newline at end of file
--- /dev/null
+// CS8043: `S.S(long)': Structs with primary constructor cannot specify default constructor initializer
+// Line: 6
+
+struct S (int x)
+{
+ public S (long x)
+ : this ()
+ {
+ }
+}
--- /dev/null
+// CS8049: Implemented interfaces cannot have arguments
+// Line: 6
+
+using System;
+
+class ID () : IDisposable ()
+{
+ public void Dispose ()
+ {
+ }
+}
+++ /dev/null
-// CS9001: Only one part of a partial type can declare primary constructor parameters
-// Line: 8
-
-partial class Part(int arg)
-{
-}
-
-partial class Part(int arg)
-{
-}
\ No newline at end of file
+++ /dev/null
-// CS9002: `S.S(long)': Instance constructor of type with primary constructor must specify `this' constructor initializer
-// Line: 6
-
-class S (int arg)
-{
- public S (long l)
- {
- }
-}
+++ /dev/null
-// CS9003: Primary constructor of type `Test<T>' has parameter of same name as type parameter `T'
-// Line: 4
-
-class Test<T>(T T)
-{
-}
+++ /dev/null
-// CS9004: Primary constructor of type `Test' has parameter of same name as containing type
-// Line: 4
-
-class Test(object Test)
-{
-}
\ No newline at end of file
public override void FlowAnalysis (FlowAnalysisContext fc)
{
source.FlowAnalysis (fc);
+ ((FieldExpr) target).SetFieldAssigned (fc);
}
public bool IsDefaultInitializer {
public ParametersCompiled PrimaryConstructorParameters { get; set; }
+ public Arguments PrimaryConstructorBaseArguments { get; set; }
+
+ public Location PrimaryConstructorBaseArgumentsStart { get; set; }
+
public TypeParameters TypeParametersAll {
get {
return all_type_parameters;
Report.Error (692, symbol.Location,
"Duplicate type parameter `{0}'", symbol.GetSignatureForError ());
} else if (symbol is PrimaryConstructorField && mc is TypeParameter) {
- Report.Error (9003, symbol.Location, "Primary constructor of type `{0}' has parameter of same name as type parameter `{1}'",
+ Report.Error (8038, symbol.Location, "Primary constructor of type `{0}' has parameter of same name as type parameter `{1}'",
symbol.Parent.GetSignatureForError (), symbol.GetSignatureForError ());
} else {
Report.Error (102, symbol.Location,
if (PrimaryConstructorParameters != null) {
if (PartialContainer.PrimaryConstructorParameters != null) {
- Report.Error (9001, Location, "Only one part of a partial type can declare primary constructor parameters");
+ Report.Error (8036, Location, "Only one part of a partial type can declare primary constructor parameters");
} else {
PartialContainer.PrimaryConstructorParameters = PrimaryConstructorParameters;
}
}
if (iface_exprs != null) {
+ if (!PrimaryConstructorBaseArgumentsStart.IsNull) {
+ Report.Error (8049, PrimaryConstructorBaseArgumentsStart, "Implemented interfaces cannot have arguments");
+ }
+
foreach (var iface_type in iface_exprs) {
// Prevents a crash, the interface might not have been resolved: 442144
if (iface_type == null)
{
}
- public Arguments PrimaryConstructorBaseArguments { get; set; }
-
protected override TypeAttributes TypeAttr {
get {
TypeAttributes ta = base.TypeAttr;
}
if (symbol is PrimaryConstructorField) {
- Report.Error (9004, symbol.Location, "Primary constructor of type `{0}' has parameter of same name as containing type",
+ Report.Error (8039, symbol.Location, "Primary constructor of type `{0}' has parameter of same name as containing type",
symbol.Parent.GetSignatureForError ());
return;
}
// The default static constructor is private
Modifiers mods;
+ ParametersCompiled parameters = null;
if (is_static) {
mods = Modifiers.STATIC | Modifiers.PRIVATE;
+ parameters = ParametersCompiled.EmptyReadOnlyParameters;
} else {
mods = ((ModFlags & Modifiers.ABSTRACT) != 0) ? Modifiers.PROTECTED : Modifiers.PUBLIC;
+ parameters = PrimaryConstructorParameters ?? ParametersCompiled.EmptyReadOnlyParameters;
}
- var c = new Constructor (this, MemberName.Name, mods, null, PrimaryConstructorParameters ?? ParametersCompiled.EmptyReadOnlyParameters, Location);
+ var c = new Constructor (this, MemberName.Name, mods, null, parameters, Location);
if (Kind == MemberKind.Class)
c.Initializer = new GeneratedBaseInitializer (Location, PrimaryConstructorBaseArguments);
- if (PrimaryConstructorParameters != null)
+ if (PrimaryConstructorParameters != null && !is_static)
c.IsPrimaryConstructor = true;
AddConstructor (c, true);
// Cannot use opt_formal_parameter_list because it can be shared instance for empty parameters
lbag.AppendToMember (current_container, GetLocation ($1), GetLocation ($3));
+
+ if (lang_version < LanguageVersion.V_6)
+ FeatureIsNotAvailable (GetLocation ($1), "primary constructor");
}
;
{
$$ = $1;
}
+ | primary_parameters class_base
+ {
+ $$ = $1;
+ }
| primary_parameters class_base OPEN_PARENS
{
++lexer.parsing_block;
+ current_type.PrimaryConstructorBaseArgumentsStart = GetLocation ($3);
}
opt_argument_list CLOSE_PARENS
{
- lbag.AppendToMember (current_container, GetLocation ($3), GetLocation ($6));
- ((Class)current_type).PrimaryConstructorBaseArguments = (Arguments) $5;
+ lbag.AppendToMember (current_container, GetLocation ($6));
+ current_type.PrimaryConstructorBaseArguments = (Arguments) $5;
--lexer.parsing_block;
$$ = $1;
: base (loc)
{
this.type = type;
- eclass = ExprClass.Variable;
}
- protected override Expression DoResolve (ResolveContext ec)
+ protected override Expression DoResolve (ResolveContext rc)
{
+ eclass = ExprClass.Variable;
+
+ var block = rc.CurrentBlock;
+ if (block != null) {
+ var top = block.ParametersBlock.TopBlock;
+ if (top.ThisVariable != null)
+ variable_info = top.ThisVariable.VariableInfo;
+
+ }
+
return this;
}
+ public override Expression DoResolveLValue (ResolveContext rc, Expression right_side)
+ {
+ return DoResolve (rc);
+ }
+
public override HoistedVariable GetHoistedVariable (AnonymousExpression ae)
{
return null;
}
}
- VariableInfo variable_info;
+ protected VariableInfo variable_info;
public This (Location loc)
{
}
public bool IsPrimaryConstructor { get; set; }
-
MethodBase IMethodDefinition.Metadata {
get {
if (!CheckBase ())
return false;
- if (Parent.PrimaryConstructorParameters != null && !IsPrimaryConstructor) {
- if (Parent.Kind == MemberKind.Struct) {
- Report.Error (9009, Location, "`{0}': Structs with primary constructor cannot have explicit constructor",
+ if (Parent.PrimaryConstructorParameters != null && !IsPrimaryConstructor && !IsStatic) {
+ if (Parent.Kind == MemberKind.Struct && Initializer is ConstructorThisInitializer && Initializer.Arguments == null) {
+ Report.Error (8043, Location, "`{0}': Structs with primary constructor cannot specify default constructor initializer",
GetSignatureForError ());
} else if (Initializer == null || Initializer is ConstructorBaseInitializer) {
- Report.Error (9002, Location, "`{0}': Instance constructor of type with primary constructor must specify `this' constructor initializer",
+ Report.Error (8037, Location, "`{0}': Instance constructor of type with primary constructor must specify `this' constructor initializer",
GetSignatureForError ());
}
}
bc.Set (ResolveContext.Options.ConstructorScope);
if (block != null) {
+ if (!IsStatic && Initializer == null && Parent.PartialContainer.Kind == MemberKind.Struct) {
+ //
+ // If this is a non-static `struct' constructor and doesn't have any
+ // initializer, it must initialize all of the struct's fields.
+ //
+ block.AddThisVariable (bc);
+ }
+
//
// If we use a "this (...)" constructor initializer, then
// do not emit field initializers, they are initialized in the other constructor
Parent.PartialContainer.ResolveFieldInitializers (bc);
if (!IsStatic) {
- if (Initializer == null) {
- if (Parent.PartialContainer.Kind == MemberKind.Struct) {
- //
- // If this is a non-static `struct' constructor and doesn't have any
- // initializer, it must initialize all of the struct's fields.
- //
- block.AddThisVariable (bc);
- } else if (Parent.PartialContainer.Kind == MemberKind.Class) {
- Initializer = new GeneratedBaseInitializer (Location, null);
- }
+ if (Initializer == null && Parent.PartialContainer.Kind == MemberKind.Class) {
+ Initializer = new GeneratedBaseInitializer (Location, null);
}
if (Initializer != null) {
expr = Child;
if (!(expr is Constant || expr is DefaultValueExpression || (expr is New && ((New) expr).IsDefaultStruct))) {
- rc.Report.Error (1736, Location,
- "The expression being assigned to optional parameter `{0}' must be a constant or default value",
- p.Name);
+ if (!(expr is ErrorExpression)) {
+ rc.Report.Error (1736, Location,
+ "The expression being assigned to optional parameter `{0}' must be a constant or default value",
+ p.Name);
+ }
return;
}
--- /dev/null
+using System;
+
+class ID () : IDisposable
+{
+ void IDisposable.Dispose ()
+ {
+ }
+}
+
+class X
+{
+ public static void Main ()
+ {
+ }
+}
\ No newline at end of file
--- /dev/null
+using System;
+
+struct S (int x)
+{
+ public int y = x;
+
+ public S (char x)
+ : this (1)
+ {
+ }
+
+ static S ()
+ {
+ }
+}
+
+class X
+{
+ public static int Main ()
+ {
+ if (new S (-5).y != -5)
+ return 1;
+
+ if (new S ('x').y != 1)
+ return 2;
+
+ return 0;
+ }
+}
\ No newline at end of file
</method>\r
</type>\r
</test>\r
+ <test name="test-primary-ctor-06.cs">\r
+ <type name="ID">\r
+ <method name="Void System.IDisposable.Dispose()" attrs="481">\r
+ <size>2</size>\r
+ </method>\r
+ <method name="Void .ctor()" attrs="6278">\r
+ <size>7</size>\r
+ </method>\r
+ </type>\r
+ <type name="X">\r
+ <method name="Void Main()" attrs="150">\r
+ <size>2</size>\r
+ </method>\r
+ <method name="Void .ctor()" attrs="6278">\r
+ <size>7</size>\r
+ </method>\r
+ </type>\r
+ </test>\r
+ <test name="test-primary-ctor-07.cs">\r
+ <type name="S">\r
+ <method name="Void .ctor(Char)" attrs="6278">\r
+ <size>9</size>\r
+ </method>\r
+ <method name="Void .cctor()" attrs="6289">\r
+ <size>2</size>\r
+ </method>\r
+ <method name="Void .ctor(Int32)" attrs="6278">\r
+ <size>15</size>\r
+ </method>\r
+ </type>\r
+ <type name="X">\r
+ <method name="Int32 Main()" attrs="150">\r
+ <size>69</size>\r
+ </method>\r
+ <method name="Void .ctor()" attrs="6278">\r
+ <size>7</size>\r
+ </method>\r
+ </type>\r
+ </test>\r
<test name="test-static-using-01.cs">\r
<type name="A.B.X">\r
<method name="Int32 Test()" attrs="150">\r