Просмотр исходного кода

* attribute.cs (Attribute.CheckAttributeType): Check for ambiguous
attributes.
Fix for #56456.

* attribute.cs (Attribute.Resolve): Check for duplicate named
attributes.
Fix for #56463.

svn path=/trunk/mcs/; revision=25532

Raja R Harinath 22 лет назад
Родитель
Сommit
d4383ca4a8
5 измененных файлов с 79 добавлено и 16 удалено
  1. 10 0
      mcs/errors/cs0246-7.cs
  2. 13 0
      mcs/errors/cs0643.cs
  3. 11 0
      mcs/errors/cs1614.cs
  4. 10 0
      mcs/mcs/ChangeLog
  5. 35 16
      mcs/mcs/attribute.cs

+ 10 - 0
mcs/errors/cs0246-7.cs

@@ -0,0 +1,10 @@
+// cs0246-7.cs: Neither 'A' nor 'AAttribute' is an attribute class
+// Line: 6
+
+using System;
+public class A {
+	[A]
+	public static void Main() {
+	}
+}
+public class AAttribute {}

+ 13 - 0
mcs/errors/cs0643.cs

@@ -0,0 +1,13 @@
+// cs0643.cs: 'x' duplicate named attribute argument
+// Line: 8
+
+using System;
+
+public class A : Attribute {
+	public int x;
+	[A (x = 1, x = 2)]
+	public static void Main ()
+	{
+	}
+}
+

+ 11 - 0
mcs/errors/cs1614.cs

@@ -0,0 +1,11 @@
+// cs1614.cs: 'A': is ambiguous;  use either '@A' or 'AAttribute'
+// Line: 6
+// Bug #56456
+
+using System;
+public class A : Attribute {
+	[A]
+	public static void Main() {
+	}
+}
+public class AAttribute : Attribute {}

+ 10 - 0
mcs/mcs/ChangeLog

@@ -1,3 +1,13 @@
+2004-04-15  Raja R Harinath  <[email protected]>
+
+	* attribute.cs (Attribute.CheckAttributeType): Check for ambiguous
+	attributes.
+	Fix for #56456.
+
+	* attribute.cs (Attribute.Resolve): Check for duplicate named
+	attributes.
+	Fix for #56463.
+
 2004-04-15  Miguel de Icaza  <[email protected]>
 
 	* iterators.cs (MarkYield): track whether we are in an exception,

+ 35 - 16
mcs/mcs/attribute.cs

@@ -81,28 +81,39 @@ namespace Mono.CSharp {
                 ///   Tries to resolve the type of the attribute. Flags an error if it can't.
                 /// </summary>
 		private Type CheckAttributeType (EmitContext ec) {
-			Type t;
-			bool isattributeclass = true;
-
-			t = RootContext.LookupType (ec.DeclSpace, Name, true, Location);
-			if (t != null) {
-				isattributeclass = t.IsSubclassOf (TypeManager.attribute_type);
-				if (isattributeclass)
-					return t;
+			Type t1 = RootContext.LookupType (ec.DeclSpace, Name, true, Location);
+
+			// FIXME: Shouldn't do this for quoted attributes: [@A]
+			Type t2 = RootContext.LookupType (ec.DeclSpace, Name + "Attribute", true, Location);
+
+			String err0616 = null;
+
+			if (t1 != null && ! t1.IsSubclassOf (TypeManager.attribute_type)) {
+				t1 = null;
+				err0616 = "'" + Name + "': is not an attribute class";
 			}
-			t = RootContext.LookupType (ec.DeclSpace, Name + "Attribute", true, Location);
-			if (t != null) {
-				if (t.IsSubclassOf (TypeManager.attribute_type))
-					return t;
+			if (t2 != null && ! t2.IsSubclassOf (TypeManager.attribute_type)) {
+				t2 = null;
+				err0616 = (err0616 != null) 
+					? "Neither '" + Name + "' nor '" + Name + "Attribute' is an attribute class"
+					: "'" + Name + "Attribute': is not an attribute class";
 			}
-			if (!isattributeclass) {
-				Report.Error (616, Location, "'" + Name + "': is not an attribute class");
+
+			if (t1 != null && t2 != null) {
+				Report.Error(1614, Location, "'" + Name + "': is ambiguous; " 
+					     + " use either '@" + Name + "' or '" + Name + "Attribute'");
 				return null;
 			}
-			if (t != null) {
-				Report.Error (616, Location, "'" + Name + "Attribute': is not an attribute class");
+			if (t1 != null)
+				return t1;
+			if (t2 != null)
+				return t2;
+
+			if (err0616 != null) {
+				Report.Error (616, Location, err0616);
 				return null;
 			}
+			
 			Report.Error (
 				246, Location, "Could not find attribute '" + Name + "' (are you" +
 				" missing a using directive or an assembly reference ?)");
@@ -258,12 +269,20 @@ namespace Mono.CSharp {
 				field_values = new ArrayList ();
 				prop_values = new ArrayList ();
 			}
+
+			Hashtable seen_names = new Hashtable();
 			
 			for (i = 0; i < named_args.Count; i++) {
 				DictionaryEntry de = (DictionaryEntry) named_args [i];
 				string member_name = (string) de.Key;
 				Argument a  = (Argument) de.Value;
 				Expression e;
+
+				if (seen_names.Contains(member_name)) {
+					Report.Error(643, Location, "'" + member_name + "' duplicate named attribute argument");
+					return null;
+				}				
+				seen_names.Add(member_name, 1);
 				
 				if (!a.Resolve (ec, Location))
 					return null;