Skip to content

Commit 221b9c9

Browse files
committed
[mcs] Initializers with parameters scope and primary constructors
1 parent 16a37db commit 221b9c9

File tree

7 files changed

+128
-7
lines changed

7 files changed

+128
-7
lines changed

mcs/errors/cs0136-18.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// CS0136: A local variable named `arg' cannot be declared in this scope because it would give a different meaning to `arg', which is already used in a `parent or current' scope to denote something else
2+
// Line: 11
3+
4+
using System;
5+
6+
class A (Func<int, int> barg)
7+
{
8+
}
9+
10+
class B (int arg)
11+
: A ((arg) => 1)
12+
{
13+
}

mcs/errors/cs0136-19.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// CS0136: A local variable named `arg' cannot be declared in this scope because it would give a different meaning to `arg', which is already used in a `parent or current' scope to denote something else
2+
// Line: 11
3+
4+
using System;
5+
6+
partial class PC
7+
{
8+
Func<int, int> f = (arg) => 1;
9+
}
10+
11+
partial class PC (int arg)
12+
{
13+
}

mcs/mcs/anonymous.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,16 +1321,28 @@ protected virtual ParametersCompiled ResolveParameters (ResolveContext ec, TypeI
13211321
return Parameters;
13221322
}
13231323

1324-
protected override Expression DoResolve (ResolveContext ec)
1324+
protected override Expression DoResolve (ResolveContext rc)
13251325
{
1326-
if (ec.HasSet (ResolveContext.Options.ConstantScope)) {
1327-
ec.Report.Error (1706, loc, "Anonymous methods and lambda expressions cannot be used in the current context");
1326+
if (rc.HasSet (ResolveContext.Options.ConstantScope)) {
1327+
rc.Report.Error (1706, loc, "Anonymous methods and lambda expressions cannot be used in the current context");
13281328
return null;
13291329
}
13301330

13311331
//
1332-
// Set class type, set type
1332+
// Update top-level block generated duting parsing with actual top-level block
13331333
//
1334+
if (rc.HasAny (ResolveContext.Options.FieldInitializerScope | ResolveContext.Options.BaseInitializer)) {
1335+
var tb = rc.ConstructorBlock.ParametersBlock.TopBlock;
1336+
if (Block.TopBlock != tb) {
1337+
Block b = Block;
1338+
while (b.Parent != Block.TopBlock && b != Block.TopBlock)
1339+
b = b.Parent;
1340+
1341+
b.Parent = tb;
1342+
tb.IncludeBlock (Block.TopBlock);
1343+
b.ParametersBlock.TopBlock = tb;
1344+
}
1345+
}
13341346

13351347
eclass = ExprClass.Value;
13361348

@@ -1341,7 +1353,7 @@ protected override Expression DoResolve (ResolveContext ec)
13411353
//
13421354
type = InternalType.AnonymousMethod;
13431355

1344-
if (!DoResolveParameters (ec))
1356+
if (!DoResolveParameters (rc))
13451357
return null;
13461358

13471359
return this;

mcs/mcs/class.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,8 +1608,12 @@ bool DoDefineBaseType ()
16081608
// defined after current container
16091609
//
16101610
if (class_partial_parts != null) {
1611-
foreach (var pp in class_partial_parts)
1611+
foreach (var pp in class_partial_parts) {
1612+
if (pp.PrimaryConstructorBaseArguments != null)
1613+
PrimaryConstructorBaseArguments = pp.PrimaryConstructorBaseArguments;
1614+
16121615
pp.DoDefineBaseType ();
1616+
}
16131617

16141618
}
16151619

@@ -2665,7 +2669,7 @@ protected virtual Constructor DefineDefaultConstructor (bool is_static)
26652669

26662670
AddConstructor (c, true);
26672671
if (PrimaryConstructorBlock == null) {
2668-
c.Block = new ToplevelBlock (Compiler, c.ParameterInfo, Location) {
2672+
c.Block = new ToplevelBlock (Compiler, parameters, Location) {
26692673
IsCompilerGenerated = true
26702674
};
26712675
} else {

mcs/mcs/statement.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3673,6 +3673,9 @@ public ToplevelBlock TopBlock {
36733673
get {
36743674
return top_block;
36753675
}
3676+
set {
3677+
top_block = value;
3678+
}
36763679
}
36773680

36783681
public bool Resolved {
@@ -4312,6 +4315,21 @@ public bool GetLocalName (string name, Block block, ref INamedBlockVariable vari
43124315
return false;
43134316
}
43144317

4318+
public void IncludeBlock (ToplevelBlock block)
4319+
{
4320+
if (block.names != null) {
4321+
foreach (var n in block.names) {
4322+
var variable = n.Value as INamedBlockVariable;
4323+
if (variable != null) {
4324+
AddLocalName (n.Key, variable, false);
4325+
continue;
4326+
}
4327+
4328+
throw new NotImplementedException ();
4329+
}
4330+
}
4331+
}
4332+
43154333
// <summary>
43164334
// This is used by non-static `struct' constructors which do not have an
43174335
// initializer - in this case, the constructor must initialize all of the

mcs/tests/test-primary-ctor-09.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
3+
class A (Func<int, int> barg)
4+
{
5+
public Func<int, int> BaseArg = barg;
6+
}
7+
8+
partial class PC
9+
{
10+
public Func<int, int> f1 = (a) => arg;
11+
}
12+
13+
partial class PC (int arg)
14+
: A ((a) => arg)
15+
{
16+
}
17+
18+
class X
19+
{
20+
public static int Main ()
21+
{
22+
if (new PC (3).f1 (4) != 3)
23+
return 1;
24+
25+
if (new PC (3).BaseArg (4) != 3)
26+
return 2;
27+
28+
return 0;
29+
}
30+
}

mcs/tests/ver-il-net_4_5.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67997,6 +67997,37 @@
6799767997
</method>
6799867998
</type>
6799967999
</test>
68000+
<test name="test-primary-ctor-09.cs">
68001+
<type name="A">
68002+
<method name="Void .ctor(Func`2)" attrs="6278">
68003+
<size>14</size>
68004+
</method>
68005+
</type>
68006+
<type name="PC">
68007+
<method name="Void .ctor(Int32)" attrs="6278">
68008+
<size>50</size>
68009+
</method>
68010+
</type>
68011+
<type name="X">
68012+
<method name="Int32 Main()" attrs="150">
68013+
<size>70</size>
68014+
</method>
68015+
<method name="Void .ctor()" attrs="6278">
68016+
<size>7</size>
68017+
</method>
68018+
</type>
68019+
<type name="PC+&lt;f1&gt;c__AnonStorey0">
68020+
<method name="Int32 &lt;&gt;m__0(Int32)" attrs="131">
68021+
<size>14</size>
68022+
</method>
68023+
<method name="Int32 &lt;&gt;m__1(Int32)" attrs="131">
68024+
<size>14</size>
68025+
</method>
68026+
<method name="Void .ctor()" attrs="6278">
68027+
<size>7</size>
68028+
</method>
68029+
</type>
68030+
</test>
6800068031
<test name="test-static-using-01.cs">
6800168032
<type name="A.B.X">
6800268033
<method name="Int32 Test()" attrs="150">

0 commit comments

Comments
 (0)