[mcs] Conditional check on attribute type needs to check its base types too
authorMarek Safar <marek.safar@gmail.com>
Tue, 9 May 2017 17:02:48 +0000 (19:02 +0200)
committerMarek Safar <marek.safar@gmail.com>
Tue, 9 May 2017 17:08:51 +0000 (19:08 +0200)
mcs/mcs/attribute.cs
mcs/tests/test-942.cs [new file with mode: 0644]
mcs/tests/ver-il-net_4_x.xml

index 8edce969a443819cabe510b5da2804ba4796a2dd..c1c28cd8eedded2fd694e782e6619a3d501ac13d 100644 (file)
@@ -1125,7 +1125,7 @@ namespace Mono.CSharp {
                                cdata = encoder.ToArray ();
                        }
 
-                       if (!ctor.DeclaringType.IsConditionallyExcluded (context)) {
+                       if (!IsConditionallyExcluded (ctor.DeclaringType)) {
                                try {
                                        foreach (Attributable target in targets)
                                                target.ApplyAttributeBuilder (this, ctor, cdata, predefined);
@@ -1166,6 +1166,18 @@ namespace Mono.CSharp {
                        }
                }
 
+               bool IsConditionallyExcluded (TypeSpec type)
+               {
+                       do {
+                               if (type.IsConditionallyExcluded (context))
+                                       return true;
+
+                               type = type.BaseType;
+                       } while (type != null);
+
+                       return false;
+               }
+
                private Expression GetValue () 
                {
                        if (pos_args == null || pos_args.Count < 1)
diff --git a/mcs/tests/test-942.cs b/mcs/tests/test-942.cs
new file mode 100644 (file)
index 0000000..e851f8b
--- /dev/null
@@ -0,0 +1,39 @@
+using System;
+using System.Reflection;
+using System.Diagnostics;
+
+namespace ConditionalAttributeTesting
+{
+       class MainClass
+       {
+               public static int Main ()
+               {
+                       return HelloWorld ();
+               }
+
+               [Some ("Test")]
+               public static int HelloWorld ()
+               {
+                       var methodInfo = MethodBase.GetCurrentMethod ();
+                       SomeAttribute someAttribute = Attribute.GetCustomAttribute (methodInfo, typeof (SomeAttribute)) as SomeAttribute;
+                       if (someAttribute != null) {
+                               return 1;
+                       }
+
+                       return 0;
+               }
+       }
+
+       [AttributeUsage (AttributeTargets.All)]
+       [Conditional ("NOT_DEFINED")]
+       public abstract class BaseAttribute : Attribute
+       {
+       }
+
+       public class SomeAttribute : BaseAttribute
+       {
+               public SomeAttribute (string someText)
+               {
+               }
+       }
+}
\ No newline at end of file
index 9ff491a9ee8f90f6e6bae49eff8c580d70c5741e..0ce8465a3f05977abecfd1deb58c560bb9a713cb 100644 (file)
       </method>
     </type>
   </test>
+  <test name="test-942.cs">
+    <type name="ConditionalAttributeTesting.MainClass">
+      <method name="Int32 Main()" attrs="150">
+        <size>14</size>
+      </method>
+      <method name="Int32 HelloWorld()" attrs="150">
+        <size>52</size>
+      </method>
+      <method name="Void .ctor()" attrs="6278">
+        <size>7</size>
+      </method>
+    </type>
+    <type name="ConditionalAttributeTesting.BaseAttribute">
+      <method name="Void .ctor()" attrs="6276">
+        <size>7</size>
+      </method>
+    </type>
+    <type name="ConditionalAttributeTesting.SomeAttribute">
+      <method name="Void .ctor(String)" attrs="6278">
+        <size>8</size>
+      </method>
+    </type>
+  </test>
   <test name="test-95.cs">
     <type name="X">
       <method name="Int32 Main()" attrs="150">