[mcs] Set builder for partial method definition attributes. Fixes #31948
authorMarek Safar <marek.safar@gmail.com>
Tue, 21 Jul 2015 09:10:38 +0000 (11:10 +0200)
committerMarek Safar <marek.safar@gmail.com>
Tue, 21 Jul 2015 09:11:48 +0000 (11:11 +0200)
mcs/mcs/attribute.cs
mcs/mcs/method.cs
mcs/tests/test-partial-34.cs [new file with mode: 0644]
mcs/tests/ver-il-net_4_x.xml

index 6c210605966226bef9e6ac57517ac6178f40b2ed..2aa793e79658681e2a6a9879ee60a024ea626d69 100644 (file)
@@ -282,6 +282,11 @@ namespace Mono.CSharp {
                        }
                }
 
+               public void SetOwner (Attributable owner)
+               {
+                       targets [0] = owner;
+               }
+
                /// <summary>
                ///   Tries to resolve the type of the attribute. Flags an error if it can't, and complain is true.
                /// </summary>
@@ -1223,6 +1228,19 @@ namespace Mono.CSharp {
                        Attrs.AddRange (attrs);
                }
 
+               public static void AttachFromPartial (Attributable target, Attributable partialSrc)
+               {
+                       if (target.OptAttributes == null) {
+                               target.OptAttributes = partialSrc.OptAttributes;
+                       } else {
+                               target.OptAttributes.Attrs.AddRange (partialSrc.OptAttributes.Attrs);
+                       }
+
+                       foreach (var attr in partialSrc.OptAttributes.Attrs) {
+                               attr.SetOwner (target);
+                       }
+               }
+
                public void AttachTo (Attributable attributable, IMemberContext context)
                {
                        foreach (Attribute a in Attrs)
index 6835ec32b061d3c35ffdbc90a85e1919c49ae7e7..6033cf47de3f0cf54054dc676c1f8f3c63629d36 100644 (file)
@@ -1411,11 +1411,7 @@ namespace Mono.CSharp {
                                p.Name = md_p.Name;
                                p.DefaultValue = md_p.DefaultValue;
                                if (md_p.OptAttributes != null) {
-                                       if (p.OptAttributes == null) {
-                                               p.OptAttributes = md_p.OptAttributes;
-                                       } else {
-                                               p.OptAttributes.Attrs.AddRange (md_p.OptAttributes.Attrs);
-                                       }
+                                       Attributes.AttachFromPartial (p, md_p);
                                }
                        }
 
diff --git a/mcs/tests/test-partial-34.cs b/mcs/tests/test-partial-34.cs
new file mode 100644 (file)
index 0000000..75cff9e
--- /dev/null
@@ -0,0 +1,69 @@
+using System;
+using CustomAttributes;
+
+partial class A
+{
+       // Partial methods w/o attributes.
+       partial void PartialMethodWith_NoAttr_NoDefn(string s);
+       partial void PartialMethodWith_NoAttr_Decl(string s);
+
+       // Partial methods w/o a definition.
+       [AttributeA("ANoDef")]
+       partial void PartialMethodWith_AAttr_NoDefn(string s);
+       partial void PartialMethodWith_BAttr_NoDefn([AttributeB("BNoDef")]string s);
+
+       // Attributes only on declaration.
+       [AttributeA("ADecl")]
+       partial void PartialMethodWith_AAttr_Decl(string s);
+       partial void PartialMethodWith_BAttr_Decl([AttributeB("BDecl")]string s);
+
+       // Attributes only on definition.
+       partial void PartialMethodWith_AAttr_Defn(string s);
+       partial void PartialMethodWith_BAttr_Defn(string s);
+
+       // Different Attribute on definition.
+       [AttributeA("WithABAttr")]
+       partial void PartialMethodWith_ABAttr(string s);
+       partial void PartialMethodWith_BAAttr([AttributeB("WithBAAttr")]string s);
+}
+
+partial class A
+{
+       // Partial methods w/o attributes.
+       partial void PartialMethodWith_NoAttr_Decl(string s) { }
+
+       // Attributes only on declaration.
+       partial void PartialMethodWith_AAttr_Decl(string s) { }
+       partial void PartialMethodWith_BAttr_Decl(string s) { }
+
+       // Attributes only on definition.
+       [AttributeA("ADefn")]
+       partial void PartialMethodWith_AAttr_Defn(string s) { }
+       partial void PartialMethodWith_BAttr_Defn([AttributeB("BDefn")]string s)
+       {
+       }
+
+       // Different Attribute on definition.
+       [AttributeB("ABAttr")]
+       partial void PartialMethodWith_ABAttr(string s) { }
+       partial void PartialMethodWith_BAAttr([AttributeA("BAAttr")]string s) { }
+}
+
+namespace CustomAttributes {
+       [AttributeUsage(AttributeTargets.All, AllowMultiple=true)]
+       public class AttributeA : Attribute {
+               public AttributeA(String a) {}
+       }
+
+       [AttributeUsage(AttributeTargets.All, AllowMultiple=true)]
+       public class AttributeB : Attribute {
+               public AttributeB(String a) {}
+       }
+}
+
+class X
+{
+       public static void Main ()
+       {
+       }
+}
\ No newline at end of file
index 7c52c50b54d227b2f4ea811c11d87ce664effeef..477a79ff1487aa06c764cc299f52e63a88394aa4 100644 (file)
       </method>
     </type>
   </test>
+  <test name="test-partial-34.cs">
+    <type name="A">
+      <method name="Void PartialMethodWith_NoAttr_Decl(System.String)" attrs="129">
+        <size>2</size>
+      </method>
+      <method name="Void PartialMethodWith_AAttr_Decl(System.String)" attrs="129">
+        <size>2</size>
+      </method>
+      <method name="Void PartialMethodWith_BAttr_Decl(System.String)" attrs="129">
+        <size>2</size>
+      </method>
+      <method name="Void PartialMethodWith_AAttr_Defn(System.String)" attrs="129">
+        <size>2</size>
+      </method>
+      <method name="Void PartialMethodWith_BAttr_Defn(System.String)" attrs="129">
+        <size>2</size>
+      </method>
+      <method name="Void PartialMethodWith_ABAttr(System.String)" attrs="129">
+        <size>2</size>
+      </method>
+      <method name="Void PartialMethodWith_BAAttr(System.String)" attrs="129">
+        <size>2</size>
+      </method>
+      <method name="Void .ctor()" attrs="6278">
+        <size>7</size>
+      </method>
+    </type>
+    <type name="CustomAttributes.AttributeA">
+      <method name="Void .ctor(String)" attrs="6278">
+        <size>8</size>
+      </method>
+    </type>
+    <type name="CustomAttributes.AttributeB">
+      <method name="Void .ctor(String)" attrs="6278">
+        <size>8</size>
+      </method>
+    </type>
+    <type name="X">
+      <method name="Void Main()" attrs="150">
+        <size>2</size>
+      </method>
+      <method name="Void .ctor()" attrs="6278">
+        <size>7</size>
+      </method>
+    </type>
+  </test>
   <test name="test-pattern-01.cs">
     <type name="TypePattern">
       <method name="Int32 Main()" attrs="150">