Skip to content
Closed
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
3aa5a34
nameof is allowed to access instance members in a static context, and…
YairHalberstadt Oct 19, 2020
c62e78f
Merge branch 'master' of https://github.com/dotnet/roslyn into allow-…
YairHalberstadt Nov 16, 2020
c2149e4
Only allow nameof to access members in static context in preview lang…
YairHalberstadt Nov 16, 2020
74dbb47
Merge branch 'master' into allow-nameof-to-access-instance-members
YairHalberstadt Dec 23, 2020
dd4df9f
Changes based on review by @333fred.
YairHalberstadt Dec 24, 2020
561251f
Fix typo and build
YairHalberstadt Dec 24, 2020
1ccdd6f
Fix formatting
YairHalberstadt Dec 25, 2020
fbc0573
Changes based on review by @333fred. Stylistic changes and adding tests.
YairHalberstadt Dec 30, 2020
5e6ff0a
Add another test
YairHalberstadt Dec 30, 2020
c57bd7f
merge main into allow-nameof-to-access-instance-members
YairHalberstadt Dec 29, 2021
e241186
Changes in response to review by @333fred
YairHalberstadt Dec 29, 2021
63e5828
fix build
YairHalberstadt Dec 30, 2021
9f8216f
Merge remote-tracking branch 'upstream/main' into allow-nameof-to-acc…
333fred Jan 28, 2022
f6775b0
Fix test baseline and formatting.
333fred Jan 28, 2022
b022c23
Merge branch 'main' into YairHalberstadt/allow-nameof-to-access-insta…
jjonescz Mar 22, 2023
5f8c58e
Revert whitespace change
jjonescz Mar 22, 2023
7ce5681
Update verified diagnostic
jjonescz Mar 22, 2023
34b7a53
Extend tests
jjonescz Mar 23, 2023
ad3e699
Avoid discarding diagnostic dependencies
jjonescz Mar 23, 2023
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
31 changes: 22 additions & 9 deletions src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1956,18 +1956,19 @@ private BoundExpression SynthesizeReceiver(SyntaxNode node, Symbol member, Bindi
(currentType.IsInterface && (declaringType.IsObjectType() || currentType.AllInterfacesNoUseSiteDiagnostics.Contains(declaringType))))
{
bool hasErrors = false;
if (EnclosingNameofArgument != node)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@gafter I think you wrote this code originally. Was it intentional to not allow nameof(InstanceProperty.Member) in a static context, but allow nameof(InstanceProperty)?

if (!IsInsideNameof || (EnclosingNameofArgument != node && !IsFeatureAvailable(node, MessageID.IDS_FeatureReducedMemberAccessChecksInNameof)))
{
var diagnosticsTemp = IsInsideNameof ? BindingDiagnosticBag.Discarded : diagnostics;
Comment thread
AlekseyTs marked this conversation as resolved.
Outdated
if (InFieldInitializer && !currentType.IsScriptClass)
{
//can't access "this" in field initializers
Error(diagnostics, ErrorCode.ERR_FieldInitRefNonstatic, node, member);
Error(diagnosticsTemp, ErrorCode.ERR_FieldInitRefNonstatic, node, member);
hasErrors = true;
}
else if (InConstructorInitializer || InAttributeArgument)
{
//can't access "this" in constructor initializers or attribute arguments
Error(diagnostics, ErrorCode.ERR_ObjectRequired, node, member);
Error(diagnosticsTemp, ErrorCode.ERR_ObjectRequired, node, member);
hasErrors = true;
}
else
Expand All @@ -1980,12 +1981,17 @@ private BoundExpression SynthesizeReceiver(SyntaxNode node, Symbol member, Bindi
if (!locationIsInstanceMember)
{
// error CS0120: An object reference is required for the non-static field, method, or property '{0}'
Error(diagnostics, ErrorCode.ERR_ObjectRequired, node, member);
Error(diagnosticsTemp, ErrorCode.ERR_ObjectRequired, node, member);
hasErrors = true;
}
}

hasErrors = hasErrors || IsRefOrOutThisParameterCaptured(node, diagnostics);
hasErrors = hasErrors || IsRefOrOutThisParameterCaptured(node, diagnosticsTemp);

if (hasErrors && IsInsideNameof)
{
CheckFeatureAvailability(node, MessageID.IDS_FeatureReducedMemberAccessChecksInNameof, diagnostics);
}
}

return ThisReference(node, currentType, hasErrors, wasCompilerGenerated: true);
Expand Down Expand Up @@ -7255,10 +7261,17 @@ private bool CheckInstanceOrStatic(
{
if (instanceReceiver == true)
{
ErrorCode errorCode = this.Flags.Includes(BinderFlags.ObjectInitializerMember) ?
ErrorCode.ERR_StaticMemberInObjectInitializer :
ErrorCode.ERR_ObjectProhibited;
Error(diagnostics, errorCode, node, symbol);
if (!IsInsideNameof)
{
ErrorCode errorCode = this.Flags.Includes(BinderFlags.ObjectInitializerMember) ?
ErrorCode.ERR_StaticMemberInObjectInitializer :
ErrorCode.ERR_ObjectProhibited;
Error(diagnostics, errorCode, node, symbol);
}
else if (CheckFeatureAvailability(node, MessageID.IDS_FeatureReducedMemberAccessChecksInNameof, diagnostics))
{
return false;
}
resultKind = LookupResultKind.StaticInstanceMismatch;
return true;
}
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/Binder/Binder_Symbols.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2499,6 +2499,11 @@ protected AssemblySymbol GetForwardedToAssembly(string name, int arity, ref Name
}

#nullable enable
internal static bool IsFeatureAvailable(SyntaxNode syntax, MessageID feature)
{
return ((CSharpParseOptions)syntax.SyntaxTree.Options).IsFeatureEnabled(feature);
}

internal static bool CheckFeatureAvailability(SyntaxNode syntax, MessageID feature, BindingDiagnosticBag diagnostics, Location? location = null)
{
return CheckFeatureAvailability(syntax, feature, diagnostics.DiagnosticBag, location);
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 @@ -6676,6 +6676,9 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="WRN_UnreadRecordParameter_Title" xml:space="preserve">
<value>Parameter is unread. Did you forget to use it to initialize the property with that name?</value>
</data>
<data name="IDS_FeatureReducedMemberAccessChecksInNameof" xml:space="preserve">
<value>reduced member access checks in 'nameof'</value>
Comment thread
AlekseyTs marked this conversation as resolved.
</data>
<data name="ERR_RecordAmbigCtor" xml:space="preserve">
<value>The primary constructor conflicts with the synthesized copy constructor.</value>
</data>
Expand Down
3 changes: 2 additions & 1 deletion src/Compilers/CSharp/Portable/Errors/MessageID.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ internal enum MessageID
IDS_FeatureNewLinesInInterpolations = MessageBase + 12813,
IDS_FeatureListPattern = MessageBase + 12814,
IDS_ParameterNullChecking = MessageBase + 12815,

IDS_FeatureCacheStaticMethodGroupConversion = MessageBase + 12816,
IDS_FeatureReducedMemberAccessChecksInNameof = MessageBase + 12817,
}

// Message IDs may refer to strings that need to be localized.
Expand Down Expand Up @@ -358,6 +358,7 @@ internal static LanguageVersion RequiredVersion(this MessageID feature)
case MessageID.IDS_FeatureListPattern: // semantic check
case MessageID.IDS_FeatureCacheStaticMethodGroupConversion: // lowering check
case MessageID.IDS_ParameterNullChecking: // syntax check
case MessageID.IDS_FeatureReducedMemberAccessChecksInNameof:
return LanguageVersion.Preview;

// C# 10.0 features.
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