Skip to content

Follow synonyms in #show_module _type#11534

Merged
Octachron merged 5 commits intoocaml:4.14from
gasche:follow-synonyms-in-show-module-type
Dec 2, 2022
Merged

Follow synonyms in #show_module _type#11534
Octachron merged 5 commits intoocaml:4.14from
gasche:follow-synonyms-in-show-module-type

Conversation

@gasche
Copy link
Member

@gasche gasche commented Sep 3, 2022

This PR against 4.14 is intended as a fix for #11533.

At the time of writing, it contains ad-hoc logic in the implementation of #show_module_type to follow a chain of synonyms, mirroring similar logic in the implementation of #show_module to follow a chain of aliases.

Fixes: #11533

@gasche
Copy link
Member Author

gasche commented Sep 3, 2022

(Reviews are already welcome.)

@gasche gasche force-pushed the follow-synonyms-in-show-module-type branch from a60a952 to 9469268 Compare September 3, 2022 20:04
@gasche
Copy link
Member Author

gasche commented Sep 3, 2022

I added some tests.

While playing with #show in presence of module aliases or module type synonyms I encountered some strange behaviors

# module U = Stdlib.Unit;;
module U = Unit
# #show U;;
module U = Unit
module U = Unit
module U :
  sig
    type t = unit = ()
    val equal : t -> t -> bool
    val compare : t -> t -> int
    val to_string : t -> string
  end

Note the repeated line module U = Unit in the #show U output, which is not part of the regression reported in #11533 -- but probably also an interaction with implicit strengthening during environment lookup. After the present PR, the same issue can now show up for module types as well.

This stuttering is independent of the present regression, I am not sure how to fix it, so I propose to leave it outside the scope of the present bugfix PR. (It is also a lesser problem, as it adds some useless noise in the output but does not hide the previously-useful output.)

@gasche
Copy link
Member Author

gasche commented Sep 6, 2022

This small PR is ripe for review. I you look at the code, it is useful to compare it with the code of #show_module above:

ocaml/toplevel/topdirs.ml

Lines 539 to 561 in adcf2cb

let () =
reg_show_prim "show_module"
(fun env loc id lid ->
let path, md = Env.lookup_module ~loc lid env in
let id = match path with
| Pident id -> id
| _ -> id
in
let rec accum_aliases md acc =
let acc rs =
Sig_module (id, Mp_present,
{md with md_type = trim_signature md.md_type},
rs, Exported) :: acc in
match md.md_type with
| Mty_alias path ->
let md = Env.find_module path env in
accum_aliases md (acc Trec_not)
| Mty_ident _ | Mty_signature _ | Mty_functor _ ->
List.rev (acc (is_rec_module id md))
in
accum_aliases md []
)
"Print the signature of the corresponding module."

| Some (Mty_ident path) ->
let mtd = Env.find_modtype path env in
accum_defs mtd acc
| None | Some (Mty_alias _ | Mty_signature _ | Mty_functor _) ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be tempted to extract the Some (Mty_alias _) case, to explicitly state that it can't happen.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure it would really help.
This is just a choice we made of not having module aliases in the syntax of module types, but this code would be correct anyway.

@xavierleroy
Copy link
Contributor

Ping? Who would be good reviewers for this PR?

@garrigue garrigue self-requested a review September 26, 2022 03:23
Copy link
Contributor

@garrigue garrigue left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks basically fine to me.
If fixing stuttering is easy, I would prefer it to be done, otherwise I do not care so much.

| Some (Mty_ident path) ->
let mtd = Env.find_modtype path env in
accum_defs mtd acc
| None | Some (Mty_alias _ | Mty_signature _ | Mty_functor _) ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure it would really help.
This is just a choice we made of not having module aliases in the syntax of module types, but this code would be correct anyway.

| _ -> id
in
let rec accum_defs mtd acc =
let acc = Sig_modtype (id, mtd, Exported) :: acc in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't you avoid stuttering by checking that you are not adding the same definition again?

@Octachron
Copy link
Member

I propose to fix the stuttering at a later date so we can have the fix in 4.14 and 5.0 (this is a toplevel fix, which I am willing to test in the release candidate).

@gasche gasche force-pushed the follow-synonyms-in-show-module-type branch from f371330 to 6453ea3 Compare December 1, 2022 21:46
@gasche gasche force-pushed the follow-synonyms-in-show-module-type branch from 6453ea3 to 699f43c Compare December 1, 2022 21:49
@gasche
Copy link
Member Author

gasche commented Dec 1, 2022

Thanks for the ping! The increase in feature scope had somehow suspended this PR from my memory.

I integrated @garrigue's review comments, and I worked out a fix for the stuttering issue. (Guess what, it was caused by Stdlib__Unit. It's the second time in two weeks that this hidden prefixing adds some pain to my development life.)

@gasche
Copy link
Member Author

gasche commented Dec 1, 2022

This still needs an approval before we can merge.

Copy link
Member

@Octachron Octachron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks good to me (even if I would have fixed the stuttering in the reverse way).

@gasche
Copy link
Member Author

gasche commented Dec 2, 2022

By "the reverse way", do you mean that if the lookup chains finds first D1 and then D2, and in fact D2 double-underscore-simplifies into D1, you would rather keep D2 and throw away D1, rather than keep D1 and throw away D2 as I am doing? (That sounds slightly more difficult to implement for no obvious benefit.)

@Octachron
Copy link
Member

As far as I can see, you are throwing away D1 and keeping D2 in your code since you only add a new item once you have confirmed that it would not be shadowed by a subsequent expansion:

if secretly_the_same_path env path new_path
then acc
else def :: acc

?

But I agree that it would be more difficult with dubious benefits.

@gasche
Copy link
Member Author

gasche commented Dec 2, 2022

We are talking about two successive aliases that would become module M = <path> and module M = <new_path> in the expansion trace. If path and new_path (which is an expansion of path) are the same modulo double-underscore-rewrite, then the def corresponding to module M = <new_path> is omitted from the result, but module M = <path> is already in the acc variable. (Or maybe I'm the one reading things wrong, but whatever, both would be fine anyway.)

@Octachron Octachron merged commit 85a0817 into ocaml:4.14 Dec 2, 2022
Octachron added a commit that referenced this pull request Dec 2, 2022
…type

Follow synonyms in #show_module _type

(cherry picked from commit 85a0817)
Octachron added a commit that referenced this pull request Dec 2, 2022
…type

Follow synonyms in #show_module _type

(cherry picked from commit 85a0817)
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
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.

5 participants