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

Range variables always considered not null, leading to absent CS8604 warnings #41193

Open
Foxite opened this issue Jan 24, 2020 · 1 comment
Open

Comments

@Foxite
Copy link

Foxite commented Jan 24, 2020

Version Used: 3.4.0-beta4-19569-03 (82f2e25)

Steps to Reproduce:

class Program {
	static void Main(string[] args) {
		// The example code is a bit contrived but the problem appears in real code.
		IEnumerable<int> results =
			from arg in args
			let parsedArg = ParseString(arg) // ParseString returns `string?`. However, parsedArg is considered a `string`, not a `string?`, which you can see when inspecting the variable in VS.
			//where !(parsedArg is null) // Removal of this line should cause a CS8604 error below, but it doesn't.
			select int.Parse(parsedArg);

		string? thing = null;
		ParseString(thing); // As a sanity check, this does cause a CS8604.
	}

	private static string? ParseString(string input) {
		// Imagine this function sometimes returns null and sometimes it doesn't.
		return null;
	}
}

Expected Behavior: the parsedArg range variable is considered a string?, and removal of the where clause causes a CS8604 (Possible null reference argument for parameter) warning in the line below it.

Actual Behavior: The variable is considered a string and no warnings are emitted.

@RikkiGibson
Copy link
Contributor

I think the part where parsedArg has not-null flow state is a bug, and the part where the where clause is expected to change the flow state is a feature request in the vein of dotnet/csharplang#8383 and #39586

@jcouv jcouv added this to the Compiler.Next milestone Feb 9, 2021
@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

4 participants