-
Notifications
You must be signed in to change notification settings - Fork 286
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
Rework TdsParserStateObjectManaged
with nullable annotations
#1555
Conversation
...Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObjectManaged.cs
Outdated
Show resolved
Hide resolved
...Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObjectManaged.cs
Outdated
Show resolved
Hide resolved
...Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObjectManaged.cs
Outdated
Show resolved
Hide resolved
@Wraith2 it is pretty common practice to turn on NRTs only for recent TFMs, to avoid the issues above, e.g. since the netstandard2.0 isn't yet properly annotated. For nullability-related attributes which were only introduced later (e.g. [DoesNotReturn]), you can have those attributes defined in the project once behind #if, so that you don't need to do conditional compilation every time you want to use them (here's the Npgsql example). This should land you in a place where you can use NRTs freely and without hacks or code smells, while getting almost perfect coverage through the new TFMs. There's always the chance of unverified code that's conditionally compiled only in older TFMs, but that's generally very rare, and code quality is usually more important. |
I considered this but my concern is what happens if multiple assemblies do the same thing? Say for example npgsql and sqlclient get loaded into the same appdomain/alc, doesn't it cause a crash because it's multiply defined? |
Having multiple types in the same namespace isn't an issue, provided they live in different assemblies. .NET types are fully-qualified by their assembly name. These would just be internal types which never get used or exposed externally. In fact, the NRT NullableAttribute (which is what gets generated based on NRT annotations) is synthesized by Roslyn into assemblies it builds (there's no NullableAttribute in the BCL), so there's already lots of copies of that type all around (see dotnet/runtime#29039). |
Ok, that works for me. I've integrated the definitions into the shared codebase so when we get to merging the managed implementation into netfx we'll be able to use the same file. This removes all the postfix non-null assertion decorations which is pleasing. |
@David-Engel I know this is queue jumping but it would be nice to try and get this into 5.0.0 because it fixes a user reported issue and is a stability improvement. |
Codecov Report
@@ Coverage Diff @@
## main #1555 +/- ##
==========================================
- Coverage 71.40% 71.25% -0.16%
==========================================
Files 295 295
Lines 61195 61208 +13
==========================================
- Hits 43697 43611 -86
- Misses 17498 17597 +99
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
src/Microsoft.Data.SqlClient/src/System/Diagnostics/CodeAnalysis.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/src/System/Diagnostics/CodeAnalysis.cs
Outdated
Show resolved
Hide resolved
...Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObjectManaged.cs
Outdated
Show resolved
Hide resolved
...Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObjectManaged.cs
Outdated
Show resolved
Hide resolved
a7a8a9e
to
625bfa8
Compare
625bfa8
to
23ed8a6
Compare
src/Microsoft.Data.SqlClient/netcore/src/Resources/Strings.Designer.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/netcore/src/Resources/Strings.resx
Outdated
Show resolved
Hide resolved
# Conflicts: # src/Microsoft.Data.SqlClient/netcore/src/Resources/Strings.resx
IncorrectPhysicalConnectionType exception
I was contacted by someone in the c# discord server who was experiencing #988 with version 3.0.1 of this library. If the previous fix was correct that shouldn't be possible so I had a look at the code in
TdsParserStateObjectManaged.ReleasePacket
:There are only two variables involved and the only one we haven't checked for null is
handle
so it's pretty obvious what's going on. Looking at this it's clear that c#8 nullable reference type annotations would have shown this as a problem so I thought I'd rewrite the file with nullable enabled to see if it caused any interesting bugs or changes to show up. This PR is the result.Nullable annotations are a compile time feature and do not add any runtime checking. It is possible to pass nulls to functions defined in nullable enabled files even if those functions are decorated with non-null context. That means that these changes only introduce new exceptions where an explicit throw is added, there are no hidden null checks.