Browse Source

2006-04-26 Marek Safar <[email protected]>

	A fix for #78115
	anonymous.cs.cs (AnonymousMethod.DoResolve): Moved the check whether
	anonymous method  is allowed from AnonymousContainer here.

	* attribute.cs, codegen.cs (EmitContext): Add IsAnonymousMethodAllowed.

svn path=/trunk/mcs/; revision=59942
Marek Safar 19 years ago
parent
commit
a4202659e2

+ 18 - 0
mcs/errors/cs1706-2.cs

@@ -0,0 +1,18 @@
+// cs1706-2.cs: Anonymous methods are not allowed in the attribute declaration
+// Line: 14
+
+public delegate void Proc();
+
+public class AAttribute : System.Attribute
+{
+	public AAttribute(Proc p)
+	{ }
+}
+
+public class Class
+{
+	[A((object)delegate { return; })]
+	public void Foo()
+	{
+	}
+} 

+ 1 - 1
mcs/errors/cs1706.cs

@@ -1,4 +1,4 @@
-// cs1706.cs: Anonymous methods are not allowed in attribute declaration
+// cs1706.cs: Anonymous methods are not allowed in the attribute declaration
 // Line: 13
 
 using System;

+ 8 - 0
mcs/gmcs/ChangeLog

@@ -1,3 +1,11 @@
+2006-04-26  Marek Safar  <[email protected]>
+
+	A fix for #78115
+	anonymous.cs.cs (AnonymousMethod.DoResolve): Moved the check whether
+	anonymous method  is allowed from AnonymousContainer here.
+
+	* attribute.cs, codegen.cs (EmitContext): Add IsAnonymousMethodAllowed.
+
 2006-04-24  Raja R Harinath  <[email protected]>
 
 	Fix #78156

+ 5 - 1
mcs/gmcs/anonymous.cs

@@ -83,7 +83,6 @@ namespace Mono.CSharp {
 			// The order is important: this setups the CaptureContext tree hierarchy.
 			//
 			if (container == null) {
-				Report.Error (1706, l, "Anonymous methods are not allowed in attribute declaration");
 				return;
 			}
 			container.SetHaveAnonymousMethods (l, this);
@@ -430,6 +429,11 @@ namespace Mono.CSharp {
 
 		public override Expression DoResolve (EmitContext ec)
 		{
+			if (!ec.IsAnonymousMethodAllowed) {
+				Report.Error (1706, loc, "Anonymous methods are not allowed in the attribute declaration");
+				return null;
+			}
+
 			if (Parameters != null && !Parameters.Resolve (ec)) {
 				return null;
 			}

+ 1 - 0
mcs/gmcs/attribute.cs

@@ -337,6 +337,7 @@ namespace Mono.CSharp {
 			Attributable owner = Owner;
 			EmitContext ec = new EmitContext (owner.ResolveContext, owner.ResolveContext.DeclContainer, owner.ResolveContext.DeclContainer,
 				Location, null, null, owner.ResolveContext.DeclContainer.ModFlags, false);
+			ec.IsAnonymousMethodAllowed = false;
 
 			ConstructorInfo ctor = ResolveConstructor (ec);
 			if (ctor == null) {

+ 11 - 1
mcs/gmcs/codegen.cs

@@ -372,7 +372,9 @@ namespace Mono.CSharp {
 			Resolving,
 			Emitting
 		}
-		
+
+		bool isAnonymousMethodAllowed = true;
+
 		Phase current_phase;
 		FlowBranching current_flow_branching;
 
@@ -444,6 +446,14 @@ namespace Mono.CSharp {
 			get { return InUnsafe || ResolveContext.IsInUnsafeScope; }
 		}
 
+		public bool IsAnonymousMethodAllowed {
+			get {
+				return isAnonymousMethodAllowed;
+			}
+			set {
+				isAnonymousMethodAllowed = value;
+			}
+		}
 
 		public FlowBranching CurrentBranching {
 			get { return current_flow_branching; }

+ 8 - 0
mcs/mcs/ChangeLog

@@ -1,3 +1,11 @@
+2006-04-26  Marek Safar  <[email protected]>
+
+	A fix for #78115
+	anonymous.cs.cs (AnonymousMethod.DoResolve): Moved the check whether
+	anonymous method  is allowed from AnonymousContainer here.
+
+	* attribute.cs, codegen.cs (EmitContext): Add IsAnonymousMethodAllowed.
+
 2006-04-24  Raja R Harinath  <[email protected]>
 
 	Fix #78156

+ 5 - 1
mcs/mcs/anonymous.cs

@@ -81,7 +81,6 @@ namespace Mono.CSharp {
 			// The order is important: this setups the CaptureContext tree hierarchy.
 			//
 			if (container == null) {
-				Report.Error (1706, l, "Anonymous methods are not allowed in attribute declaration");
 				return;
 			}
 			container.SetHaveAnonymousMethods (l, this);
@@ -384,6 +383,11 @@ namespace Mono.CSharp {
 
 		public override Expression DoResolve (EmitContext ec)
 		{
+			if (!ec.IsAnonymousMethodAllowed) {
+				Report.Error (1706, loc, "Anonymous methods are not allowed in the attribute declaration");
+				return null;
+			}
+
 			if (Parameters != null && !Parameters.Resolve (ec)) {
 				return null;
 			}

+ 1 - 0
mcs/mcs/attribute.cs

@@ -331,6 +331,7 @@ namespace Mono.CSharp {
 			Attributable owner = Owner;
 			EmitContext ec = new EmitContext (owner.ResolveContext, owner.ResolveContext.DeclContainer, owner.ResolveContext.DeclContainer,
 				Location, null, null, owner.ResolveContext.DeclContainer.ModFlags, false);
+			ec.IsAnonymousMethodAllowed = false;
 
 			ConstructorInfo ctor = ResolveConstructor (ec);
 			if (ctor == null)

+ 11 - 1
mcs/mcs/codegen.cs

@@ -361,7 +361,9 @@ namespace Mono.CSharp {
 			Resolving,
 			Emitting
 		}
-		
+
+		bool isAnonymousMethodAllowed = true;
+
 		Phase current_phase;
 		FlowBranching current_flow_branching;
 
@@ -430,6 +432,14 @@ namespace Mono.CSharp {
 			get { return InUnsafe || ResolveContext.IsInUnsafeScope; }
 		}
 
+		public bool IsAnonymousMethodAllowed {
+			get {
+				return isAnonymousMethodAllowed;
+			}
+			set {
+				isAnonymousMethodAllowed = value;
+			}
+		}
 
 		public FlowBranching CurrentBranching {
 			get { return current_flow_branching; }

+ 14 - 0
mcs/tests/test-anon-37.cs

@@ -0,0 +1,14 @@
+using System;
+
+public class DelegateInit {
+    public delegate void FooDelegate();
+
+    public static readonly FooDelegate _print =
+        delegate() {
+            Console.WriteLine("delegate!");
+        };
+
+    public static void Main(string[] args) {
+        _print();
+    }
+}