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

Error message not properly located (or phrased) with or-pattern #6308

Closed
vicuna opened this issue Jan 27, 2014 · 4 comments
Closed

Error message not properly located (or phrased) with or-pattern #6308

vicuna opened this issue Jan 27, 2014 · 4 comments
Assignees

Comments

@vicuna
Copy link

vicuna commented Jan 27, 2014

Original bug ID: 6308
Reporter: @alainfrisch
Assigned to: @alainfrisch
Status: closed (set by @xavierleroy on 2015-12-11T18:25:52Z)
Resolution: fixed
Priority: normal
Severity: minor
Fixed in version: 4.02.0+dev
Category: typing
Monitored by: @gasche

Bug description

Consider the following error message:

type 'a t = {x: 'a}
function {x=a} | {x=(a,_)} -> ();;
         ^^^^^^^^^^^^^^^^^
Error: This pattern matches values of type 'a * 'b
       but a pattern was expected which matches values of type 'a
       The type variable 'a occurs inside 'a * 'b

The location of the error is the whole or-pattern, which is not coherent with its text.

@vicuna
Copy link
Author

vicuna commented Jan 27, 2014

Comment author: @alainfrisch

It all happens in the enter_orpat_variables function (in typecore.ml). One must decide between using l2 instead of loc for the Pattern_type_clash error (and calling "unify env t2 t1" instead of "unify env t1 t2"), or using a different error which clearly indicates that the problem is related to the or-pattern (and gives the variable name).

@vicuna
Copy link
Author

vicuna commented Jan 27, 2014

Comment author: @alainfrisch

I've committed a fix using a different error message, commit 14421 on trunk. If anyone want to argue in favor of keeping the old error with a different location (or suggest a better wording of the new message), don't hesitate!

# type 'a t = {x: 'a};;
type 'a t = { x : 'a; }
# function {x=Some a} | {x=a} -> ();;
           ^^^^^^^^^^^^^^^^^^
Error: The variable a on the left-hand side of this or-pattern has type 'a
       but on the right-hand side it has type 'a option
       The type variable 'a occurs inside 'a option

@vicuna
Copy link
Author

vicuna commented Jan 27, 2014

Comment author: @lpw25

Does that message still make sense if there are more than two branches to the or-pattern?

For example:

type 'a t = {y : bool; x: 'a};;

function {y = true; _} | {x=Some a; _} | {x=a; _} -> ();;

@vicuna
Copy link
Author

vicuna commented Jan 28, 2014

Comment author: @alainfrisch

function {y = true; _} | {x=Some a; _} | {x=a; _} -> ();;

Error: Variable a must occur on both sides of this | pattern

If you meant:

# function {y = a; _} | {x=Some a; _} | {x=a; _} -> ();; 
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: The variable a on the left-hand side of this or-pattern has type 
       bool but on the right-hand side it has type bool option
# type t = { x : int; y : string; z : bool; };;
# function {x = a; _} | {y = a; _} | {z = a; _} -> ();;
           ^^^^^^^^^^^^^^^^^^^^^^^
Error: The variable a on the left-hand side of this or-pattern has type 
       int but on the right-hand side it has type string

This seems decent to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants