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

Fix type inference of non-rigid number vars #721

Merged
merged 1 commit into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/compiler/typeInference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2475,7 +2475,10 @@ export class InferenceScope {
TMutableRecord(Object.assign({}, type2.fields), type2.baseType),
);
} else {
assign(type1, type2);
const typeClass1 = getTypeclassName(type1);
if (type1.rigid || !typeClass1) {
assign(type1, type2);
}
}
}
}
Expand Down
71 changes: 69 additions & 2 deletions test/diagnosticTests/elmDiagnostics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ import { describe, expect } from "@jest/globals";

const basicsSources = `
--@ Basics.elm
module Basics exposing ((+), (|>), (>>), (==), (>), Int, Float, Bool(..), Order(..), negate)
module Basics exposing ((+), (|>), (>>), (==), (>), (++), Int, Float, Bool(..), Order(..), negate)

infix left 0 (|>) = apR
infix non 4 (==) = eq
infix right 5 (++) = append
infix left 6 (+) = add
infix right 9 (>>) = composeR
infix non 4 (>) = gt
Expand Down Expand Up @@ -60,13 +61,25 @@ type Order = LT | EQ | GT
negate : number -> number
negate n =
-n

append : appendable -> appendable -> appendable
append =
Elm.Kernel.Utils.append
`;

const stringSources = `
--@ String.elm
module String exposing (String)
module String exposing (String, fromInt, fromFloat)

type String = String

fromInt : Int -> String
fromInt =
\\_ -> ""

fromFloat : Float -> String
fromFloat =
\\_ -> ""
`;

describe("test elm diagnostics", () => {
Expand Down Expand Up @@ -1520,4 +1533,58 @@ type DataType
`;
await testTypeInference(basicsSources + source, []);
});

it("should not have an error when converting a number to Float or Int", async () => {
const source = `
--@ Test.elm
module Test exposing (..)

import String

foo : Bool -> String
foo asInt =
let
myGoodNumber =
216

( myBadNumber, _ ) =
( 216, 0 )
in
if asInt then
String.fromInt myGoodNumber
++ String.fromInt myBadNumber

else
String.fromFloat myGoodNumber
++ String.fromFloat myBadNumber
`;
await testTypeInference(basicsSources + stringSources + source, []);
});

it("should have an error when converting a type annotation number to Float or Int", async () => {
const source = `
--@ Test.elm
module Test exposing (..)

import String

foo : Bool -> number -> String
foo asInt myNumber =
let
myGoodNumber =
216
in
if asInt then
String.fromInt myNumber
++ String.fromInt myGoodNumber

else
String.fromFloat myNumber
--^
++ String.fromFloat myGoodNumber
`;
await testTypeInference(basicsSources + stringSources + source, [
{ message: Diagnostics.TypeMismatch, args: ["Float", "Int"] },
]);
});
});