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 prototype comments #61436

Merged
merged 7 commits into from
May 24, 2022
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
@@ -7136,4 +7136,10 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="ERR_UnsupportedCompilerFeature" xml:space="preserve">
<value>'{0}' requires compiler feature '{1}', which is not supported by this version of the C# compiler.</value>
</data>
<data name="WRN_ObsoleteMembersShouldNotBeRequired" xml:space="preserve">
<value>Members attributed with 'ObsoleteAttribute' should not be required unless the containing type is obsolete or all constructors are obsolete.</value>
333fred marked this conversation as resolved.
Show resolved Hide resolved
</data>
<data name="WRN_ObsoleteMembersShouldNotBeRequired_Title" xml:space="preserve">
<value>Members attributed with 'ObsoleteAttribute' should not be required unless the containing type is obsolete or all constructors are obsolete.</value>
</data>
333fred marked this conversation as resolved.
Show resolved Hide resolved
</root>
1 change: 1 addition & 0 deletions src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
Original file line number Diff line number Diff line change
@@ -2088,5 +2088,6 @@ internal enum ErrorCode
ERR_ChainingToSetsRequiredMembersRequiresSetsRequiredMembers = 9510,
ERR_NewConstraintCannotHaveRequiredMembers = 9511,
ERR_UnsupportedCompilerFeature = 9512,
WRN_ObsoleteMembersShouldNotBeRequired = 9513,
}
}
1 change: 1 addition & 0 deletions src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs
Original file line number Diff line number Diff line change
@@ -501,6 +501,7 @@ internal static int GetWarningLevel(ErrorCode code)
case ErrorCode.WRN_UseDefViolationThisSupportedVersion:
case ErrorCode.WRN_UnassignedThisAutoPropertySupportedVersion:
case ErrorCode.WRN_UnassignedThisSupportedVersion:
case ErrorCode.WRN_ObsoleteMembersShouldNotBeRequired:
return 1;
default:
return 0;
Original file line number Diff line number Diff line change
@@ -676,7 +676,7 @@ void checkMemberStateOnConstructorExit(MethodSymbol constructor, Symbol member,
return;
}

if (symbol.IsRequired())
if (symbol.IsRequired() && constructor.ShouldCheckRequiredMembers())
{
return;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions src/Compilers/CSharp/Portable/Symbols/CompletionPart.cs
Original file line number Diff line number Diff line change
@@ -49,15 +49,16 @@ internal enum CompletionPart
SynthesizedExplicitImplementations = 1 << 13,
StartMemberChecks = 1 << 14,
FinishMemberChecks = 1 << 15,
MembersCompleted = 1 << 16, // this should be the last (highest-value) part
MembersCompletedChecksStarted = 1 << 16,
MembersCompleted = 1 << 17, // this should be the last (highest-value) part

All = (1 << 17) - 1,
All = (1 << 18) - 1,

// This is the work we can do if ForceComplete is scoped to a particular SyntaxTree.
NamedTypeSymbolWithLocationAll = Attributes | StartBaseType | FinishBaseType | StartInterfaces | FinishInterfaces | EnumUnderlyingType |
TypeArguments | TypeParameters | Members | TypeMembers | SynthesizedExplicitImplementations | StartMemberChecks | FinishMemberChecks,

NamedTypeSymbolAll = NamedTypeSymbolWithLocationAll | MembersCompleted,
NamedTypeSymbolAll = NamedTypeSymbolWithLocationAll | MembersCompletedChecksStarted | MembersCompleted,

// For Usings
StartValidatingImports = 1 << 4,
Original file line number Diff line number Diff line change
@@ -244,6 +244,8 @@ internal override LexicalSortKey GetLexicalSortKey()
// so we will keep them the same.
return new LexicalSortKey(this.syntaxReferenceOpt.GetLocation(), this.DeclaringCompilation);
}

protected override bool HasSetsRequiredMembersImpl => false;
}

private sealed class InvokeMethod : SourceDelegateMethodSymbol
@@ -450,5 +452,6 @@ private static bool IsUnique(ArrayBuilder<ParameterSymbol> currentParameters, st

return true;
}

333fred marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
@@ -597,6 +597,7 @@ internal override void ForceComplete(SourceLocation? locationOpt, CancellationTo
}
break;

