roslyn: Should SyntaxFactory.ParseTypeName reject void?

Version Used: 2.0.0-rc2 from NuGet

Steps to Reproduce:

Execute SyntaxFactory.ParseTypeName("void").

Expected Behavior:

ParseTypeName returns a valid syntax node for the void type.

Actual Behavior:

ParseTypeName returns a syntax node for the void type with the following diagnostic:

error CS1547: Keyword ‘void’ cannot be used in this context


I have discovered this while investigating an SO question. I’m not sure if the behavior I expect is actually correct or not, but the current actual behavior is surprising to me.

I also couldn’t find any tests for this one way or the other. (There is a test for other working keyword-types, which would pass for VoidKeyword, since the test does not check diagnostics.)

I think the fix would be to change the implementation of LanguageParser.ParseTypeName() from return ParseType(); to return ParseTypeOrVoid();.

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Reactions: 2
  • Comments: 16 (15 by maintainers)

Most upvoted comments

@gafter I firmly disagree with you 😃. This is an argument we’ve had many times before. In essence, i think the problem is that you’re effectively making API decisions based on a document that defines the language, but does not define the API. As i’ve argued plenty of times in the past, the grammar is simply what Anders/Mads decided was what they felt was appropriate to balance out how much they’d want to codify in the spec simply, vs what they felt would need more complex text to explain. Mads could turn around tomorrow and decide “actually, we’ll just make ‘void’ grammatically type, and put in some text elsewhere in the spec to state that it’s illegal to use it in contexts X, Y and Z”.

Furthermore, as has been clear from literally the last 20 PRs i’ve sent through on the compiler. Most of the parser errors we produce today are not actually necessary to create at the parsing level. Indeed, it’s not even desirable to create them there as it has negative effects on downstream consumers.**

We have to divorce the idea that “The behavior of the parse methods is defined by the language grammar”. The behavior of the parser is influenced by the grammar. However, it is the compiler that is defined by the entire specification. We are free (and should be encouraged) to make sensible API and implementation decisions that allow our individual layers to deviate when appropriate, as long as we’re abiding by the overall definition.

As we go forward, we will end up in the situation that the compiler reports absolutely no errors except for missing tokens, or skipped tokens. At that point, we’ll end up essentially addressing this bug anyways. Parsing will be extremely relaxed, and will certainly accept a much larger superset of the language without producing errors that the grammar would generally require. My last 20 PRs have been steps along that, and we have many more to go. For example, the grammar would never allow “if (foo) var v = bar();” and our current parser produces an error here. However, post https://github.com/dotnet/roslyn/pull/16134 this will no longer be the case. The parser will allow this without any errors, and it will be up to the rest of hte compiler to report the error.

–

** For example, the grammar states that it is syntactically illegal for someone to write “where T : new(), X, class”, and yet, we now no longer report syntax errors about this. Instead, the errors are deferred to later in the compilation pipeline. That way we abide by the constraint that the compiler produces the errors, without any sort of guarantee about which stage of the compiler will do it.

A method return type is one of them.

Only because the current API has this issue. If we address this issue, then there won’t be a problem parsing 100% of types that can be used in 100% of situations right?

Basically, right now i can’t see how the current approach actually helps anyone. it seems to have just been an accidental implementation detail that just ends up being problematic. We can trivially loosen things to what would be intuitive and expected, without any downsides that i can tell.

There are many nonterminals in the language for which we do not have parse methods.

Sure. But we chose ‘Type’ as a reasonable entry-point to expose. It seems utterly silly that this method would literally handle every possible type in C# except ‘void’. This makes the method useful for almost every place you might want to have a type, instead of being useful everywhere where you’d want a type.

or assemble them from primitive elements,

Yes. Hence makign it so that ParseType can properly parse ‘void’ would be sensible. That way you can construct things appropriately from primitive elements. If we don’t do this, then anyone who wants to parse the return type for a method needs to not only use ‘ParseTypeName’ but also then special case this single type.