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

Raw string literal with u8 suffix, either and/or editor and compiler parsing issue #63669

Closed
ericwj opened this issue Aug 30, 2022 · 21 comments
Closed
Labels
Area-Compilers Language-C# Question Resolution-Answered The question has been answered untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@ericwj
Copy link

ericwj commented Aug 30, 2022

Version Used:
Compiler version: '4.4.0-1.22403.13 (ebbf56c)'.
Language version: preview.

Steps to Reproduce:

Re #58848

interpolated strings with suffix disallowed

I bump into this.

  • I had:
    var json = """"
      "": {
          "type": "array",
          "description": "Description.",
          "items": {
              "type": "string"
          },
          "uniqueItems": true,
          "minItems": 1
      }
      """"u8
    
    which was fine
  • I removed the starting "":
    image
  • it started to consider the { and } as interpolation holes
  • so I add a $$
      var json = $$""""
      {
          "type": "array",
          "description": "Description.",
          "items": {
              "type": "string"
          },
          "uniqueItems": true,
          "minItems": 1
      }
      """"u8;
    
  • now I'm broken - u8 is no longer allowed, even though I don't have interpolation holes

Expected Behavior:
I'm not sure whether this parsing is just a bug, or how it is possible to define a raw literal string as u8 if it contains an arbitrary number of { and }.

Actual Behavior:

error CS1002: ; expected
error CS0103: The name 'u8' does not exist in the current context
error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Aug 30, 2022
@CyrusNajmabadi
Copy link
Member

it started to consider the { and } as interpolation holes

What do you mean by this?

@ericwj
Copy link
Author

ericwj commented Aug 30, 2022

Like in the screenshot. It is showing syntax highlighting because it isn't literal anymore.

@CyrusNajmabadi
Copy link
Member

Like in the screenshot. It is showing syntax highlighting because it isn't literal anymore.

?

I'm not sure what you're talking about. Can you clarify?

@CyrusNajmabadi
Copy link
Member

CyrusNajmabadi commented Aug 30, 2022

This is what is see, which looks entirely correct to me:

image

There are no errors here, and the compiler and IDE properly compile this code.

Viewing the decompiled source code shows me:

image

Which is also what i would expect.

@ericwj
Copy link
Author

ericwj commented Aug 30, 2022

Yes, I might have prematurely added those $$ over the syntax highlighting though.
image

@CyrusNajmabadi
Copy link
Member

@ericwj i still don't know what issue you're having :) You cannot mix interpolated strings (those with $ at the start) and utf8 strings.

however, in your examples, you are not using interpolated stings. You're using normal raw-string-literals. And there does not seem to be any issues here. The code is compiling properly and as expected.

@CyrusNajmabadi
Copy link
Member

I'm not sure whether this parsing is just a bug, or how it is possible to define a raw literal string as u8 if it contains an arbitrary number of { and }.

A raw-utf8-string-literal has no restrictions/rules about { and } in it. You can use as many or as few as you want without any concerns whatsoever.

@CyrusNajmabadi CyrusNajmabadi added Language-C# Need More Info The issue needs more information to proceed. and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Aug 30, 2022
@ericwj
Copy link
Author

ericwj commented Aug 30, 2022

Yes, my mistake is that I did add $$ because I got the syntax highlighting, even though it turns out that it compiles.

That answers the question you quoted in the issue. I simply don't add $'s and it'll be fine with u8.

Then what remains is whether the syntax highlighting that confused me is either a compiler issue or merely an issue with VS 17.4.0 Preview 1. It would still be a compiler issue if it told the editor that there was an interpolation hole when there was not. At least it is weird that the editor and compiler don't agree, assuming the syntax parsed by the compiler is what is feeding the syntax highlighting.

@ghost ghost added untriaged Issues and PRs which have not yet been triaged by a lead and removed Need More Info The issue needs more information to proceed. labels Aug 30, 2022
@DoctorKrolic
Copy link
Contributor

I guess, @ericwj misinterpreted json string classification as consider the { and } as interpolation holes

@DoctorKrolic
Copy link
Contributor

IDE recognized your string as a valid json, so it tries to help you by enabling json syntax highlighting inside that string

@ericwj
Copy link
Author

ericwj commented Aug 30, 2022

json string classification

Hmm.

And it doesn't do that for "": { }? Because it is just a fragment?

@DoctorKrolic
Copy link
Contributor

Because "": {} is not a valid json

@CyrusNajmabadi
Copy link
Member

It would still be a compiler issue if it told the editor that there was an interpolation hole when there was not.

There is no interpolation hole.

At least it is weird that the editor and compiler don't agree

They 100% agree here.

assuming the syntax parsed by the compiler is what is feeding the syntax highlighting.

The syntax highlighting is marking up your string content as json, which matches that you were in fact writing json :)

You can control that here:

image

@CyrusNajmabadi
Copy link
Member

And it doesn't do that for "": { }? Because it is just a fragment?

Correct. That's not legal json.

@DoctorKrolic
Copy link
Contributor

I doesn't try to classify parts of the string, but a string as a whole

@ericwj
Copy link
Author

ericwj commented Aug 30, 2022

OK, that's it then. I have never seen this before.

And the colors are pretty much the same as for regular code.

Thanks for your time.

@ericwj ericwj closed this as completed Aug 30, 2022
@CyrusNajmabadi
Copy link
Member

CyrusNajmabadi commented Aug 30, 2022

Note: you can mark an invalid string as explicitly json:

image

If you do, you'll see a warning now that this isn't valid json though.

image

@CyrusNajmabadi CyrusNajmabadi added Question Resolution-Answered The question has been answered labels Aug 30, 2022
@DoctorKrolic
Copy link
Contributor

Btw @CyrusNajmabadi on your screenshot after Viewing the decompiled source code shows me: the json variable is a span, but initialized with array, which leads to unnecessary allocation. Is that really expected here (the string is kinda constant here, so why not stackalloc)?

@DoctorKrolic
Copy link
Contributor

Also close issue as "not planned" please. It better fits here

@CyrusNajmabadi
Copy link
Member

@DoctorKrolic that's simply how ILSpy chooses to show it. It's a ROS over a segment of the data in the dll.

@ericwj ericwj closed this as not planned Won't fix, can't repro, duplicate, stale Aug 30, 2022
@ericwj
Copy link
Author

ericwj commented Aug 30, 2022

There are a series of settings for the colors used in Environment | Fonts and Colors.

image

image

I made it sufficiently different from normal C#.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Language-C# Question Resolution-Answered The question has been answered untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

No branches or pull requests

3 participants