Skip to content

Invalid branch code generation of type tests in VS 17.2.6 #13607

@miegir

Description

@miegir

Steps to reproduce:

  1. Launch VS 17.2.6 and create F# console application targeting .NET 6.
  2. Replace contents Program.fs with the following code:
let classify (o: obj) =
    match o with
    | :? System.DateTime as d when d = Unchecked.defaultof<_> -> "default date"
    | :? System.IFormattable -> "formattable"
    | _ -> "not a date"
    
classify System.DateTime.Now
    |> printfn "%s"

Expected behavior: "formattable" should be printed to the console.
Actual behavior: "not a date" is printed to the console.

This is a regression, and the code is correctl executes at sharplab.io for now (see https://sharplab.io/#v2:DYLgZgzgNALiBOBXAdlAJiA1AH2AUxgAIBjYAQwggEswBPQgCgHsRCmAjAKwEpCBeALAAoQqMIBbMjGIALNoQDuVGDOFjC2QiAD8hAMq0IMPOIB0AESl4AKlXF5CFQmkUy8yZ/0IBVZLLzEANZ4aKZoeGBkiMAwTGAAPAD6AHyEALSpAEThkdFEaFaZamKaOvqGxmYAkgBiTPCSMDBk7PjpWWD1jc2teEUiJYSJ7YSZyExEZM6FxaLCpBTUdOVGJhZWtvamAHJMCrMaqQAO8FTIMGAemQCkEP1AA).

This code was also compiling correctly in VS earlier, but that "earlier" is several years ago and I do not know exactly when it was broken.

Incorrectly generated code in Release mode decompiles to the following C# code:

using System;

public static string classify(object o)
{
	if (o is DateTime)
	{
		DateTime d = default(DateTime);
		if (DateTime.Equals((DateTime)o, d))
		{
			d = (DateTime)o;
			return "default date";
		}
	}
	else if (o is IFormattable)
	{
		return "date";
	}
	return "not a date";
}

The compiler incorrectly assumes that DateTime does not implement IFormattable.

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions