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

Typing error message is worse that before #6173

Open
vicuna opened this issue Sep 12, 2013 · 9 comments

Comments

Projects
None yet
2 participants
@vicuna
Copy link

commented Sep 12, 2013

Original bug ID: 6173
Reporter: @johnwhitington
Assigned to: @garrigue
Status: assigned (set by @johnwhitington on 2015-11-23T21:49:16Z)
Resolution: reopened
Priority: normal
Severity: feature
Version: 4.01.0
Target version: later
Fixed in version: 4.02.0+dev
Category: typing
Has duplicate: #6295
Related to: #5936
Child of: #7338
Monitored by: @gasche @hcarty @dbuenzli

Bug description

OCaml version 4.01.0+rc1
# let f x = x @ [];;
val f : 'a list -> 'a list = <fun>
# f ();;
Error: The variant type list has no constructor ()

Is this related to the new variants work? Is it related to the fact that () is, unusually, a valid constructor (this has been debated before, I know)?

The previous behaviour is much less confusing.

 OCaml version 4.00.1

# let f x = x @ [];;
val f : 'a list -> 'a list = <fun>
# f ();;
Error: This expression has type unit but an expression was expected of type
         'a list
@vicuna

This comment has been minimized.

Copy link
Author

commented Sep 19, 2013

Comment author: furuse

I am also confused by this error message.

Is it possible to print which type the constructor (here ()) belongs to? Does the new variant name disambiguation thing prevent it?

@vicuna

This comment has been minimized.

Copy link
Author

commented Sep 19, 2013

Comment author: @garrigue

Indeed, the new variant name disambiguation allows using a constructor based on the type, even if this constructor is not the latest one defined, or not even in scope.

We could easily list up the types in scope which contain the said constructor.
But would this really help ?
Knowing that () is of type unit doesn't seem really relevant here.
Maybe it is just that the wording is not so good.
Something like:

Error: An expression of type 'a list was expected.
The constructor () does not belong to the variant type list.

might be clearer.

Note also that if it was just a typo, the error message is often better than before:

(Same 3 : int option);;

Error: The variant type option has no constructor Same
Did you mean Some?

@vicuna

This comment has been minimized.

Copy link
Author

commented Sep 19, 2013

Comment author: furuse

I prefer the message you proposed.

@vicuna

This comment has been minimized.

Copy link
Author

commented Sep 19, 2013

Comment author: @johnwhitington

It is suggested by doligez in bug #5936 that () as a constructor may be removed soon. This would fix this particular problem.

I didn't mean to suggest that the new message is in general a problem, I just think it's awfully confusing to the user when something as fundamental as unit is involved.

@vicuna

This comment has been minimized.

Copy link
Author

commented Apr 4, 2014

Comment author: @garrigue

As explained in #6173, the error message in trunk is now:

Error: This variant expression is expected to have type 'a list
The constructor () does not belong to type list

This seams clear enough.

@vicuna

This comment has been minimized.

Copy link
Author

commented Jan 4, 2015

Comment author: @johnwhitington

Even more confusing example, where the list constructor :: doesn't even appear explicitly in the source:

        OCaml version 4.03.0+dev5-2014-10-15

# let f () = ();;
val f : unit -> unit = <fun>
# f [1; 2; 3];;
Error: This variant expression is expected to have type unit
       The constructor :: does not belong to type unit

This used to be:

# let f () = ();;
val f : unit -> unit = <fun>
# f [1; 2; 3];;
Error: This expression has type 'a list
       but an expression was expected of type unit

which was better, because it mentioned the type 'list'.

@vicuna

This comment has been minimized.

Copy link
Author

commented Nov 23, 2015

Comment author: @xavierleroy

Any hope of addressing johnwhitington's latest example? I'm afraid not given the new type inference strategy introduced to handle constructor disambiguation.

@vicuna

This comment has been minimized.

Copy link
Author

commented Nov 23, 2015

Comment author: @Octachron

I have a naive question. Since the confusing apparition of (::) in the error message stems from special casing [ a ; ..] in the syntax, would it not make sense to also special case (::) in the error message? For instance, by adding a short note:

The constructor :: does not belong to type unit. Note: this constructor is used implicitely by the list syntax [ a ; .. ].

@vicuna

This comment has been minimized.

Copy link
Author

commented Nov 23, 2015

Comment author: @johnwhitington

A long-term solution to these problems might be to decorate the parse tree to indicate where syntactic sugar has been expanded during parsing.

If those decorations find their way unharmed through to the typed tree, the we could know the difference between a programmer writing "1::2::3::[]" and "[1; 2; 3]" in the source. Ditto for "fun x y -> x + y" vs "fun x -> fun y -> x + y". This difference would only be relevant when reporting an error, of course, not during typechecking itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.