Beef icon indicating copy to clipboard operation
Beef copied to clipboard

Unassigned variable is usable in a specific context

Open disarray2077 opened this issue 3 years ago • 2 comments

While trying to fix an IDE crash that I encountered (https://github.com/beefytech/Beef/pull/1429), I found out that the cause of the issue is because Beef currently allows an unassigned variable to be used if that usage comes after an enum union matching.

Example code:

		public static Result<int64> Test64()
		{
			return .Err;
		}

		public static int Main(String[] args)
		{
			int64 i64;

			if (Test64() case .Ok(out i64))
			{
			}

			Console.WriteLine(i64); // no error
			return 0;
		}

disarray2077 avatar Feb 10 '22 19:02 disarray2077

I noticed that this issue was already fixed, but I found two more issues similar to the old one:

Code with comments describing the issues:

	Result<String> str()
	{
		return .Err;
	}

	for (int i < 10)
	{
		String s;

#if true // Issue 1
		// This shouldn't fix the unassigned error
		if (str() case .Ok(out s) || s == null) // s shouldn't be usable after enum matching failed
		{
			Console.WriteLine(s);
			continue;
		}
#endif

#if false // Issue 2
		// This shouldn't cause the unassigned error in Console.WriteLine
		if (true || !(str() case .Ok(out s)))
		{
			continue;
		}
#endif

		Console.WriteLine(s);
	}

disarray2077 avatar Mar 02 '22 21:03 disarray2077