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

Remove !! parsing logic from the parser #75394

Merged
merged 7 commits into from
Oct 7, 2024
Merged
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
3 changes: 0 additions & 3 deletions src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -6358,9 +6358,6 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="ERR_DuplicateNullSuppression" xml:space="preserve">
<value>Duplicate null suppression operator ('!')</value>
</data>
<data name="ERR_ParameterNullCheckingNotSupported" xml:space="preserve">
<value>The 'parameter null-checking' feature is not supported.</value>
</data>
<data name="ERR_ReAbstractionInNoPIAType" xml:space="preserve">
<value>Type '{0}' cannot be embedded because it has a re-abstraction of a member from base interface. Consider setting the 'Embed Interop Types' property to false.</value>
</data>
Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2029,7 +2029,7 @@ internal enum ErrorCode
ERR_ScopedMismatchInParameterOfPartial = 8988,

// param-nullchecking feature removed from C# 11
ERR_ParameterNullCheckingNotSupported = 8989,
// ERR_ParameterNullCheckingNotSupported = 8989,
// ERR_DiscardCannotBeNullChecked = 8990,
// ERR_MustNullCheckInImplementation = 8991,
// ERR_NonNullableValueTypeIsNullChecked = 8992,
Expand Down
1 change: 0 additions & 1 deletion src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2223,7 +2223,6 @@ or ErrorCode.ERR_ListPatternRequiresLength
or ErrorCode.ERR_ScopedMismatchInParameterOfTarget
or ErrorCode.ERR_ScopedMismatchInParameterOfOverrideOrImplementation
or ErrorCode.ERR_ScopedMismatchInParameterOfPartial
or ErrorCode.ERR_ParameterNullCheckingNotSupported
or ErrorCode.ERR_RawStringNotInDirectives
or ErrorCode.ERR_UnterminatedRawString
or ErrorCode.ERR_TooManyQuotesForRawString
Expand Down
145 changes: 7 additions & 138 deletions src/Compilers/CSharp/Portable/Parser/LanguageParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4697,10 +4697,7 @@ private ParameterSyntax ParseParameter()
this.EatToken()));
}

ParseParameterNullCheck(ref identifier, out var equalsToken);

// If we didn't already consume an equals sign as part of !!=, then try to scan one out now.
equalsToken ??= TryEatToken(SyntaxKind.EqualsToken);
var equalsToken = TryEatToken(SyntaxKind.EqualsToken);

return _syntaxFactory.Parameter(
attributes,
Expand All @@ -4710,80 +4707,6 @@ private ParameterSyntax ParseParameter()
equalsToken == null ? null : _syntaxFactory.EqualsValueClause(equalsToken, this.ParseExpressionCore()));
}

/// <summary>
/// Parses the <c>!!</c> as skipped tokens following a parameter name token. If the parameter name
/// is followed by <c>!!=</c> or <c>! !=</c>, then the final equals will be returned through <paramref
/// name="equalsToken"/>.
/// </summary>
private void ParseParameterNullCheck(
ref SyntaxToken identifier,
out SyntaxToken? equalsToken)
{
equalsToken = null;

if (this.CurrentToken.Kind is SyntaxKind.ExclamationEqualsToken)
{
// split != into two tokens.
var exclamationEquals = this.EatToken();

// treat the '!' as '!!' and give the feature unsupported error
identifier = AddTrailingSkippedSyntax(
identifier,
this.AddError(SyntaxFactory.Token(exclamationEquals.GetLeadingTrivia(), SyntaxKind.ExclamationToken, "!", "!", trailing: null), ErrorCode.ERR_ParameterNullCheckingNotSupported));

// Return the split out `=` for the consumer to handle.
equalsToken = SyntaxFactory.Token(leading: null, SyntaxKind.EqualsToken, exclamationEquals.GetTrailingTrivia());
}
else if (this.CurrentToken.Kind is SyntaxKind.ExclamationToken)
{
// We have seen at least '!'
// We check for a following '!' or '!=' to see if the user is trying to use '!!' (so we can give an appropriate error).
identifier = AddTrailingSkippedSyntax(identifier, this.AddError(this.EatToken(), ErrorCode.ERR_ParameterNullCheckingNotSupported));
if (this.CurrentToken.Kind is SyntaxKind.ExclamationToken)
{
identifier = AddTrailingSkippedSyntax(identifier, this.EatToken());
}
else if (this.CurrentToken.Kind is SyntaxKind.ExclamationEqualsToken)
{
// split != into two tokens.
var exclamationEquals = this.EatToken();

identifier = AddTrailingSkippedSyntax(
identifier,
SyntaxFactory.Token(exclamationEquals.GetLeadingTrivia(), SyntaxKind.ExclamationToken, trailing: null));
equalsToken = SyntaxFactory.Token(leading: null, SyntaxKind.EqualsToken, exclamationEquals.GetTrailingTrivia());
}
}
}

