-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[generator] Use 'var' when possible in generated code to reduce nullability annotations. #621
Conversation
112e771
to
56ba268
Compare
…bility annotations.
56ba268
to
39b2b2b
Compare
I feel like, as-is, this only moves the warning, it doesn't "fix" the warning. Consider Updating line 76 to use
Replacing CS8600 with CS8602 doesn't feel like a "win". We could fix the CS8602 by using
Which is to say, this feels like a losing game. It's simple-yet-massive change which won't really fix what it attempts to fix. I fear that the "real" fix will be to just stick static int n_GetSpanFlags_Ljava_lang_Object_ (IntPtr jnienv, IntPtr native__this, IntPtr native_tag)
{
#nullable disable
Android.Text.ISpannable __this = global::Java.Lang.Object.GetObject<Android.Text.ISpannable> (jnienv, native__this, JniHandleOwnership.DoNotTransfer);
Java.Lang.Object tag = global::Java.Lang.Object.GetObject<Java.Lang.Object> (native_tag, JniHandleOwnership.DoNotTransfer);
int __ret = (int) __this.GetSpanFlags (tag);
return __ret;
#nullable enable
} which would be an even larger change, |
Correct, this PR does not attempt to introduce nullability annotations, or fix the right-hand side of the equals sign, it simply fixes the left-hand side. The NRT PR will need to generate this when NRT is turned off:
and this when NRT is turned on:
This PR makes our eventual NRT logic simpler by not needing to conditionally annotate the left-hand side. When NRT is turned off ( These tests are the current tests and will continue testing the !NRT case, so they will not change in the NRT PR. New tests will be added for the NRT case. Thus we can move the "noise" of updating the non-NRT tests to this PR. |
I assume this should use However, that won't work, because the return type of the method will still be Additionally, in this context this doesn't seem quite right? The only time that Given how rare the return value will actually be (Unfortunately we can't use After discussing on #onedotnet, it feels like |
I think in this specific case, the NRT PR uses the null-forgiving operator (
As stated in the NRT non-goals, we are not attempting to rewrite our generated code to be more provably non-null at this time. We are working under the assumption that our current generated code works and thus we are currently preventing an NRE here via other means. However, for a user calling |
|
Context: dotnet/android#4227 Our generated code *may* predate `var`, or at least predate `var` becoming widely accepted. As such, we generate code like this: java.code.MyClass __this = global::Java.Lang.Object.GetObject<java.code.MyClass> (jnienv, native__this, JniHandleOwnership.DoNotTransfer); string key = JNIEnv.GetString (native_key, JniHandleOwnership.DoNotTransfer); Migrate this code to `var` as it simplifies `generator` and reasoning about behavior when "cross-compiling" code between C#7 and C#8-with- [Nullable Reference Type][0] toolchains: var __this = global::Java.Lang.Object.GetObject<java.code.MyClass> (jnienv, native__this, JniHandleOwnership.DoNotTransfer); var key = JNIEnv.GetString (native_key, JniHandleOwnership.DoNotTransfer); [0]: https://docs.microsoft.com/en-us/dotnet/csharp/nullable-references
Our generated code may predate
var
, or at least predatevar
becoming widely accepted. As such, we generate code like this:In an NRT world, each one of these declarations will have to be audited and optionally annotated with the nullable operator
?
. Or..... we can just mark them asvar
and not worry about it.Pulling this change into a separate commit in order to lower the noise in the eventual NRT PR.