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 with null literals and nullable reference types #55466

Closed
svick opened this issue Aug 6, 2021 · 1 comment
Closed

Type inference with null literals and nullable reference types #55466

svick opened this issue Aug 6, 2021 · 1 comment
Labels
Area-Compilers Feature - Nullable Reference Types Nullable Reference Types Resolution-Duplicate The described behavior is tracked in another issue

Comments

@svick
Copy link
Contributor

svick commented Aug 6, 2021

I'm having trouble with the compiler not inferring the expected type for a generic method when nullable reference types are enabled and one of the arguments is the null literal. Consider the following code:

using System;

#nullable enable

void V<T>(T x1, T x2) { }
void F<T>(Func<T> f1, Func<T> f2) { }

V("", null);                // V<string>, warning
V("", default);             // V<string?>, no warning
F(() => "", () => null);    // F<string>, warning
F(() => "", () => default); // F<string>, warning

In all these examples, my expectation is that the inferred type should be V<string?> and there should be no warning. But only the V("", default) example follows that expectation.

When looking at the NRT spec, I see this:

If the resulting candidate is a reference type or a nonnullable value type and all of the exact bounds or any of the lower bounds are nullable value types, nullable reference types, null or default, then ? is added to the resulting candidate, making it a nullable value type or reference type.

As far as I can tell, in all of these cases the second argument should produce a lower bound of null or default, so the inferred type should be string?.

So, am I correct and is this a bug in the compiler? Or did I misunderstood the spec and this is the expected behavior?

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Aug 6, 2021
@RikkiGibson
Copy link
Contributor

We have a bug where null doesn't contribute its nullability to type inference. I believe it's tracked by #43536.

At any rate, I think for nullability analysis, a null literal should have the same behavior as any expression with constant value null.

@jaredpar jaredpar added Feature - Nullable Reference Types Nullable Reference Types Resolution-Duplicate The described behavior is tracked in another issue and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Aug 9, 2021
@jaredpar jaredpar closed this as completed Aug 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Feature - Nullable Reference Types Nullable Reference Types Resolution-Duplicate The described behavior is tracked in another issue
Projects
None yet
Development

No branches or pull requests

3 participants