/// <summary>
/// Merges two successive tokens into a single token with the given <paramref name="kind"/>. If the two tokens
/// have no trivia between them, then the final token will be trivially generated, properly passing on the right
/// leading/trailing trivia. However, if there is trivia between the tokens, then appropriate errors will be
/// reported that the tokens cannot merge successfully.
/// </summary>
/// <remarks>
/// IsFabricatedToken should be updated for tokens whose SyntaxKind is <paramref name="kind"/>.
/// </remarks>
private SyntaxToken? MergeAdjacent(SyntaxToken t1, SyntaxToken t2, SyntaxKind kind)
{
// Make sure we don't reuse the merged token for incremental parsing.
// "=>" wasn't proven to be a source of issues. See https://github.com/dotnet/roslyn/issues/60002
Debug.Assert(Blender.Reader.IsFabricatedToken(kind) || kind == SyntaxKind.EqualsGreaterThanToken);
if (NoTriviaBetween(t1, t2))
return SyntaxFactory.Token(t1.GetLeadingTrivia(), kind, t2.GetTrailingTrivia());

var sb = PooledStringBuilder.GetInstance();
var writer = new StringWriter(sb.Builder, System.Globalization.CultureInfo.InvariantCulture);
t1.WriteTo(writer, leading: false, trailing: true);
t2.WriteTo(writer, leading: true, trailing: false);
var text = sb.ToStringAndFree();

return WithAdditionalDiagnostics(
SyntaxFactory.Token(t1.GetLeadingTrivia(), kind, text, text, t2.GetTrailingTrivia()),
GetExpectedTokenError(kind, t1.Kind));
}

internal static bool NoTriviaBetween(SyntaxToken token1, SyntaxToken token2)
=> token1.GetTrailingTriviaWidth() == 0 && token2.GetLeadingTriviaWidth() == 0;

Expand Down Expand Up @@ -12150,13 +12073,8 @@ private bool ScanParenthesizedImplicitlyTypedLambda(Precedence precedence)
// case 2: ( x ) =>
if (IsTrueIdentifier(this.PeekToken(1)))
{
// allow for a) => or a!!) =>
// allow for a) =>
var skipIndex = 2;
if (PeekToken(skipIndex).Kind == SyntaxKind.ExclamationToken
&& this.PeekToken(skipIndex + 1).Kind == SyntaxKind.ExclamationToken)
{
skipIndex += 2;
}

// Must have: ) =>
if (this.PeekToken(skipIndex).Kind == SyntaxKind.CloseParenToken
Expand Down Expand Up @@ -12199,18 +12117,9 @@ bool isParenVarCommaSyntax()
{
return true;
}

var token3 = this.PeekToken(3);
// ( x!! , [...]
// https://github.com/dotnet/roslyn/issues/58335: https://github.com/dotnet/roslyn/pull/46520#discussion_r466650228
if (token2.Kind == SyntaxKind.ExclamationToken
&& token3.Kind == SyntaxKind.ExclamationToken
&& this.PeekToken(4).Kind == SyntaxKind.CommaToken)
{
return true;
}
}
}

return false;
}
}
Expand Down Expand Up @@ -12263,9 +12172,7 @@ private bool ScanExplicitlyTypedLambda(Precedence precedence)
// eat the parameter name.
var identifier = this.IsTrueIdentifier() ? this.EatToken() : CreateMissingIdentifierToken();

