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

Fix imports not being removed from MAS files. #44230

Merged
merged 21 commits into from
May 16, 2020

Conversation

CyrusNajmabadi
Copy link
Member

@CyrusNajmabadi CyrusNajmabadi commented May 13, 2020

Fixes #44200
Fixes #30327

Also does a reasonable effort of emitting #nullable enable/disable directives in MAS files when it contains signatures wiht types that are annotated/oblivious.

@CyrusNajmabadi CyrusNajmabadi requested a review from a team as a code owner May 13, 2020 22:06
@CyrusNajmabadi CyrusNajmabadi requested a review from jcouv May 13, 2020 22:06
@CyrusNajmabadi
Copy link
Member Author

tagging @dotnet/roslyn-ide

using Microsoft.CodeAnalysis.Formatting.Rules;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.CSharp.MetadataAsSource
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 is justa move.

@@ -71,55 +73,162 @@ protected override ImmutableArray<AbstractReducer> GetReducers()
new CSharpParenthesizedPatternReducer(),
new CSharpDefaultExpressionReducer());

private class FormattingRule : AbstractMetadataFormattingRule
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 type was moved to a sibling file.

@jasonmalinowski
Copy link
Member

@CyrusNajmabadi Does this also close #30327?

@JoeRobich
Copy link
Member

@CyrusNajmabadi So the extra using directives were being added because we were always adding Attribute namespaces to the list of usings, and the .NET 5 assemblies now have nullable annotations?

@CyrusNajmabadi
Copy link
Member Author

@CyrusNajmabadi Does this also close #30327?

Yup!

@CyrusNajmabadi
Copy link
Member Author

@CyrusNajmabadi So the extra using directives were being added because we were always adding Attribute namespaces to the list of usings, and the .NET 5 assemblies now have nullable annotations?

That was one problem. But the primary problem was that we basically turned off the step to then clean up those imports. So any added imports that turned out to be unnecessary just were not removed. Nullable just exacerbated this as you might have a ton of those in code which just exploded this sort of thing.


[NullableContextAttribute(1)]
public interface [|C|]<[NullableAttribute(2)] T>
public interface [|C|]<T>
Copy link
Member

Choose a reason for hiding this comment

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

Should this have gotten a #nullable enable added?

Copy link
Member Author

Choose a reason for hiding this comment

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

not that i can tell. @jcouv we dont' need #nullable enable here right? we don't have an annotated reference type afaict.

Copy link
Member

Choose a reason for hiding this comment

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

Tagging @cston. I'm surprised that we emit a NullableAttribute(2) (means ?) on an unconstrained T in the first place (sharplab)

Copy link
Member

Choose a reason for hiding this comment

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

The attribute value represents the lack of a notnull constraint - see examples.

Comment on lines +181 to +182
// if we hit a type, and we're currently disabled, then switch us back to enabled for that type.
// This ensures whenever we walk into a type-decl, we're always in the enabled-state.
Copy link
Member

Choose a reason for hiding this comment

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

Is that to simply the algorithm since we're doing this as a bottom-up rewrite so we can't really know where the type might end up?

Copy link
Member Author

Choose a reason for hiding this comment

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

that's exactly it. it also keeps things stateless so we don't need to keep track of everything as we walk.

{{
public TestType();

#nullable disable
Copy link
Member

Choose a reason for hiding this comment

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

When looking at the implementation at first this seemed a bit strange to tag it this way but I have to say I think I actually like it -- it means there's always a #nullable enable at the top which is easy to see, and the special cases are called out specifically.

Copy link
Member Author

Choose a reason for hiding this comment

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

yup. that seemed really sane and sensible. it's the coding pattern i would prefer to see tbh :)

Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Auto-approval

{
}

#nullable disable
Copy link
Member

Choose a reason for hiding this comment

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

nit: formatting

public TestType();

public void [|M1|](dynamic s);
}}";
Copy link
Member

@jcouv jcouv May 14, 2020

Choose a reason for hiding this comment

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

Consider including a nasty case (where best effort fails):

List<string
#nullable enable
    > M(
#nullable disable
        string parameter) => throw null;

Also, the case we discussed yesterday where members omitted by MAS still affect the output.

@CyrusNajmabadi CyrusNajmabadi merged commit f8ed9e7 into dotnet:master May 16, 2020
@ghost ghost added this to the Next milestone May 16, 2020
@CyrusNajmabadi CyrusNajmabadi deleted the nullableMAS branch May 16, 2020 01:38
@JoeRobich JoeRobich modified the milestones: Next, 16.7.P2 May 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Duplicate using directives in Metadata as source MetadataAsSource isn't aware of nullability
5 participants