Skip to content

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

@TessenR

Description

@TessenR

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

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

Status

Misc

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions