Skip to content

#11194: fix an inconsistency in type variable names in error messages#11609

Merged
Octachron merged 4 commits intoocaml:trunkfrom
Octachron:pr11194_unbound_and_printing_context
Oct 17, 2022
Merged

#11194: fix an inconsistency in type variable names in error messages#11609
Octachron merged 4 commits intoocaml:trunkfrom
Octachron:pr11194_unbound_and_printing_context

Conversation

@Octachron
Copy link
Member

Supersede #11194
Currently, the error message for unbound type variables in classes use inconsistent names for the offending type variable
in the class and method submessage:

class test a c =
object
  method b = c
end
Lines 1-4, characters 0-3:
1 | class test a c =
2 | object
3 |   method b = c
4 | end
Error: Some type variables are unbound in this type:
         class test : 'a -> 'b -> object method b : 'b end
       The method b has type 'a where 'a is unbound

This inconsistency is due to ill-timed reset of the naming context.
The previous attempt to fix this issue in #11194 erased by accident the marking of loops in the printed types, leading to a stack overflow of the type printer in

class ['a] c = object method m: (<x:'a; f:'b> as 'b) -> unit = fun _ -> () end
class d = ['a] c

This PR fixes this issue by adding one more expert function Printtyp.add_type_to_preparation for adding a type expression to the set of marked type exoressions without resetting the naming context. Using this new function fix both issues:

  • the names for type variable are consistent between the class and error submessages
  • all loops in the type of the method are marked by the time we print the method type.

@gasche
Copy link
Member

gasche commented Oct 6, 2022

This is not a problem with your patch proper, but: it is hard for newcomers to figure out what's going on here, because you carved out a nice API to talk about "preparation" (before prepare_for_printing and then prepared_type_expr, and now your masterfully named add_type_to_preparation), but the code of typeclass.ml is not using it consistently.
It prepares but never uses prepared_type_expr. Most of the printing calls weirdly use the lower-level function Printtype.tree_of_typexp, which of course does not specify whether it resets the printing context or not (and good luck figuring out from the implementation), they should be refactored to use prepared_type_expr instead. Then the Unexpected_field case calls Printtyp.typ_expr, which I think is a small bug (the prepare_printing call above is probably a no-op?

While we are at it, on the preparation API:

  • I think it would be more natural (and would require less masterful naming) if prepare_* would always add a type to a current preparation. That would require having an explicit reset first (or, if we prefer, start_preparation), and we should ensure that the following printing always uses a prepared_* function.
  • We could detect this kind of bugs if we temporarily (to run the testsuite for example) made it an error to reset the context before printing prepared expression. This could be done by incrementing a counter on prepare_* calls, resetting it to 0 on prepared_* printing calls, and emitting an error or warning if reset () is called while the counter is not 0.

@Octachron
Copy link
Member Author

Good catch on the redundant call to prepare_type in the Unexpected_field case.

The call to the low-level tree construction functions are currently necessary due to the fact that the printing type needs to go through two stages of preparation before any type is printed:

  • First, loops are marked and type variables are named
  • Second, types constructor are shortened and colliding identifiers names are annotated with the /$n suffix.

This second stage is done when constructing the outcome tree and relies on an internal mutable state. Consequently, all trees needs to be constructed before any of them are printed. One of the many advantages of #11515 is that it removes this internal state which means that it would be possible to remove most calls to the tree_of_* functions when printing errors. A temporary alternative would be to annotate paths within Printtyp.prepare_type, which is sufficient for removing at least the calls to Printtyp.tree_of_typexp.

In term of API, I think that my current opinion is that it would be better to materialize the existing mutable stables in the Printtyp API.

@gasche
Copy link
Member

gasche commented Oct 7, 2022

Hm, so i guess the short-term conclusion is that we should not try to use prepared_* everywhere for now but wait for #11515 to be reviewed and merged first. Makes sense to me.

voodoos and others added 4 commits October 13, 2022 15:49
The type var should stay coherent in a same error message. This is not the case for "variables unbound" messages.
and use this new function in class error messages.
@Octachron Octachron force-pushed the pr11194_unbound_and_printing_context branch from d4a68b9 to f193278 Compare October 13, 2022 13:50
@Octachron
Copy link
Member Author

Octachron commented Oct 14, 2022

I am planning to cherry-pick the change to 4.14 in order to have the fix available for merlin too (currently merlin loops with the new example in the testsuite).

@Octachron Octachron merged commit 1b93239 into ocaml:trunk Oct 17, 2022
Octachron added a commit that referenced this pull request Oct 17, 2022
Octachron added a commit that referenced this pull request Oct 17, 2022
stedolan pushed a commit to stedolan/ocaml that referenced this pull request Mar 21, 2023
a09392d Set Menhir version back to 20210419 again (ocaml#89)
cc63992 Merge pull request ocaml#88 from mshinwell/flambda-backend-changes-2022-12-27
3e49df3 HACKING.jst.adoc
1866676 Merge flambda-backend changes
e012992 Merge pull request ocaml#87 from mshinwell/merge-4.14.1
ac5c7c8 Merge tag '4.14.1' into main
3da21bc add a useful debug printer
83b7c72 Document the debug_printers script
98896e0 Remove a tiny code stutter I came across
99cb5d9 release 4.14.1
b49060f last commit before tagging 4.14.1
fae9aef Add documentation
708e5a9 Add tests
c609eee Bootstrap
7f922d0 Polymorphic parameters
51aeb04 Keep generalized structure from patterns when typing let
4b68bb3 Add test of princiaplity from polymorphic type constraints
82c7afe fix wong raise
aca252f x86: Force result of Icomp to be in a register (ocaml#11808)
985725b Add dynlink_compilerlibs.mli to .gitignore (ocaml#79)
2b1fa24 Regenerate parser (ocaml#80)
1bb6c79 Merge pull request ocaml#78 from mshinwell/flambda-backend-patches-2022-12-13
9029581 Update otherlibs/dynlink/Makefile
3e4f1b9 Revert toplevel/native/dune to ocaml-jst version
6061e4c Regenerate configure using autoconf 2.71
888d4b1 Back out patch which disables alloc-check in ocaml-jst
a6d5796 Fix dynlink build
3e46daf Update .depend files
a5c547e Bootstrap
a6a9031 Merge flambda-backend changes
0ac7fdd temp fix for linker error (ocaml#77)
1018602 Remove references to 32-bit Cygwin (ocaml#11797)
e2d0d9e Enable individual testing with Makefile.jst (ocaml#76)
f10cbf6 increment version number after tagging 4.14.1~rc1
11c5ab7 release 4.14.1~rc1
e4c3920 last commit before tagging 4.14.1~rc1
9e598ca Merge pull request ocaml#11793 from dra27/then-than
2a7e501 Use a more relaxed mode for unification in Ctype.subst (ocaml#11771) (ocaml#73)
7b35ef7 Statically initialize `caml_global_data` with a valid value (ocaml#11788)
cbd791a Allow immediates to cross modes (ocaml#58)
85a0817 Merge pull request ocaml#11534 from gasche/follow-synonyms-in-show-module-type
699f43c Changes
e54e9bc fix the 'stuttering' issue in #show
d9799d3 test comments
fec3b23 follow synonyms when #show-ing module types
06a1ad7 regression tests for ocaml#11533 (still failing)
549d757 Run "misplaced attributes" check when compiling mlis (ocaml#72)
b2b74bf Fix bug in `Mtype.strengthen_lazy` causing spurious typing errors (ocaml#11776)
a6c0e75 Ensure that Ctype.nongen always calls remove_mode_variables (ocaml#70)
6c50831 array elements are global (ocaml#67)
bc510ed Ensure that types from packed modules are always generalised (ocaml#11732)
4d47036 Fix ocaml#10768
8788ff6 Add/move some documentation
9891a36 Propagate location information to `local_` in expressions
988306d Add support for `global_` and `nonlocal_` constructor arguments (ocaml#50)
6729eb8 Missing CAMLparam in win32's Unix.stat (ocaml#11737)
e7dd740 Add debug_printers.ml (ocaml#63)
65f2896 more entries in gitignore (ocaml#62)
a9a84d0 Move `global_flag` to `Asttypes` (ocaml#60)
fac5896 Minor attribute fixes from flambda-backend
75f402e Note about make install and Makefile.jst (ocaml#56)
fb5b1e4 Remove the -force-tmc flag (ocaml#11661)
bd87a61 ocamlmklib: use `ar rcs` instead of `ar rc` (ocaml#11670)
83762af Merge pull request ocaml#11622 from Octachron/fix_recursive_types_in_constructor_mismatch
ca48730 Merge pull request ocaml#11609 from Octachron/pr11194_unbound_and_printing_context

git-subtree-dir: ocaml
git-subtree-split: a09392d
ejgallego pushed a commit to ejgallego/ocaml that referenced this pull request Nov 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants