Skip to content

Commit be7e8f5

Browse files
committed
[generator] Fix availability in property accessor when different from the property itself.
Scenario: * Type is available on iOS, tvOS. * Property in the type is available on iOS (and not tvOS). * Property accessor has explicit availability attributes for iOS. Then the property accessor would get the availability attribute for tvOS from the type, and not the (un)availability attribute from the property. The fix is to make sure the parent context is the property (and not the type) when processing availability attributes for the accessor.
1 parent d2e7a2d commit be7e8f5

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

src/generator.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3677,15 +3677,16 @@ List<AvailabilityBaseAttribute> GetAllParentAttributes (MemberInfo context)
36773677
}
36783678
}
36793679

3680-
AvailabilityBaseAttribute [] GetPlatformAttributesToPrint (MemberInfo mi, Type type, MemberInfo inlinedType)
3680+
AvailabilityBaseAttribute [] GetPlatformAttributesToPrint (MemberInfo mi, MemberInfo context, MemberInfo inlinedType)
36813681
{
36823682
// Attributes are directly on the member
36833683
List<AvailabilityBaseAttribute> memberAvailability = AttributeManager.GetCustomAttributes<AvailabilityBaseAttribute> (mi).ToList ();
36843684

36853685
// Due to differences between Xamarin and NET6 availability attributes, we have to synthesize many duplicates for NET6
36863686
// See https://github.com/xamarin/xamarin-macios/issues/10170 for details
36873687
#if NET
3688-
MemberInfo context = type ?? FindContainingContext (mi);
3688+
if (context is null)
3689+
context = FindContainingContext (mi);
36893690
// Attributes on the _target_ context, the class itself or the target of the protocol inlining
36903691
List<AvailabilityBaseAttribute> parentContextAvailability = GetAllParentAttributes (context);
36913692
// (Optional) Attributes from the inlined protocol type itself
@@ -3769,7 +3770,7 @@ public bool PrintPlatformAttributes (MemberInfo mi, Type inlinedType = null)
37693770

37703771
AvailabilityBaseAttribute [] type_ca = null;
37713772

3772-
foreach (var availability in GetPlatformAttributesToPrint (mi, mi.DeclaringType, inlinedType)) {
3773+
foreach (var availability in GetPlatformAttributesToPrint (mi, null, inlinedType)) {
37733774
var t = inlinedType ?? (mi as TypeInfo) ?? mi.DeclaringType;
37743775
if (type_ca == null) {
37753776
if (t != null)

tests/generator/BGenTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,35 @@ public void GeneratedAttributeOnPropertyAccessors ()
11551155
Assert.AreEqual (string.Empty, RenderSupportedOSPlatformAttributes (getter), "Getter Attributes");
11561156
}
11571157

1158+
#if !NET
1159+
[Ignore ("This test only applies to .NET")]
1160+
#endif
1161+
[Test]
1162+
public void GeneratedAttributeOnPropertyAccessors2 ()
1163+
{
1164+
var bgen = BuildFile (Profile.MacCatalyst, "tests/generated-attribute-on-property-accessors2.cs");
1165+
1166+
var messaging = bgen.ApiAssembly.MainModule.Types.First (v => v.Name == "ISomething");
1167+
var property = messaging.Properties.First (v => v.Name == "MicrophoneEnabled");
1168+
var getter = messaging.Methods.First (v => v.Name == "get_MicrophoneEnabled");
1169+
var setter = messaging.Methods.First (v => v.Name == "set_MicrophoneEnabled");
1170+
1171+
var expectedPropertyAttributes =
1172+
@"[SupportedOSPlatform(""ios"")]
1173+
[SupportedOSPlatform(""maccatalyst"")]
1174+
[SupportedOSPlatform(""macos11.0"")]
1175+
[UnsupportedOSPlatform(""tvos"")]";
1176+
var expectedSetterAttributes =
1177+
@"[SupportedOSPlatform(""ios"")]
1178+
[SupportedOSPlatform(""maccatalyst"")]
1179+
[SupportedOSPlatform(""macos11.0"")]
1180+
[UnsupportedOSPlatform(""tvos"")]";
1181+
1182+
Assert.AreEqual (expectedPropertyAttributes, RenderSupportedOSPlatformAttributes (property), "Property attributes");
1183+
Assert.AreEqual (string.Empty, RenderSupportedOSPlatformAttributes (getter), "Getter Attributes");
1184+
Assert.AreEqual (expectedSetterAttributes, RenderSupportedOSPlatformAttributes (setter), "Setter Attributes");
1185+
}
1186+
11581187
BGenTool BuildFile (Profile profile, params string [] filenames)
11591188
{
11601189
return BuildFile (profile, true, false, filenames);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using Foundation;
3+
using ObjCRuntime;
4+
5+
namespace NS {
6+
[Introduced (PlatformName.iOS, 9, 0)]
7+
[Introduced (PlatformName.TvOS, 10, 0)]
8+
[Introduced (PlatformName.MacOSX, 11, 0)]
9+
[Introduced (PlatformName.MacCatalyst, 13, 1)]
10+
[BaseType (typeof (NSObject))]
11+
[DisableDefaultCtor]
12+
[Sealed]
13+
interface ISomething {
14+
[NoTV]
15+
[Introduced (PlatformName.MacCatalyst, 13, 1)]
16+
[Export ("microphoneEnabled", ArgumentSemantic.Assign)]
17+
bool MicrophoneEnabled {
18+
[Bind ("isMicrophoneEnabled")]
19+
get;
20+
[Introduced (PlatformName.iOS, 10, 0)]
21+
[Introduced (PlatformName.MacCatalyst, 13, 1)]
22+
set;
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)