Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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
16 changes: 11 additions & 5 deletions src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1891,18 +1891,19 @@ private BoundExpression SynthesizeReceiver(SyntaxNode node, Symbol member, Diagn
(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 && !((CSharpParseOptions)node.SyntaxTree.Options).IsFeatureEnabled(MessageID.IDS_FeatureNameofAccessInstanceMembersInAllContexts))
Comment thread
333fred marked this conversation as resolved.
Outdated
{
var diagnosticsTemp = IsInsideNameof ? new DiagnosticBag() : diagnostics;
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 @@ -1915,12 +1916,17 @@ private BoundExpression SynthesizeReceiver(SyntaxNode node, Symbol member, Diagn
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_FeatureNameofAccessInstanceMembersInAllContexts, diagnostics);
}
}

return ThisReference(node, currentType, hasErrors, wasCompilerGenerated: true);
Expand Down
59 changes: 31 additions & 28 deletions src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema

<!--
Microsoft ResX Schema
Version 2.0

The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.

Example:

... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
Expand All @@ -26,36 +26,36 @@
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>

There are any number of "resheader" rows that contain simple
There are any number of "resheader" rows that contain simple
name/value pairs.

Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.

The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:

Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.

mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
Expand Down Expand Up @@ -6588,4 +6588,7 @@ 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>
</root>
<data name="IDS_FeatureNameofAccessInstanceMembersInAllContexts" xml:space="preserve">
<value>nameof can access instance members in all contexts</value>
Comment thread
333fred marked this conversation as resolved.
Outdated
</data>
</root>
6 changes: 6 additions & 0 deletions src/Compilers/CSharp/Portable/Errors/MessageID.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ internal enum MessageID
IDS_Parameter = MessageBase + 12789,
IDS_Return = MessageBase + 12790,
IDS_FeatureVarianceSafetyForStaticInterfaceMembers = MessageBase + 12791,

IDS_FeatureNameofAccessInstanceMembersInAllContexts = MessageBase + 12792,
}

// Message IDs may refer to strings that need to be localized.
Expand Down Expand Up @@ -320,6 +322,10 @@ internal static LanguageVersion RequiredVersion(this MessageID feature)
// Checks are in the LanguageParser unless otherwise noted.
switch (feature)
{
// preview features.
case MessageID.IDS_FeatureNameofAccessInstanceMembersInAllContexts:
return LanguageVersion.Preview;

// C# 9.0 features.
case MessageID.IDS_FeatureLambdaDiscardParameters: // semantic check
case MessageID.IDS_FeatureFunctionPointers:
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