Skip to content

Commit

Permalink
Partial properties: diagnostics for mismatch between parts (#73250)
Browse files Browse the repository at this point in the history
  • Loading branch information
RikkiGibson committed May 7, 2024
1 parent c3f13e3 commit 176000a
Show file tree
Hide file tree
Showing 45 changed files with 2,435 additions and 697 deletions.
55 changes: 35 additions & 20 deletions src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -2210,14 +2210,14 @@ If such a class is used as a base class and if the deriving class defines a dest
<data name="ERR_InconsistentLambdaParameterUsage" xml:space="preserve">
<value>Inconsistent lambda parameter usage; parameter types must be all explicit or all implicit</value>
</data>
<data name="ERR_PartialMethodInvalidModifier" xml:space="preserve">
<value>A partial method cannot have the 'abstract' modifier</value>
<data name="ERR_PartialMemberCannotBeAbstract" xml:space="preserve">
<value>A partial member cannot have the 'abstract' modifier</value>
</data>
<data name="ERR_PartialMethodOnlyInPartialClass" xml:space="preserve">
<value>A partial method must be declared within a partial type</value>
<data name="ERR_PartialMemberOnlyInPartialClass" xml:space="preserve">
<value>A partial member must be declared within a partial type</value>
</data>
<data name="ERR_PartialMethodNotExplicit" xml:space="preserve">
<value>A partial method may not explicitly implement an interface method</value>
<data name="ERR_PartialMemberNotExplicit" xml:space="preserve">
<value>A partial member may not explicitly implement an interface member</value>
</data>
<data name="ERR_PartialMethodExtensionDifference" xml:space="preserve">
<value>Both partial method declarations must be extension methods or neither may be an extension method</value>
Expand All @@ -2234,20 +2234,20 @@ If such a class is used as a base class and if the deriving class defines a dest
<data name="ERR_PartialMethodMustHaveLatent" xml:space="preserve">
<value>No defining declaration found for implementing declaration of partial method '{0}'</value>
</data>
<data name="ERR_PartialMethodInconsistentTupleNames" xml:space="preserve">
<value>Both partial method declarations, '{0}' and '{1}', must use the same tuple element names.</value>
<data name="ERR_PartialMemberInconsistentTupleNames" xml:space="preserve">
<value>Both partial member declarations, '{0}' and '{1}', must use the same tuple element names.</value>
</data>
<data name="ERR_PartialMethodInconsistentConstraints" xml:space="preserve">
<value>Partial method declarations of '{0}' have inconsistent constraints for type parameter '{1}'</value>
</data>
<data name="ERR_PartialMethodToDelegate" xml:space="preserve">
<value>Cannot create delegate from method '{0}' because it is a partial method without an implementing declaration</value>
</data>
<data name="ERR_PartialMethodStaticDifference" xml:space="preserve">
<value>Both partial method declarations must be static or neither may be static</value>
<data name="ERR_PartialMemberStaticDifference" xml:space="preserve">
<value>Both partial member declarations must be static or neither may be static</value>
</data>
<data name="ERR_PartialMethodUnsafeDifference" xml:space="preserve">
<value>Both partial method declarations must be unsafe or neither may be unsafe</value>
<data name="ERR_PartialMemberUnsafeDifference" xml:space="preserve">
<value>Both partial member declarations must be unsafe or neither may be unsafe</value>
</data>
<data name="ERR_PartialMethodInExpressionTree" xml:space="preserve">
<value>Partial methods with only a defining declaration or removed conditional methods cannot be used in expression trees</value>
Expand Down Expand Up @@ -5752,8 +5752,8 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="ERR_FieldLikeEventCantBeReadOnly" xml:space="preserve">
<value>Field-like event '{0}' cannot be 'readonly'.</value>
</data>
<data name="ERR_PartialMethodReadOnlyDifference" xml:space="preserve">
<value>Both partial method declarations must be readonly or neither may be readonly</value>
<data name="ERR_PartialMemberReadOnlyDifference" xml:space="preserve">
<value>Both partial member declarations must be readonly or neither may be readonly</value>
</data>
<data name="ERR_ReadOnlyModMissingAccessor" xml:space="preserve">
<value>'{0}': 'readonly' can only be used on accessors if the property or indexer has both a get and a set accessor</value>
Expand Down Expand Up @@ -6536,17 +6536,17 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="ERR_PartialMethodWithExtendedModMustHaveAccessMods" xml:space="preserve">
<value>Partial method '{0}' must have accessibility modifiers because it has a 'virtual', 'override', 'sealed', 'new', or 'extern' modifier.</value>
</data>
<data name="ERR_PartialMethodAccessibilityDifference" xml:space="preserve">
<value>Both partial method declarations must have identical accessibility modifiers.</value>
<data name="ERR_PartialMemberAccessibilityDifference" xml:space="preserve">
<value>Both partial member declarations must have identical accessibility modifiers.</value>
</data>
<data name="ERR_PartialMethodExtendedModDifference" xml:space="preserve">
<value>Both partial method declarations must have identical combinations of 'virtual', 'override', 'sealed', and 'new' modifiers.</value>
<data name="ERR_PartialMemberExtendedModDifference" xml:space="preserve">
<value>Both partial member declarations must have identical combinations of 'virtual', 'override', 'sealed', and 'new' modifiers.</value>
</data>
<data name="ERR_PartialMethodReturnTypeDifference" xml:space="preserve">
<value>Both partial method declarations must have the same return type.</value>
</data>
<data name="ERR_PartialMethodRefReturnDifference" xml:space="preserve">
<value>Partial method declarations must have matching ref return values.</value>
<data name="ERR_PartialMemberRefReturnDifference" xml:space="preserve">
<value>Partial member declarations must have matching ref return values.</value>
</data>
<data name="WRN_PartialMethodTypeDifference" xml:space="preserve">
<value>Partial method declarations '{0}' and '{1}' have signature differences.</value>
Expand Down Expand Up @@ -7947,4 +7947,19 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="ERR_PartialPropertyUnexpectedAccessor" xml:space="preserve">
<value>Property accessor '{0}' does not implement any accessor declared on the definition part</value>
</data>
<data name="ERR_PartialPropertyInitMismatch" xml:space="preserve">
<value>Property accessor '{0}' must be '{1}' to match the definition part</value>
</data>
<data name="ERR_PartialPropertyTypeDifference" xml:space="preserve">
<value>Both partial property declarations must have the same type.</value>
</data>
<data name="WRN_PartialPropertySignatureDifference" xml:space="preserve">
<value>Partial property declarations '{0}' and '{1}' have signature differences.</value>
</data>
<data name="WRN_PartialPropertySignatureDifference_Title" xml:space="preserve">
<value>Partial property declarations have signature differences.</value>
</data>
<data name="ERR_PartialPropertyRequiredDifference" xml:space="preserve">
<value>Both partial property declarations must be required or neither may be required</value>
</data>
</root>
24 changes: 14 additions & 10 deletions src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -529,20 +529,20 @@ internal enum ErrorCode
ERR_InvalidAnonymousTypeMemberDeclarator = 746,
ERR_InvalidInitializerElementInitializer = 747,
ERR_InconsistentLambdaParameterUsage = 748,
ERR_PartialMethodInvalidModifier = 750,
ERR_PartialMethodOnlyInPartialClass = 751,
ERR_PartialMemberCannotBeAbstract = 750,
ERR_PartialMemberOnlyInPartialClass = 751,
// ERR_PartialMethodCannotHaveOutParameters = 752, Removed as part of 'extended partial methods' feature
// ERR_PartialMethodOnlyMethods = 753, Removed as it is subsumed by ERR_PartialMisplaced
ERR_PartialMethodNotExplicit = 754,
ERR_PartialMemberNotExplicit = 754,
ERR_PartialMethodExtensionDifference = 755,
ERR_PartialMethodOnlyOneLatent = 756,
ERR_PartialMethodOnlyOneActual = 757,
ERR_PartialMethodParamsDifference = 758,
ERR_PartialMethodMustHaveLatent = 759,
ERR_PartialMethodInconsistentConstraints = 761,
ERR_PartialMethodToDelegate = 762,
ERR_PartialMethodStaticDifference = 763,
ERR_PartialMethodUnsafeDifference = 764,
ERR_PartialMemberStaticDifference = 763,
ERR_PartialMemberUnsafeDifference = 764,
ERR_PartialMethodInExpressionTree = 765,
// ERR_PartialMethodMustReturnVoid = 766, Removed as part of 'extended partial methods' feature
ERR_ExplicitImplCollisionOnRefOut = 767,
Expand Down Expand Up @@ -1392,7 +1392,7 @@ internal enum ErrorCode
ERR_CantChangeTupleNamesOnOverride = 8139,
ERR_DuplicateInterfaceWithTupleNamesInBaseList = 8140,
ERR_ImplBadTupleNames = 8141,
ERR_PartialMethodInconsistentTupleNames = 8142,
ERR_PartialMemberInconsistentTupleNames = 8142,
ERR_ExpressionTreeContainsTupleLiteral = 8143,
ERR_ExpressionTreeContainsTupleConversion = 8144,
#endregion tuple diagnostics introduced in C# 7
Expand Down Expand Up @@ -1709,7 +1709,7 @@ internal enum ErrorCode
ERR_InvalidPropertyReadOnlyMods = 8660,
ERR_DuplicatePropertyReadOnlyMods = 8661,
ERR_FieldLikeEventCantBeReadOnly = 8662,
ERR_PartialMethodReadOnlyDifference = 8663,
ERR_PartialMemberReadOnlyDifference = 8663,
ERR_ReadOnlyModMissingAccessor = 8664,
ERR_OverrideRefConstraintNotSatisfied = 8665,
ERR_OverrideValConstraintNotSatisfied = 8666,
Expand Down Expand Up @@ -1809,8 +1809,8 @@ internal enum ErrorCode
ERR_PartialMethodWithNonVoidReturnMustHaveAccessMods = 8796,
ERR_PartialMethodWithOutParamMustHaveAccessMods = 8797,
ERR_PartialMethodWithExtendedModMustHaveAccessMods = 8798,
ERR_PartialMethodAccessibilityDifference = 8799,
ERR_PartialMethodExtendedModDifference = 8800,
ERR_PartialMemberAccessibilityDifference = 8799,
ERR_PartialMemberExtendedModDifference = 8800,

ERR_SimpleProgramLocalIsReferencedOutsideOfTopLevelStatement = 8801,
ERR_SimpleProgramMultipleUnitsWithTopLevelStatements = 8802,
Expand All @@ -1832,7 +1832,7 @@ internal enum ErrorCode
ERR_ModuleInitializerMethodAndContainingTypesMustNotBeGeneric = 8816,

ERR_PartialMethodReturnTypeDifference = 8817,
ERR_PartialMethodRefReturnDifference = 8818,
ERR_PartialMemberRefReturnDifference = 8818,
WRN_NullabilityMismatchInReturnTypeOnPartial = 8819,

ERR_StaticAnonymousFunctionCannotCaptureVariable = 8820,
Expand Down Expand Up @@ -2317,6 +2317,10 @@ internal enum ErrorCode
ERR_PartialPropertyDuplicateImplementation = 9303,
ERR_PartialPropertyMissingAccessor = 9304,
ERR_PartialPropertyUnexpectedAccessor = 9305,
ERR_PartialPropertyInitMismatch = 9306,
ERR_PartialPropertyTypeDifference = 9307,
WRN_PartialPropertySignatureDifference = 9308,
ERR_PartialPropertyRequiredDifference = 9309,

#endregion

Expand Down
25 changes: 15 additions & 10 deletions src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ internal static int GetWarningLevel(ErrorCode code)
case ErrorCode.WRN_DynamicDispatchToParamsCollectionMethod:
case ErrorCode.WRN_DynamicDispatchToParamsCollectionIndexer:
case ErrorCode.WRN_DynamicDispatchToParamsCollectionConstructor:
case ErrorCode.WRN_PartialPropertySignatureDifference:

return 1;
default:
Expand Down Expand Up @@ -1069,18 +1070,18 @@ internal static bool IsBuildOnlyDiagnostic(ErrorCode code)
case ErrorCode.ERR_InvalidAnonymousTypeMemberDeclarator:
case ErrorCode.ERR_InvalidInitializerElementInitializer:
case ErrorCode.ERR_InconsistentLambdaParameterUsage:
case ErrorCode.ERR_PartialMethodInvalidModifier:
case ErrorCode.ERR_PartialMethodOnlyInPartialClass:
case ErrorCode.ERR_PartialMethodNotExplicit:
case ErrorCode.ERR_PartialMemberCannotBeAbstract:
case ErrorCode.ERR_PartialMemberOnlyInPartialClass:
case ErrorCode.ERR_PartialMemberNotExplicit:
case ErrorCode.ERR_PartialMethodExtensionDifference:
case ErrorCode.ERR_PartialMethodOnlyOneLatent:
case ErrorCode.ERR_PartialMethodOnlyOneActual:
case ErrorCode.ERR_PartialMethodParamsDifference:
case ErrorCode.ERR_PartialMethodMustHaveLatent:
case ErrorCode.ERR_PartialMethodInconsistentConstraints:
case ErrorCode.ERR_PartialMethodToDelegate:
case ErrorCode.ERR_PartialMethodStaticDifference:
case ErrorCode.ERR_PartialMethodUnsafeDifference:
case ErrorCode.ERR_PartialMemberStaticDifference:
case ErrorCode.ERR_PartialMemberUnsafeDifference:
case ErrorCode.ERR_PartialMethodInExpressionTree:
case ErrorCode.ERR_ExplicitImplCollisionOnRefOut:
case ErrorCode.ERR_IndirectRecursiveConstructorCall:
Expand Down Expand Up @@ -1744,7 +1745,7 @@ internal static bool IsBuildOnlyDiagnostic(ErrorCode code)
case ErrorCode.ERR_CantChangeTupleNamesOnOverride:
case ErrorCode.ERR_DuplicateInterfaceWithTupleNamesInBaseList:
case ErrorCode.ERR_ImplBadTupleNames:
case ErrorCode.ERR_PartialMethodInconsistentTupleNames:
case ErrorCode.ERR_PartialMemberInconsistentTupleNames:
case ErrorCode.ERR_ExpressionTreeContainsTupleLiteral:
case ErrorCode.ERR_ExpressionTreeContainsTupleConversion:
case ErrorCode.ERR_AutoPropertyCannotBeRefReturning:
Expand Down Expand Up @@ -1983,7 +1984,7 @@ internal static bool IsBuildOnlyDiagnostic(ErrorCode code)
case ErrorCode.ERR_InvalidPropertyReadOnlyMods:
case ErrorCode.ERR_DuplicatePropertyReadOnlyMods:
case ErrorCode.ERR_FieldLikeEventCantBeReadOnly:
case ErrorCode.ERR_PartialMethodReadOnlyDifference:
case ErrorCode.ERR_PartialMemberReadOnlyDifference:
case ErrorCode.ERR_ReadOnlyModMissingAccessor:
case ErrorCode.ERR_OverrideRefConstraintNotSatisfied:
case ErrorCode.ERR_OverrideValConstraintNotSatisfied:
Expand Down Expand Up @@ -2053,8 +2054,8 @@ internal static bool IsBuildOnlyDiagnostic(ErrorCode code)
case ErrorCode.ERR_PartialMethodWithNonVoidReturnMustHaveAccessMods:
case ErrorCode.ERR_PartialMethodWithOutParamMustHaveAccessMods:
case ErrorCode.ERR_PartialMethodWithExtendedModMustHaveAccessMods:
case ErrorCode.ERR_PartialMethodAccessibilityDifference:
case ErrorCode.ERR_PartialMethodExtendedModDifference:
case ErrorCode.ERR_PartialMemberAccessibilityDifference:
case ErrorCode.ERR_PartialMemberExtendedModDifference:
case ErrorCode.ERR_SimpleProgramLocalIsReferencedOutsideOfTopLevelStatement:
case ErrorCode.ERR_SimpleProgramMultipleUnitsWithTopLevelStatements:
case ErrorCode.ERR_TopLevelStatementAfterNamespaceOrType:
Expand All @@ -2072,7 +2073,7 @@ internal static bool IsBuildOnlyDiagnostic(ErrorCode code)
case ErrorCode.ERR_ModuleInitializerMethodMustBeStaticParameterlessVoid:
case ErrorCode.ERR_ModuleInitializerMethodAndContainingTypesMustNotBeGeneric:
case ErrorCode.ERR_PartialMethodReturnTypeDifference:
case ErrorCode.ERR_PartialMethodRefReturnDifference:
case ErrorCode.ERR_PartialMemberRefReturnDifference:
case ErrorCode.WRN_NullabilityMismatchInReturnTypeOnPartial:
case ErrorCode.ERR_StaticAnonymousFunctionCannotCaptureVariable:
case ErrorCode.ERR_StaticAnonymousFunctionCannotCaptureThis:
Expand Down Expand Up @@ -2445,6 +2446,10 @@ internal static bool IsBuildOnlyDiagnostic(ErrorCode code)
case ErrorCode.ERR_PartialPropertyDuplicateImplementation:
case ErrorCode.ERR_PartialPropertyMissingAccessor:
case ErrorCode.ERR_PartialPropertyUnexpectedAccessor:
case ErrorCode.ERR_PartialPropertyInitMismatch:
case ErrorCode.ERR_PartialPropertyTypeDifference:
case ErrorCode.WRN_PartialPropertySignatureDifference:
case ErrorCode.ERR_PartialPropertyRequiredDifference:
return false;
default:
// NOTE: All error codes must be explicitly handled in this switch statement
Expand Down

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

Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@ internal static bool IsExplicitInterfaceImplementation(this Symbol member)

internal static bool IsPartialMember(this Symbol member)
{
Debug.Assert(member.IsDefinition);
return member
is SourceOrdinaryMethodSymbol { IsPartial: true }
or SourcePropertySymbol { IsPartial: true }
Expand All @@ -555,6 +556,7 @@ internal static bool IsPartialMember(this Symbol member)

internal static bool IsPartialImplementation(this Symbol member)
{
Debug.Assert(member.IsDefinition);
return member
is SourceOrdinaryMethodSymbol { IsPartialImplementation: true }
or SourcePropertySymbol { IsPartialImplementation: true }
Expand All @@ -563,6 +565,7 @@ internal static bool IsPartialImplementation(this Symbol member)

internal static bool IsPartialDefinition(this Symbol member)
{
Debug.Assert(member.IsDefinition);
return member
is SourceOrdinaryMethodSymbol { IsPartialDefinition: true }
or SourcePropertySymbol { IsPartialDefinition: true }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,9 @@ IMethodSymbol IMethodSymbol.PartialDefinitionPart
}
}

bool IMethodSymbol.IsPartialDefinition => _underlying.IsPartialDefinition();
// PROTOTYPE(partial-properties): Perhaps this API should be implemented as '_underlying.OriginalDefinition.IsPartialDefinition()' instead.
// However, this would be a behavior change. Callers may have been assuming that if the API returned true, then the receiver is an original definition symbol.
bool IMethodSymbol.IsPartialDefinition => _underlying.IsDefinition && _underlying.IsPartialDefinition();

INamedTypeSymbol IMethodSymbol.AssociatedAnonymousDelegate
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ public sealed override bool ReturnsVoid
{
if (MethodKind == MethodKind.ExplicitInterfaceImplementation)
{
diagnostics.Add(ErrorCode.ERR_PartialMethodNotExplicit, _location);
diagnostics.Add(ErrorCode.ERR_PartialMemberNotExplicit, _location);
}

if (!ContainingType.IsPartial())
{
diagnostics.Add(ErrorCode.ERR_PartialMethodOnlyInPartialClass, _location);
diagnostics.Add(ErrorCode.ERR_PartialMemberOnlyInPartialClass, _location);
}
}

Expand Down

0 comments on commit 176000a

Please sign in to comment.