Skip to content
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

Address NRT issues found by targeting net5.0 #50895

Merged
merged 13 commits into from
Mar 5, 2021

Conversation

huoyaoyuan
Copy link
Member

Moving target is currently blocked by #50893 . Cherry-picking NRT commits. Part of #50822.

Mostly caused by LINQ, Path/Directory and xml dom.

I'm trying to use asset patterns in the same file.

@@ -89,7 +89,7 @@ public static IEnumerable<TNode> GetCurrentNodes<TNode>(this SyntaxNode root, TN
/// </summary>
/// <param name="root">The root of the subtree containing the current node corresponding to the original tracked node.</param>
/// <param name="node">The node instance originally tracked.</param>
public static TNode GetCurrentNode<TNode>(this SyntaxNode root, TNode node)
public static TNode? GetCurrentNode<TNode>(this SyntaxNode root, TNode node)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is public API bug, and causing bug in Features layer.

@@ -175,7 +175,7 @@ private static void ReportTelemetryIfNecessary(ImmutableArray<(TExpressionSyntax
var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var spanNodes = diagnostics.SelectAsArray(
d => root.FindNode(d.Location.SourceSpan, getInnermostNodeForTie: true)
.GetAncestorsOrThis<TExpressionSyntax>().FirstOrDefault());
.GetAncestorsOrThis<TExpressionSyntax>().First());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks very confidential about it (defensive in outer code but not this), so I changed to First.

@jcouv jcouv added the Concept-Null Annotations The issue involves annotating an API for nullable reference types label Jan 29, 2021
@@ -19,11 +19,11 @@ internal static class RoslynLazyInitializer
=> LazyInitializer.EnsureInitialized<T>(ref target!, valueFactory);

/// <inheritdoc cref="LazyInitializer.EnsureInitialized{T}(ref T, ref bool, ref object)"/>
public static T EnsureInitialized<T>([NotNull] ref T? target, ref bool initialized, [NotNull] ref object? syncLock)
public static T EnsureInitialized<T>([NotNull] ref T? target, ref bool initialized, [NotNullIfNotNull("syncLock")] ref object? syncLock)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's clever ;-)
Do we still need RoslynLazyInitializer now that LazyInitializer is annotated? If it's not a trivial change, please file an issue and assign it to me.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied the annotation from net5.0.
Lack of [NotNull] should have impact on netstandard2.0

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. Thanks

@jcouv jcouv self-assigned this Jan 29, 2021
Copy link
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done with review pass of compiler changes (iteration 3)

@RikkiGibson
Copy link
Contributor

It's necessary to merge the latest master into this branch to fix the build.

Copy link
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compiler changes LGTM Thanks (iteration 5)

@jaredpar jaredpar added the Community The pull request was submitted by a contributor who is not a Microsoft employee. label Feb 1, 2021
@jcouv
Copy link
Member

jcouv commented Feb 9, 2021

@dotnet/roslyn-ide for review of the IDE changes. Thanks

Copy link
Member

@cston cston left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compiler changes LGTM, thanks.

@jcouv
Copy link
Member

jcouv commented Feb 10, 2021

@dotnet/roslyn-ide @jinujoseph for review of the IDE changes. Thanks

@@ -106,6 +106,7 @@ public static ImmutableArray<string> FindSatelliteAssemblies(string filePath)
var builder = ImmutableArray.CreateBuilder<string>();

string? directory = Path.GetDirectoryName(filePath);
RoslynDebug.AssertNotNull(directory);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❔ Is it ever possible for PathUtilities.IsAbsolute(filePath) to return true but Path.GetDirectoryName(filePath) to return null? If not, I almost prefer this form:

Debug.Assert(PathUtilities.IsAbsolute(filePath));
string directory = Path.GetDirectoryName(filePath)!;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If path happens to be C:\?

Copy link
Member

@jasonmalinowski jasonmalinowski Feb 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, assert seems wise so I think this looks good -- there shouldn't ever be a directory name versus a file path being passed here, but if that were to happen...

Copy link
Member

@sharwell sharwell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made it partly through. It'll take a while to finish all cases since I really have to think about each one :)

Copy link
Member

@jasonmalinowski jasonmalinowski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally looks good, but a few specific questions.

@@ -106,6 +106,7 @@ public static ImmutableArray<string> FindSatelliteAssemblies(string filePath)
var builder = ImmutableArray.CreateBuilder<string>();

string? directory = Path.GetDirectoryName(filePath);
RoslynDebug.AssertNotNull(directory);
Copy link
Member

@jasonmalinowski jasonmalinowski Feb 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, assert seems wise so I think this looks good -- there shouldn't ever be a directory name versus a file path being passed here, but if that were to happen...

@@ -82,7 +82,7 @@ public NotificationOption2 Notification
new("CodeStyleOption", // Ensure that we use "CodeStyleOption" as the name for back compat.
new XAttribute(nameof(SerializationVersion), SerializationVersion),
new XAttribute("Type", GetTypeNameForSerialization()),
new XAttribute(nameof(Value), GetValueForSerialization()),
new XAttribute(nameof(Value), GetValueForSerialization()!),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's going on here? GetValueForSerialization is pretty directly marked as null-returning; can we revise that and push the suppression somewhere closer if possible?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's only nullable when using uninitialized value with T=string. There could be a later refactor on that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I'd say in that case we should:

  1. annotate GetValueForSerialization to return not-null.
  2. In the branch for string, do a Contract.ThrowIfNull().
  3. In the branch for bool, just ! since that's more obvious what's going on here.

Now I'm also a bit confused because the docs for the type say that T is only allowed to be bool or an enum, so clearly we are breaking that rule somewhere...? @mavasani or @jmarolf any idea if nulls are supposed to be supported here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jmarolf confirmed that we don't expect to get nulls here, so current code is good.

src/Compilers/Core/Portable/PEWriter/SigningUtilities.cs Outdated Show resolved Hide resolved
public static T WaitAndGetResult<T>(this Task<T> task, CancellationToken cancellationToken)
{
#if DEBUG
if (IsThreadPoolThread(Thread.CurrentThread))
if (Thread.CurrentThread.IsThreadPoolThread)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥳

Copy link
Member

@jasonmalinowski jasonmalinowski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments left; this is looking pretty good from my perspective although do want some of the feature area owners to comment on their own code.

@@ -82,7 +82,7 @@ public NotificationOption2 Notification
new("CodeStyleOption", // Ensure that we use "CodeStyleOption" as the name for back compat.
new XAttribute(nameof(SerializationVersion), SerializationVersion),
new XAttribute("Type", GetTypeNameForSerialization()),
new XAttribute(nameof(Value), GetValueForSerialization()),
new XAttribute(nameof(Value), GetValueForSerialization()!),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I'd say in that case we should:

  1. annotate GetValueForSerialization to return not-null.
  2. In the branch for string, do a Contract.ThrowIfNull().
  3. In the branch for bool, just ! since that's more obvious what's going on here.

Now I'm also a bit confused because the docs for the type say that T is only allowed to be bool or an enum, so clearly we are breaking that rule somewhere...? @mavasani or @jmarolf any idea if nulls are supposed to be supported here?

Base automatically changed from master to main March 3, 2021 23:53
@jasonmalinowski jasonmalinowski merged commit 0aaa2eb into dotnet:main Mar 5, 2021
@jasonmalinowski
Copy link
Member

Thanks @huoyaoyuan for the fixes!

@huoyaoyuan huoyaoyuan deleted the net5.0-nrt branch March 5, 2021 04:20
@allisonchou allisonchou added this to the Next milestone Mar 12, 2021
@allisonchou allisonchou modified the milestones: Next, 16.10.P2 Mar 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community The pull request was submitted by a contributor who is not a Microsoft employee. Concept-Null Annotations The issue involves annotating an API for nullable reference types
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants