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

Parse-Error: new-line in string interpolation expressions #50742

Closed
bernd5 opened this issue Jan 24, 2021 · 11 comments
Closed

Parse-Error: new-line in string interpolation expressions #50742

bernd5 opened this issue Jan 24, 2021 · 11 comments
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@bernd5
Copy link
Contributor

bernd5 commented Jan 24, 2021

Version Used: 16.9.0 Preview 3

Sample Code:

Link

using System;

struct Point
{
    public int X;
    public int Y;
    
    public override string ToString() => $"({X}, {Y})";
}

public class App
{

    public static void Main()
    {
        Console.WriteLine($"A Point: {new Point{ 
              X = 1, 
              Y = 2 
        }}");
    }
}

Expected Behavior:
According to csharplang every expression is allowed in an interpolation. So expressions with New-Line trivia should compile.

Actual Behavior:
Code does not compile, just with removing the new lines.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Jan 24, 2021
@CyrusNajmabadi
Copy link
Member

You need to use $@""

Normal $"" strings do not allow newlines in them.

@bernd5
Copy link
Contributor Author

bernd5 commented Jan 25, 2021

The new line does not appear in the string itself! It is inside a normal expression. @ would dissallow escaping / change escaping rules.

Of course we could use verbatim interpolated strings here, string.operator+, local variables or simply string.Format.

@CyrusNajmabadi
Copy link
Member

The new line does not appear in the string itself! It is inside a normal expression.

Yes. I understand that :-)

This is how we designed the language to work. The string literal start/end is discovered first, then internally parsed. Fur these strings, the end can't be found as they are supposed to be in a single line, but a new line is encountered before the end is reached.

@ would dissallow escaping / change escaping rules.

Yes. That is true.

Of course we could use verbatim interpolated strings here, string.operator+, local variables or simply string.Format.

Yes.

@bernd5
Copy link
Contributor Author

bernd5 commented Jan 25, 2021

Is it defined in the spec that expressions with newline trivia are dissallowed here?

A resulting bug is a call to aSyntaxTreeRoot.NormalizeWhitespace() with long interpolation expressions becomes invalid just because
this function produces nice formatted code which is unfortunately not valid / parsable anymore. NormalizeWhitespace inserts new lines here.
I would say NormalizeWhitespace() is not buggy here, it is the interpolated string parsing!

What's about comments, those are "real trivia" - but a multiline comment would break interpolated strings, too.

@CyrusNajmabadi
Copy link
Member

NormalizeWhitespace inserts new lines here.

That is a bug.

@CyrusNajmabadi
Copy link
Member

CyrusNajmabadi commented Jan 25, 2021

Please see the conversation in dotnet/csharplang#1409 for a better understanding of what's going on here.

In particular: dotnet/csharplang#1409 (comment)

@bernd5
Copy link
Contributor Author

bernd5 commented Jan 26, 2021

I understand the problem with the colon handling. But I think the issue with new lines is easy.

Coudn't we change the call at: Lexer-Source

To IsAtEnd(isHole).

New lines are generally allowed in those "holes".

@bernd5
Copy link
Contributor Author

bernd5 commented Jan 26, 2021

New lines in nested-interpolated-verbatim-string-expressions in suche "holes" are already working see:

using System;

Console.WriteLine($"Test { (@$"
12")}");

The code at: Link could be simplified very much.

@bernd5
Copy link
Contributor Author

bernd5 commented Jan 26, 2021

I fixed the code but it is still over-complicated and bug-prone.

One example.
The old implementation allowed:

using System;

Console.WriteLine($"Test { @$"
12"}");

But it rejected:

using System;

Console.WriteLine($"Test { $@"
12"}");

@CyrusNajmabadi
Copy link
Member

New lines in nested-interpolated-verbatim-string-expressions in suche "holes" are already working

yes. this is as expected.

@bernd5
Copy link
Contributor Author

bernd5 commented Nov 13, 2021

Implemented via #56853

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

No branches or pull requests

2 participants