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 TestMissingInRegularAndScriptAsync("[|Console|].WriteLine();");
}
}
}
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 { IsImplicitlyDeclared: false, Name: not TopLevelStatementsEntryPointTypeName };
Youssef1313 marked this conversation as resolved.
Show resolved Hide resolved
}

internal bool CanGenerateLocal()
Expand All @@ -148,7 +148,10 @@ 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;
return ContainingMethod is { IsImplicitlyDeclared: false }
// Workaround: The compiler returns IsImplicitlyDeclared = false for <Main>$.
&& ContainingMethod.Name != WellKnownMemberNames.TopLevelStatementsEntryPointMethodName
Youssef1313 marked this conversation as resolved.
Show resolved Hide resolved
&& !IsInMemberContext && !IsConstant;
Youssef1313 marked this conversation as resolved.
Show resolved Hide resolved
}

private bool TryInitializeExplicitInterface(
Expand Down