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

Extensions parsing #76867

Open
wants to merge 13 commits into
base: features/extensions
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 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
26 changes: 26 additions & 0 deletions docs/compilers/CSharp/Compiler Breaking Changes - DotNet 10.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,29 @@ unsafe record struct R(
public bool Equals(R other) => true;
}
```

## `extension` treated as a contextual keyword

PROTOTYPE record which version this break is introduced in

Starting with C# 14, the `extension` keyword serves a special purpose in denoting extension containers.
This changes how the compiler interprets certain code constructs.

If you need to use "extension" as an identifier rather than a keyword, escape it with the `@` prefix: `@extension`. This tells the compiler to treat it as a regular identifier instead of a keyword.

The compiler will parse this as an extension container rather than a constructor.
```csharp
class extension
{
extension(object o) { } // parsed as an extension container
}
```

The compiler will fail to parse this as a method with return type `extension`.
```csharp
class extension
{
extension M() { } // will not compile
}
```

5 changes: 4 additions & 1 deletion src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -2600,7 +2600,7 @@ A catch() block after a catch (System.Exception e) block can catch non-CLS excep
<value>Invalid preprocessor expression</value>
</data>
<data name="ERR_InvalidMemberDecl" xml:space="preserve">
<value>Invalid token '{0}' in class, record, struct, or interface member declaration</value>
<value>Invalid token '{0}' in class, record, struct, interface, or extension member declaration</value>
Copy link
Contributor

@AlekseyTs AlekseyTs Jan 23, 2025

Choose a reason for hiding this comment

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

class, record, struct, interface, or extension member

Would it make sense to simply say "a member" instead of "class, record, struct, interface, or extension member"? #Resolved

</data>
<data name="ERR_MemberNeedsType" xml:space="preserve">
<value>Method must have a return type</value>
Expand Down Expand Up @@ -8047,4 +8047,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="WRN_InterceptsLocationAttributeUnsupportedSignature_Title" xml:space="preserve">
<value>'InterceptsLocationAttribute(string, int, int)' is not supported. Move to 'InterceptableLocation'-based generation of these attributes instead. (https://github.com/dotnet/roslyn/issues/72133)</value>
</data>
<data name="IDS_FeatureExtensions" xml:space="preserve">
<value>extensions</value>
</data>
</root>
3 changes: 3 additions & 0 deletions src/Compilers/CSharp/Portable/Errors/MessageID.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ internal enum MessageID
IDS_FeatureFirstClassSpan = MessageBase + 12849,
IDS_FeatureUnboundGenericTypesInNameof = MessageBase + 12850,
IDS_FeatureSimpleLambdaParameterModifiers = MessageBase + 12851,

IDS_FeatureExtensions = MessageBase + 12852,
}

// Message IDs may refer to strings that need to be localized.
Expand Down Expand Up @@ -480,6 +482,7 @@ internal static LanguageVersion RequiredVersion(this MessageID feature)
case MessageID.IDS_FeatureFirstClassSpan:
case MessageID.IDS_FeatureUnboundGenericTypesInNameof:
case MessageID.IDS_FeatureSimpleLambdaParameterModifiers:
case MessageID.IDS_FeatureExtensions:
return LanguageVersion.Preview;

// C# 13.0 features.
Expand Down

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

Loading
Loading