case CompletionPart.MembersCompletedChecksStarted:
case CompletionPart.MembersCompleted:
{
ImmutableArray<Symbol> members = this.GetMembersUnordered();
@@ -631,11 +632,22 @@ internal override void ForceComplete(SourceLocation? locationOpt, CancellationTo

EnsureFieldDefinitionsNoted();

// We've completed all members, so we're ready for the PointedAtManagedTypeChecks;
// proceed to the next iteration.
state.NotePartComplete(CompletionPart.MembersCompleted);
break;

if (state.NotePartComplete(CompletionPart.MembersCompletedChecksStarted))
Copy link
Member

@jcouv jcouv May 24, 2022

Choose a reason for hiding this comment

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

nit: double empty line above #Closed

Copy link
Member

@jcouv jcouv May 24, 2022

Choose a reason for hiding this comment

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

nit: The name of the completion flag is odd, as it suggests it comes in play when members are completed, when in fact it comes before setting CompletionPart.MembersCompleted. Consider MembersChecksStarted or MembersCompletionChecksStarted. #Closed

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 name of the completion flag is odd, as it suggests it comes in play when members are completed

I think that interpretation is correct. The flag is only set after members are completed, and we start the checks we perform after members are completed.

Copy link
Member

Choose a reason for hiding this comment

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

we start the checks we perform after members are completed.

There is a rub here. I that the MembersCompleted completion part to indicate when the members are completed. The checks we're adding come just before members are completed.

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 checks we're adding come just before members are completed.

The members are all completed when these checks are run. We don't publish that fact yet, because we want to run some checks now that they are completed, but they are completed.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I get that we have the members at hand. But we can't say they are completed until we've marked that part as complete. That's the definition of CompletionPart.MembersCompleted.
Perhaps CompletionPart.StartValidatingMembers?

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'm not a fan of that name either, because I don't know how it's different from StartMemberChecks or similar. This phase is "the members are fully complete, so let's do checks involving their attributes now". I think MembersCompletedChecksStarted is the correct name for such a phase.

{
cancellationToken.ThrowIfCancellationRequested();
333fred marked this conversation as resolved.
Show resolved Hide resolved
var diagnostics = BindingDiagnosticBag.GetInstance();
AfterMembersCompletedChecks(diagnostics);
AddDeclarationDiagnostics(diagnostics);

// We've completed all members, so we're ready for the PointedAtManagedTypeChecks;
// proceed to the next iteration.
var thisThreadCompleted = state.NotePartComplete(CompletionPart.MembersCompleted);
Debug.Assert(thisThreadCompleted);
diagnostics.Free();
}
}
break;

case CompletionPart.None:
return;
@@ -1740,6 +1752,10 @@ static bool needsTupleElementNamesAttribute(TypeSymbol type)
}
}

protected virtual void AfterMembersCompletedChecks(BindingDiagnosticBag diagnostics)
{
}

private void CheckMemberNamesDistinctFromType(BindingDiagnosticBag diagnostics)
{
foreach (var member in GetMembersAndInitializers().NonTypeMembers)
Original file line number Diff line number Diff line change
@@ -197,7 +197,6 @@ internal static DeclarationModifiers MakeModifiers(NamedTypeSymbol containingTyp

if ((result & DeclarationModifiers.Required) != 0)
{
// PROTOTYPE(req): capture the allowed modifier combinations in the specification
// The modifier 'required' is not valid for this item
diagnostics.Add(ErrorCode.ERR_BadMemberFlag, errorLocation, SyntaxFacts.GetText(SyntaxKind.RequiredKeyword));
}
Original file line number Diff line number Diff line change
@@ -1634,5 +1634,41 @@ internal bool IsSimpleProgram
return this.declaration.Declarations.Any(d => d.IsSimpleProgram);
}
}

protected override void AfterMembersCompletedChecks(BindingDiagnosticBag diagnostics)
{
base.AfterMembersCompletedChecks(diagnostics);

// We need to give warnings if Obsolete is applied to any required members and there are constructors where a user would be forced to set that member,
// unless:
// 1. We're in an obsolete context ourselves, or
// 2. All constructors of this type are obsolete or attributed with SetsRequiredMembersAttribute
// We don't warn for required members from base types, as the user either has a warning that they're depending on an obsolete constructor/type already,
// or the original author ignored this warning.

// Obsolete states should have already been calculated by this point in the pipeline.
Debug.Assert(ObsoleteKind != ObsoleteAttributeKind.Uninitialized);
Debug.Assert(GetMembers().All(m => m.ObsoleteKind != ObsoleteAttributeKind.Uninitialized));

if (ObsoleteKind != ObsoleteAttributeKind.None
|| GetMembers().All(m => m is not MethodSymbol { MethodKind: MethodKind.Constructor, ObsoleteKind: ObsoleteAttributeKind.None }
|| !((MethodSymbol)m).ShouldCheckRequiredMembers()))
333fred marked this conversation as resolved.
Show resolved Hide resolved
{
return;
}

foreach (var member in GetMembers())
{
if (!member.IsRequired())
{
continue;
}

if (member.ObsoleteKind != ObsoleteAttributeKind.None)
{
diagnostics.Add(ErrorCode.WRN_ObsoleteMembersShouldNotBeRequired, member.Locations[0]);
}
}
}
}
}
10 changes: 10 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading