-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Non-Nullable reference types of static fields #2088
Comments
This sounds more like an issue for Roslyn. However, I'm confused by sample 2. What in there should trigger a warning? Everything can be statically proven to be non-null. |
Definitely an issue for the Roslyn repo. I believe it should already by covered by roslyn#26628 (initializing with null should warn) and roslyn#27014 (uninitialized static fields should warn).
|
What if sample 2 were //A.dll
class A {
static A() { Program.PrintLength(); }
public static string GetName() { return string.Empty; }
}
//Program.exe
using System;
class Program {
public static string name = A.GetName();
public static void PrintLength() { Console.WriteLine(name.Length); }
static void Main(string[] args) { }
} |
If sample 2 were that, then you'd never get it to compile as you are creating a circular dependency between the two assemblies. |
@DavidArno |
Oops, I missed that the constructor was static in sample 2. In my opinion, that the compiler shouldn't try to detect that situation. Static initialization is already kinda funky, care should be taken when doing it. Methods used to initialize static members probably shouldn't even accessing other static fields as a rule. @DavidArno You actually can do this with reference assembly shenanigans since the runtime will happily resolve circular assembly references like that even though the project system won't. That example is pure evil though. If you're intentionally doing stuff like that, you'd better know what you're doing. |
Relevant: roslyn#31440. |
I'm going to close this as it is an issue for roslyn. Some of these have been fixed, and some haven't, so I'm not going to ask this to be moved - instead I would suggest that you open new issues there. Note that I'm not very hopeful on e.g. sample3 and sample4 being fixed. Nullable Analysis is at the moment intraprocedural (looks at only one method at a time). Changing that is infeasible, and in the general case impossible (due to the halting problem). So you would have to come up with a method that detects this case without forcing you to declare all static members as nullable. |
Following code samples compile without warnings on the VS2019 16.0.0 preview 1.1 and result in null reference exception
I would expect to get a null-reference warning message for the following cases:
Usage of following fields without null check:
A lyrical digression, my personal regret about the language becoming less elegant :)
I am sure this was taken into account and found practical.
However, the feature the way it was designed could lead programmers towards omitting null checks and thus introducing new null reference exceptions, where previously they would not have happened.
The absence of compiler warnings is misleading. The compiler does not claim to ensure non-nullability, however, we are used to trusting it, and not to question the scope of its competency.
Provable non-nullability would have a run-time performance penalty, and I would rather opt into it when I prefer safety over speed.
The text was updated successfully, but these errors were encountered: