Skip to content
Open
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
11 changes: 10 additions & 1 deletion src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2086,7 +2086,16 @@ private BoundExpression BindNonMethod(SimpleNameSyntax node, Symbol symbol, Bind
(this.ContainingMember() is MethodSymbol { MethodKind: MethodKind.Constructor } containingMember && (object)containingMember != primaryCtor)) && // We are in a non-primary instance constructor
!IsInsideNameof)
{
Error(diagnostics, ErrorCode.ERR_InvalidPrimaryConstructorParameterReference, node, parameter);
// Give a better error for the simple case of using a primary constructor parameter in a static member
if (this.ContainingMember() is { IsStatic: true } && !InParameterDefaultValue && !InAttributeArgument &&
Copy link
Contributor

Choose a reason for hiding this comment

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

!InParameterDefaultValue

Is there a test that fails if condition !InParameterDefaultValue is removed?

Copy link
Contributor

Choose a reason for hiding this comment

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

!InAttributeArgument

Is there a test that fails if condition !InAttributeArgument is removed?

(object)this.ContainingMember().ContainingSymbol == primaryCtor.ContainingSymbol)
Comment on lines +2090 to +2091
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps we could do something like this to avoid calling ContainingMember() twice (since it seems to do potentially non-trivial work):

Suggested change
if (this.ContainingMember() is { IsStatic: true } && !InParameterDefaultValue && !InAttributeArgument &&
(object)this.ContainingMember().ContainingSymbol == primaryCtor.ContainingSymbol)
if (this.ContainingMember() is { IsStatic: true } containingMember && !InParameterDefaultValue && !InAttributeArgument &&
(object)containingMember.ContainingSymbol == primaryCtor.ContainingSymbol)

{
Error(diagnostics, ErrorCode.ERR_PrimaryConstructorParameterInStaticContext, node, parameter.Name);
}
else
{
Error(diagnostics, ErrorCode.ERR_InvalidPrimaryConstructorParameterReference, node, parameter);
}
}
else if (parameter.IsExtensionParameter() &&
(InParameterDefaultValue || InAttributeArgument ||
Expand Down
3 changes: 3 additions & 0 deletions src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -8238,4 +8238,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<value>'{0}' does not contain a definition for '{1}' and no accessible extension method '{1}' accepting a first argument of type '{0}' could be found (did you mean to iterate over the async collection with 'await foreach' instead?)</value>
<comment>'await foreach' is not localizable</comment>
</data>
<data name="ERR_PrimaryConstructorParameterInStaticContext" xml:space="preserve">
<value>Cannot use primary constructor parameter '{0}' in a static member.</value>
</data>
</root>
1 change: 1 addition & 0 deletions src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2462,6 +2462,7 @@ internal enum ErrorCode
ERR_CollectionArgumentsMustBeEmpty = 9357,
ERR_CollectionRefLikeElementType = 9358,
ERR_BadCollectionArgumentsArgCount = 9359,
ERR_PrimaryConstructorParameterInStaticContext = 9360,

// Note: you will need to do the following after adding errors:
// 1) Update ErrorFacts.IsBuildOnlyDiagnostic (src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs)
Expand Down
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 @@ -2346,6 +2346,7 @@ or ErrorCode.ERR_UnscopedRefAttributeUnsupportedMemberTarget
or ErrorCode.ERR_UnscopedRefAttributeInterfaceImplementation
or ErrorCode.ERR_UnrecognizedRefSafetyRulesAttributeVersion
or ErrorCode.ERR_InvalidPrimaryConstructorParameterReference
or ErrorCode.ERR_PrimaryConstructorParameterInStaticContext
or ErrorCode.ERR_AmbiguousPrimaryConstructorParameterAsColorColorReceiver
or ErrorCode.WRN_CapturedPrimaryConstructorParameterPassedToBase
or ErrorCode.WRN_UnreadPrimaryConstructorParameter
Expand Down
5 changes: 5 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.

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

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

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

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

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

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

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

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf

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

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf

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

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf

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

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

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

Loading
Loading