-
Notifications
You must be signed in to change notification settings - Fork 845
Description
Steps to reproduce:
- Launch VS 17.2.6 and create F# console application targeting .NET 6.
- 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
Labels
Type
Projects
Status