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

Type inference handles null and default for reference types differently #43536

Closed
TessenR opened this issue Apr 21, 2020 · 3 comments · Fixed by #56022
Closed

Type inference handles null and default for reference types differently #43536

TessenR opened this issue Apr 21, 2020 · 3 comments · Fixed by #56022
Assignees
Labels
Area-Compilers Concept-Design Debt Engineering Debt, Design Debt, or poor product code quality Feature - Nullable Reference Types Nullable Reference Types Urgency-Soon
Milestone

Comments

@TessenR
Copy link

TessenR commented Apr 21, 2020

Version Used:

Branch master (10 Mar 2020)
Latest commit 441c154 by msftbot[bot]:
Merge pull request #42303 from CyrusNajmabadi/useCompoundParens

Support parenthesized expressions in use-compound-operator.

Steps to Reproduce:

Compile the following code

#nullable enable
class C
{
  void M(string s)
  {
    Infer(s, default); // Infer<string?>, no warnings
    Infer(s, null); // Infer<string>, CS8625 cannot convert null literal to non-nullable refernece type
  }

  T Infer<T>(T t1, T t2) => t1 ?? t2;
}

Expected Behavior:
The same type argument is inferred for both Infer calls.

Actual Behavior:
Different nullability is inferred for calls with default and null. defualt infers string? and doesn't have any warnings while the call with null infers string and reports CS8625 cannot convert null literal to non-nullable refernece type

/cc @jcouv

@jcouv
Copy link
Member

jcouv commented Jun 23, 2020

I've not been able to make progress on this due to other work. I'll leave it open if someone could pick it up.

@jcouv jcouv removed their assignment Jun 23, 2020
@jcouv jcouv modified the milestones: 16.7, 16.8 Jun 24, 2020
@jcouv jcouv added the Concept-Design Debt Engineering Debt, Design Debt, or poor product code quality label Aug 3, 2020
@jaredpar jaredpar modified the milestones: 16.8, Compiler.Next Aug 25, 2020
@RikkiGibson
Copy link
Contributor

Interestingly, it seems that use of default produces undesired warnings in some scenarios, while use of null produces undesired warnings in other scenarios.

@TessenR
Copy link
Author

TessenR commented Jan 25, 2021

Interestingly, it seems that use of default produces undesired warnings in some scenarios, while use of null produces undesired warnings in other scenarios.

Note that now default and null values in optional arguments also contribute to inferred nullability.
It created yet another inconsistency with null which seems to be a collateral of this issue - type inference changes based on whether you specified the argument explicitly or not

#nullable enable
class C<T> where T: class
{
    void M(T t1, T t2 = null) { }
    
    void Test1(T t) => M(t); // no warnings
    void Test2(T t) => M(t, null); // CS8625
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Concept-Design Debt Engineering Debt, Design Debt, or poor product code quality Feature - Nullable Reference Types Nullable Reference Types Urgency-Soon
Projects
Archived in project
5 participants