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
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -7136,6 +7136,15 @@ 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>Required member '{0}' should not be attributed with 'ObsoleteAttribute' unless the containing type is obsolete or all constructors are obsolete.</value>
</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
<data name="ERR_RefReturningPropertiesCannotBeRequired" xml:space="preserve">
<value>Ref returning properties cannot be required.</value>
</data>
<data name="ERR_MisplacedUnchecked" xml:space="preserve">
<value>Unexpected keyword 'unchecked'</value>
</data>
Expand Down
2 changes: 2 additions & 0 deletions src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2089,5 +2089,7 @@ internal enum ErrorCode
ERR_ChainingToSetsRequiredMembersRequiresSetsRequiredMembers = 9510,
ERR_NewConstraintCannotHaveRequiredMembers = 9511,
ERR_UnsupportedCompilerFeature = 9512,
WRN_ObsoleteMembersShouldNotBeRequired = 9513,
ERR_RefReturningPropertiesCannotBeRequired = 9514,
}
}
1 change: 1 addition & 0 deletions src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ void checkMemberStateOnConstructorExit(MethodSymbol constructor, Symbol member,
return;
}

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

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
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@ internal override void ForceComplete(SourceLocation? locationOpt, CancellationTo
}
break;

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

EnsureFieldDefinitionsNoted();
cancellationToken.ThrowIfCancellationRequested();

// 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.

{
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;
Expand Down Expand Up @@ -1744,6 +1755,10 @@ static bool needsTupleElementNamesAttribute(TypeSymbol type)
}
}

protected virtual void AfterMembersCompletedChecks(BindingDiagnosticBag diagnostics)
{
}

private void CheckMemberNamesDistinctFromType(BindingDiagnosticBag diagnostics)
{
foreach (var member in GetMembersAndInitializers().NonTypeMembers)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1638,5 +1638,42 @@ 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 obsolete 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 } method
|| !method.ShouldCheckRequiredMembers()))
{
return;
}

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

if (member.ObsoleteKind != ObsoleteAttributeKind.None)
{
// Required member '{0}' should not be attributed with 'ObsoleteAttribute' unless the containing type is obsolete or all constructors are obsolete.
diagnostics.Add(ErrorCode.WRN_ObsoleteMembersShouldNotBeRequired, member.Locations[0], member);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,12 @@ internal override void AfterAddingTypeMembersChecks(ConversionsBase conversions,
CheckInitializer(IsAutoProperty, ContainingType.IsInterface, IsStatic, Location, diagnostics);
}

if (RefKind != RefKind.None && IsRequired)
{
// Ref returning properties cannot be required.
diagnostics.Add(ErrorCode.ERR_RefReturningPropertiesCannotBeRequired, Location);
}

if (IsAutoPropertyWithGetAccessor)
{
Debug.Assert(GetMethod is object);
Expand Down
15 changes: 15 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.

15 changes: 15 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.

15 changes: 15 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.

15 changes: 15 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.

Loading