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

Question about switch expression with ?? operator #1292

Closed
sungam3r opened this issue Feb 6, 2022 · 11 comments
Closed

Question about switch expression with ?? operator #1292

sungam3r opened this issue Feb 6, 2022 · 11 comments
Labels
bug Something isn't working tracking-external-issue The issue is caused by external problem - nothing we can do to fix it directly

Comments

@sungam3r
Copy link

sungam3r commented Feb 6, 2022

See https://app.codecov.io/gh/graphql-dotnet/parser/blob/master/src/GraphQLParser/ParserContext.Parse.cs
All 4 usages of ExpectOneOf (lines 862, 1101, 1114, 1353) are partilly covered though there are tests that call ExpectOneOf with null and non-null values. Why so?

@daveMueller
Copy link
Collaborator

daveMueller commented Feb 7, 2022

We have to look into it, could be a bug. Thanks for reporting this. 👍

It's related to null-coalescing operator inside method argument.

grafik

@daveMueller daveMueller added the untriaged To be investigated label Feb 7, 2022
@daveMueller
Copy link
Collaborator

I took a quick peek and it really seems to be a bug. I created a simplified repro here: https://github.com/daveMueller/CoverletIssue1292 and I will start working on this.

@daveMueller daveMueller added bug Something isn't working and removed untriaged To be investigated labels Feb 9, 2022
@daveMueller
Copy link
Collaborator

OK seems to be a really special case. When I change the default value to just underscore coverage works.

grafik

@sungam3r
Copy link
Author

What if you try string??

@daveMueller
Copy link
Collaborator

Thanks, but it's the same with string?. This is getting way more complicated than I thought it would be 😄. For switch cases the compiler generates multiple branch point. Depending on how complex the switch expression is the amount of those generated branch points can vary. In the simplest form the issue here looks like this (not related to ?? at all):

grafik

I suspect the second branch is generated as the else branch to the default case. If the switch expression doesn't have a defined default case, the compiler generates an exception.

grafik

Nevertheless I have to look into which of the generated branch point we already exclude and which have to be excluded in addition.

@sungam3r
Copy link
Author

You are right, interesting 🤔 . From here https://stackoverflow.com/questions/56068105/what-happens-if-my-c-sharp-switch-expression-is-non-exhaustive:

The compiler will warn you if your switch expression is not exhaustive, but it's not an error and it'll compile and run anyway.

But I see no warning in my code 😕 . Yes, after reading this I tried to pass null intentionally from OneOf and indeed got SwitchExpressionException at runtime. So my switch expression was not exhaustive because of null branch. And no compiler warnings...

Fully covered now! https://app.codecov.io/gh/graphql-dotnet/parser/compare/238/diff

@sungam3r
Copy link
Author

So actually it may be a question to Roslyn team.

@daveMueller
Copy link
Collaborator

Yes I also started to think that this most likely isn't an issue with coverlet. Because you can fully cover the code when you know that it can throw an exception. E.g. the function

public static bool DoSomething(string? foo)
{
   return foo switch
   {
      "hello" => true
   };
}

Can be fully covered with

[Fact]
public void Test1()
{
   Assert.True(Class1.DoSomething("hello"));
   Assert.Throws<SwitchExpressionException>(() => Class1.DoSomething(""));
}

grafik

When I looked into your code I also thought you handled all the bad cases up front (e.g. with ??) and thats why there is no warning anymore. So I also don't really understand why the compiler still generates the exception. Maybe this really is a Roslyn issue.

Nevertheless I don't think that coverlet has an issue here anymore.

@sungam3r
Copy link
Author

There was no case for null in my switch.

@daveMueller
Copy link
Collaborator

Yes you are right.

@daveMueller
Copy link
Collaborator

Closing this because coverlet works as designed. Feel free to reopen.

@daveMueller daveMueller added the tracking-external-issue The issue is caused by external problem - nothing we can do to fix it directly label Mar 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working tracking-external-issue The issue is caused by external problem - nothing we can do to fix it directly
Projects
None yet
Development

No branches or pull requests

2 participants