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

Roslyn fails to verify [NotNullWhen] contracts in methods returning Nullable<bool> #57649

Open
TessenR opened this issue Nov 9, 2021 · 6 comments

Comments

@TessenR
Copy link

TessenR commented Nov 9, 2021

Version Used:

Branch main (5 Nov 2021)
Latest commit 44f0576 by Joey Robichaud:
Revert to VS2019 for official build

Steps to Reproduce:

Compile and run the following code

#nullable enable
using System.Diagnostics.CodeAnalysis;

class C
{
  public static void Main()
  {
    if (M1(out var x) ?? false)
    {
      x.ToString();
    }
  }

  static bool? M1([NotNullWhen(true)] out string? x)
  {
    x = null;
    return true;
  }
}

Expected Behavior:
CS8762: Parameter 'x' must have a non-null value when exiting with 'true'. reported for return true; in M1

Actual Behavior:
No warnings at all in the program above. It crashes at runtime with a NullReferenceException

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Nov 9, 2021
@jaredpar jaredpar added Bug New Language Feature - Nullable Reference Types Nullable Reference Types and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Nov 9, 2021
@jaredpar jaredpar added this to the 17.1 milestone Nov 9, 2021
@RikkiGibson
Copy link
Contributor

@jcouv did we inadvertently do the "call-site" side of this without doing the "declaration" side of it?

@jcouv
Copy link
Member

jcouv commented Nov 10, 2021

Conditional nullable attributes are only expected to work on bool, not Nullable<bool>.
https://github.com/dotnet/csharplang/blob/main/meetings/2019/LDM-2019-05-15.md#conditional-postconditions

The bug here is actually on the call-site. We are not able to analyze the call-site because we only track one or two states (unconditional or conditional respectively), but Nullable<bool> involves more than two states...
The bug is likely in learnFromPostConditions which should check for bool type before splitting the state.

@TessenR
Copy link
Author

TessenR commented Nov 11, 2021

@jcouv Does it mean that the following code should have a warning because the expression dictionary?.TryGetValue("", out var x) is of type Nullable<bool>?

#nullable enable
using System.Collections.Generic;

class C
{
  public void M(Dictionary<string, string>? dictionary)
  {
    if (dictionary?.TryGetValue("", out var x) ?? false)
    {
      x.ToString(); // currently no warning
    }
  }
}

@jcouv
Copy link
Member

jcouv commented Nov 11, 2021

@TessenR No, that scenario will continue to work. TryGetValue returns a bool. We have a special treatment for this whole expression pattern (ie. x?.y ?? false) as part of improved definite assignment.

@sharwell
Copy link
Member

It seems like one solution here is to report a diagnostic for use of NotNullWhen/MaybeNullWhen on non-bool-returning methods.

@RikkiGibson
Copy link
Contributor

RikkiGibson commented Nov 15, 2021

#36073 is an umbrella issue for reporting warnings on misuse of attributes based on the signature. We do already have some warnings on misuse in terms of what the implementation is doing, as @jaredpar has pointed out in email. For example, when the method has DoNotReturn, we warn if the implementation returns.

@jinujoseph jinujoseph modified the milestones: 17.1, 17.2 Mar 2, 2022
@jcouv jcouv modified the milestones: 17.2, 17.3 May 14, 2022
@jaredpar jaredpar modified the milestones: 17.3, Backlog, Compiler.Next Jun 27, 2022
@jaredpar jaredpar modified the milestones: Compiler.Next, Backlog Sep 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants