You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[generator] Better support deprecated property getter/setters. (#1062)
Fixes: #1033
Context: dotnet/roslyn#32571
When we are converting Java get-method and set-method pairs to
C# properties, we can hit an interesting scenario where a getter may
be `@Deprecated` and the setter is not, or vice versa:
class Example {
public boolean hasOptionsMenu () { ... }
@deprecated
public void setHasOptionsMenu (boolean hasMenu) { ... }
}
C# has traditionally not allowed `[Obsolete]` to be placed on just a
`get` or a `set` block; it could only be on the entire property:
partial class Example {
[Obsolete]
public bool HasOptionsMenu { get; set; }
}
This can lead to confusion because using the getter will report an
obsolete warning when it is not obsolete. Thus, for properties, we
only add `[Obsolete]` in 2 cases:
1. The `get` is obsolete and there is no `set`
2. Both the `get` and `set` are obsolete
We have this comment in our code:
// Unlike [Register], [Obsolete] cannot be put on property accessors, so we can apply them only under limited condition...
However, the C# compiler team has determined that preventing
`[Obsolete]` on property accessors was a bug, and has fixed it in C#8
via dotnet/roslyn#32571.
Update `generator` to support scenarios in which only the Java getter
or setter is marked as `@Deprecated`, by placing `[Obsolete]` on the
appropriate `get` or `set` block:
partial class Example {
public bool HasOptionsMenu {
get => …
[Obsolete]
set =>…
}
}
Copy file name to clipboardExpand all lines: tests/generator-Tests/Unit-Tests/CodeGeneratorTests.cs
+162-2Lines changed: 162 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -565,7 +565,8 @@ public void ObsoletedOSPlatformAttributeSupport ()
565
565
Assert.True(writer.ToString().Contains("[global::System.Runtime.Versioning.ObsoletedOSPlatform (\"android25.0\", @\"This is a field deprecated since 25!\")]"),writer.ToString());
566
566
Assert.True(writer.ToString().Contains("[global::System.Runtime.Versioning.ObsoletedOSPlatform (\"android25.0\", @\"This is a constructor deprecated since 25!\")]"),writer.ToString());
567
567
Assert.True(writer.ToString().Contains("[global::System.Runtime.Versioning.ObsoletedOSPlatform (\"android25.0\", @\"This is a method deprecated since 25!\")]"),writer.ToString());
568
-
Assert.True(writer.ToString().Contains("[global::System.Runtime.Versioning.ObsoletedOSPlatform (\"android25.0\", @\"This is a property getter deprecated since 25! This is a property setter deprecated since 25!\")]"),writer.ToString());
568
+
Assert.True(writer.ToString().Contains("[global::System.Runtime.Versioning.ObsoletedOSPlatform (\"android25.0\", @\"This is a property getter deprecated since 25!\")]"),writer.ToString());
569
+
Assert.True(writer.ToString().Contains("[global::System.Runtime.Versioning.ObsoletedOSPlatform (\"android25.0\", @\"This is a property setter deprecated since 25!\")]"),writer.ToString());
569
570
}
570
571
571
572
[Test]
@@ -611,7 +612,152 @@ public void ObsoletedOSPlatformAttributeUnneededSupport ()
611
612
}
612
613
613
614
[Test]
614
-
[NonParallelizable]// We are setting a static property on Report
0 commit comments