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

Don't offer generate parameter/field/property for top-level programs #48173

Merged
merged 9 commits into from
Oct 1, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -9336,5 +9336,12 @@ internal MyException(int error, int offset, string message) : base(message)
string.Format(FeaturesResources.Generate_parameter_0, "Error", "MyException"),
});
}

[WorkItem(48172, "https://github.com/dotnet/roslyn/issues/48172")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateVariable)]
public async Task TestMissingOfferParameterInTopLevel()
{
await TestMissingAsync("[|Console|].WriteLine();", new TestParameters(Options.Regular));
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 test is for Regular C# only.
C#-script have a different behavior which is already tested in other tests.

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private async Task<bool> TryInitializeAsync(

internal bool CanGeneratePropertyOrField()
{
return !ContainingType.IsImplicitClass;
return ContainingType is { IsImplicitClass: false, Name: not WellKnownMemberNames.TopLevelStatementsEntryPointTypeName };
}

internal bool CanGenerateLocal()
Expand All @@ -148,7 +148,9 @@ internal bool CanGenerateLocal()
internal bool CanGenerateParameter()
{
// !this.IsInMemberContext prevents us offering this fix for `x.goo` where `goo` does not exist
return ContainingMethod != null && !IsInMemberContext && !IsConstant;
// Workaround: The compiler returns IsImplicitlyDeclared = false for <Main>$.
return ContainingMethod is { IsImplicitlyDeclared: false, Name: not WellKnownMemberNames.TopLevelStatementsEntryPointMethodName }
&& !IsInMemberContext && !IsConstant;
}

private bool TryInitializeExplicitInterface(
Expand Down