// eat a !! if present.
this.ParseParameterNullCheck(ref identifier, out var equalsToken);
equalsToken ??= TryEatToken(SyntaxKind.EqualsToken);
var equalsToken = TryEatToken(SyntaxKind.EqualsToken);

// If we have an `=` then parse out a default value. Note: this is not legal, but this allows us to
// to be resilient to the user writing this so we don't go completely off the rails.
Expand Down Expand Up @@ -12523,25 +12430,6 @@ private bool IsPossibleLambdaExpression(Precedence precedence)
return true;
}

var token2 = this.PeekToken(2);
var token3 = this.PeekToken(3);

if ((token1.Kind, token2.Kind, token3.Kind) is

// x!! =>
//
// Def a lambda (though possibly has errors).
(SyntaxKind.ExclamationToken, SyntaxKind.ExclamationToken, SyntaxKind.EqualsGreaterThanToken)

// Broken case but error will be added in lambda function (!=>).
or (SyntaxKind.ExclamationEqualsToken, SyntaxKind.GreaterThanToken, _)

// Broken case but error will be added in lambda function (!!=>).
or (SyntaxKind.ExclamationToken, SyntaxKind.ExclamationEqualsToken, SyntaxKind.GreaterThanToken))
{
return true;
}

using var _ = this.GetDisposableResetPoint(resetOnDispose: true);

// A lambda could be starting with attributes, attempt to skip past them and check after that point.
Expand Down Expand Up @@ -13358,28 +13246,12 @@ LambdaExpressionSyntax parseLambdaExpressionWorker()
{
// Unparenthesized lambda case
// x => ...
// x!! => ...
var identifier = (this.CurrentToken.Kind != SyntaxKind.IdentifierToken && this.PeekToken(1).Kind == SyntaxKind.EqualsGreaterThanToken)
? this.EatTokenAsKind(SyntaxKind.IdentifierToken)
: this.ParseIdentifierToken();

ParseParameterNullCheck(ref identifier, out var equalsToken);

SyntaxToken arrow;
if (equalsToken != null)
{
// we only get an equals token if we had !!=> or !=> (enforced by IsPossibleLambdaExpression).
// So we must have a greater than token following. If not, some invariant is badly broken.

var greaterThan = this.EatToken();
Debug.Assert(greaterThan.Kind == SyntaxKind.GreaterThanToken);
arrow = MergeAdjacent(equalsToken, greaterThan, SyntaxKind.EqualsGreaterThanToken);
}
else
{
// Case x=>, x =>
arrow = this.EatToken(SyntaxKind.EqualsGreaterThanToken);
}
// Case x=>, x =>
var arrow = this.EatToken(SyntaxKind.EqualsGreaterThanToken);

var parameter = _syntaxFactory.Parameter(
attributeLists: default, modifiers: default, type: null, identifier, @default: null);
Expand Down Expand Up @@ -13470,10 +13342,9 @@ private ParameterSyntax ParseLambdaParameter()
: null;

var identifier = this.ParseIdentifierToken();
ParseParameterNullCheck(ref identifier, out var equalsToken);

// Parse default value if any
equalsToken ??= TryEatToken(SyntaxKind.EqualsToken);
var equalsToken = TryEatToken(SyntaxKind.EqualsToken);

return _syntaxFactory.Parameter(
attributes,
Expand Down Expand Up @@ -13512,7 +13383,6 @@ private bool ShouldParseLambdaParameterType()
// (a)
// (a =>
// (a {
// (a !! or (a !!=
// (a =
//
// In all other cases, parse out a type.
Expand All @@ -13521,7 +13391,6 @@ private bool ShouldParseLambdaParameterType()
peek1.Kind != SyntaxKind.CloseParenToken &&
peek1.Kind != SyntaxKind.EqualsGreaterThanToken &&
peek1.Kind != SyntaxKind.OpenBraceToken &&
peek1.Kind != SyntaxKind.ExclamationToken &&
peek1.Kind != SyntaxKind.EqualsToken)
{
return true;
Expand Down
5 changes: 0 additions & 5 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: 0 additions & 5 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: 0 additions & 5 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: 0 additions & 5 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: 0 additions & 5 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: 0 additions & 5 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: 0 additions & 5 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.

Loading