Fsharp: Fields from inconsistent types when adding empty code file

Created on 29 Jun 2016  路  3Comments  路  Source: dotnet/fsharp

Please accept my apologies if this is a known issue, but a casual Google search didn't turn up anything, and it's one of those things that are a bit difficult to search for.

Repro steps

Create a new F# library project in Visual Studio 2015.

Add a new code file. As far as I can tell, the name of the file doesn't matter, but for the repro, I called it Dtrs.fs:

namespace Ploeh.Weird.Repro

type Foo = {
    Value : string
    Text : string }

type Bar = {
    Value : string
    Number : int }

type Baz = {
    Value : int
    Text : string }

Notice the namespace: it's important.

Now add _another_ F# library project to the solution.

Reference the first library project from the second project.

Add a new code file to the second library project. Again, I don't think the name of the file matters, but I called mine Client.fs:

module Ploeh.Repro.Client

open Ploeh.Weird.Repro

let b = {
    Value = 42
    Text = "Ploeh" }

Compile. This code compiles, and the compiler has inferred that b is a value of the type Baz. This is expected, since the expression is unambiguous.

Now go back to the first library project, and add a new code file _below_ the one that contains the three record types. The name of the file doesn't, AFAICT, matter, but you can call it Boo.fs if you'd like (I did). Put this in the file:

namespace Ploeh.Weird.Repro

That's it. Nothing else. This is the same namespace as the file declaring the three record types.

Now compile the solution.

Expected behavior

The code still compiles. I'd expect that, since the new file adds no new types (or values).

Actual behavior

The code no longer compiles. The compiler gives this error message:

error FS0656: This record contains fields from inconsistent types

This error occurs in the Client.fs file, in the expression that binds to b.

Known workarounds

One can work around the issue in a couple of different ways:

  • Delete the Boo.fs file.
  • Change the namespace in Boo.fs. For example, it's enough to remove a single character from the namespace declaration to make the code compile again: namespace Ploeh.Weird.Repr.
  • Use a type annotation in Client.fs: let b : Baz = \....

Related information

  • Operating system
    Windows 10 x64
  • .NET Runtime, CoreCLR or Mono Version
    .NET 4.6.1
  • Editing Tools (e.g. Visual Studio Version)
    Visual Studio 2015 Update 2
  • Indications of severity
    Mainly _strange_...
Area-Compiler Ready Severity-Medium bug

Most helpful comment

@ploeh Thanks. Yes, I can repro this and it should be fixed. Presumably the "merging" of the two namespace declaration fragments is upsetting the order in which record types are considered when inferring them from field names.

All 3 comments

@ploeh Thanks. Yes, I can repro this and it should be fixed. Presumably the "merging" of the two namespace declaration fragments is upsetting the order in which record types are considered when inferring them from field names.

@ploeh I've pushed a PR for a fix for this, link above

Fixed by merged PR above

Was this page helpful?
0 / 5 - 0 ratings