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

Disallow nullable suppression in patterns #54072

Merged
merged 5 commits into from
Aug 20, 2021
Merged

Conversation

alrz
Copy link
Member

@alrz alrz commented Jun 14, 2021

Fixes #54071

@alrz alrz requested a review from a team as a code owner June 14, 2021 01:34
@alrz alrz requested a review from jcouv June 14, 2021 01:42
Copy link
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM Thanks (iteration 1)

@alrz
Copy link
Member Author

alrz commented Jun 14, 2021

Looks like suppression operator is allowed but only in some cases, presumably only a coincidence because in those other cases parser couldn't handle it, we didn't have an explicit check. How should we approach this?

        _= o is 1!; // ok (error with this change)
        const int i = 1;
        _= o is i!; // error

@jcouv
Copy link
Member

jcouv commented Jun 14, 2021

Looks like suppression operator is allowed but only in some cases [...]How should we approach this?

Given that a suppression in such place is nonsense, I think we can just disallow and treat the old behavior as a bug. We'd document the (minor) break just in case.

@alrz
Copy link
Member Author

alrz commented Jun 15, 2021

I've seen is null! usages but it's just wrong and misleading. I suppose we need compat council sign off on this.
Where should we document the breaking change, post VS2019? And what version we're targeting for the fix?

@jcouv
Copy link
Member

jcouv commented Jun 15, 2021

Will start email thread with compat council.
This would be documented in a new file for DotNet 6, similar to this one from DotNet 5: https://github.com/dotnet/roslyn/blob/main/docs/compilers/CSharp/Compiler%20Breaking%20Changes%20-%20DotNet%205.md

@jcouv jcouv self-assigned this Jun 15, 2021
@jcouv
Copy link
Member

jcouv commented Jun 17, 2021

Sorry for the delay. Before I send the email out, let's clarify what is the proposed scope of break. Is it just on number literals and null?

@alrz
Copy link
Member Author

alrz commented Jun 17, 2021

All literal expressions can be suppressed right now, wherever they appear in a constant/relational pattern. Only identifiers gave syntax errors previously because we couldn't parse ! after a type-like.

Note this PR doesn't cover the case where a subexpression is suppressed, we may need to descend into the expression more deeply.

@alrz
Copy link
Member Author

alrz commented Jun 19, 2021

Note this PR doesn't cover the case where a subexpression is suppressed, we may need to descend into the expression more deeply.

@jcouv This would probably have even less impact than the top-level case but I'm not sure if we want it.

I think we could flag the pattern binder and report on any suppression instead of walking down the whole thing again here.

@jcouv
Copy link
Member

jcouv commented Jul 14, 2021

case where a subexpression is suppressed

@alrz I'm not sure where we stand on subexpressions. Could we start with a couple of examples to clarify the discussion?

@alrz
Copy link
Member Author

alrz commented Jul 14, 2021

Both of these checks are not covered for subexpressions.

public class C {
    bool b;
    public void M() {
        
        if (this is { b: default }) {} // error
        if (this is { b: true! }) {} // proposed to flag as error
        
        if (this is { b: 0 == default }) {} // no error
        if (this is { b: 1 == 1! }) {} // no error

    }
}

@jcouv
Copy link
Member

jcouv commented Jul 14, 2021

I think it's fine to leave those last two line as "no error". Let's stick with the PR as scoped :-)

@jcouv
Copy link
Member

jcouv commented Jul 14, 2021

@dotnet/roslyn-compiler for a second review. Thanks

1 similar comment
@jcouv
Copy link
Member

jcouv commented Aug 3, 2021

@dotnet/roslyn-compiler for a second review. Thanks

@RikkiGibson RikkiGibson self-assigned this Aug 11, 2021
case SyntaxKind.DefaultLiteralExpression:
diagnostics.Add(ErrorCode.ERR_DefaultPattern, e.Location);
hasErrors = true;
goto default;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like the particular control flow here is a little weird. i.e. this could be return e and the continues could be breaks. However this seems subjective enough that I won't block the PR about it.

@jcouv jcouv enabled auto-merge (squash) August 18, 2021 21:42
@jcouv
Copy link
Member

jcouv commented Aug 18, 2021

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@jcouv
Copy link
Member

jcouv commented Aug 19, 2021

CI integration legs won't pass until #55681 is merged.

@jcouv
Copy link
Member

jcouv commented Aug 20, 2021

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@jcouv jcouv merged commit 8bc4c14 into dotnet:main Aug 20, 2021
@ghost ghost added this to the Next milestone Aug 20, 2021
@dibarbet dibarbet modified the milestones: Next, 17.0.P4 Aug 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Nullable suppressions should remain disallowed on constant and type patterns
4 participants