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 confusing for the beginner when a type escapes its scope. #6171

Closed
vicuna opened this issue Sep 11, 2013 · 3 comments

Comments

Projects
None yet
2 participants
@vicuna
Copy link

commented Sep 11, 2013

Original bug ID: 6171
Reporter: @johnwhitington
Assigned to: @garrigue
Status: closed (set by @xavierleroy on 2016-12-07T10:47:07Z)
Resolution: fixed
Priority: normal
Severity: minor
Version: 4.01.0+beta/+rc
Target version: 4.02.2+dev / +rc1
Fixed in version: 4.03.0+dev / +beta1
Category: typing
Has duplicate: #6734
Monitored by: @bobzhang @gasche @hcarty

Bug description

I'd only had to deal with this error when using the module system until now -- I hadn't realised how easily it could be encountered with a single reference.

The first line of the message in particular is rather confusing:

let x = ref []

type t = A

let store () =
x := A::!x

feast:~ john$ ocamlc mess.ml
File "mess.ml", line 6, characters 7-8:
Error: This expression has type t but an expression was expected of type t
The type constructor t would escape its scope

Perhaps skip the first line of the error message since it's confusing and add a clarification -- something like "A type can only be used after it has been introduced."?

@vicuna

This comment has been minimized.

Copy link
Author

commented Sep 11, 2013

Comment author: @garrigue

Reporting of unification errors is fundamentally broken: it is hard to see where the error comes from, and specific cases are poorly handled.
I hope to be able to fix it, but this would need some deep changes.
Quick fixes do not help much.
For instance here, if we remove the first line, we lose the type of the expression, which can be informative.

@vicuna

This comment has been minimized.

Copy link
Author

commented Sep 16, 2013

Comment author: @lpw25

It wouldn't help much in this case, but could the error at least mention the type that it tried to unify t with, rather than t itself.

So, for example:

# let f b (x: 'x) = 
    let module M = struct
      type t = A
    end in
      if b then x else M.A;;
        Characters 96-99:
        if b then x else M.A;;
                         ^^^
Error: This expression has type M.t but an expression was expected of type
         'x
       The type constructor M.t would escape its scope

This information is often quite useful, and I don't think it would be hard to expose.

@vicuna

This comment has been minimized.

Copy link
Author

commented Jan 17, 2015

Comment author: @garrigue

Fixed as suggest by Leo in trunk at revision 15782.

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.