Repository navigation
toplevel: print shorter paths for constructors/labels in submodules of open modules - #13536
Conversation
dffc517 to
7b53f74
Compare
|
(I reduced the amount of list gymnastics involved.) |
| in | ||
| loop lid [] (* [[]; [Foo]; [N; Foo]; [M; N; Foo]] *) | ||
| |> List.filter_map Longident.unflatten | ||
| in |
There was a problem hiding this comment.
This implementation feels quite awkward when the environment for constructors and labels contains the list od successive opens. I have the impression that adding a relative_constructor_longident: Path.t -> t -> Longident to Env would be more natural.
There was a problem hiding this comment.
I don't understand the code in env.ml. Maybe TycompTbl is supposed to serve to do this, who knows? I cannot propose an alternative implementation along the lines you suggest in an acceptable amount of effort, so if you think this is a requirement to implement this feature I will have to close the current PR and let someone else work on it.
There was a problem hiding this comment.
I will have a look at implementing my suggestion at the top of this PR to make the discussion clearer.
There was a problem hiding this comment.
@Octachron proposed an implementation of his suggestion in gasche#15 , but my understanding from the PR and a brief discussion this morning is that this alternative approach in fact does not really work in presence of shadowing, and it may be that the approach in the present PR is in fact the simplest.
e15ef36 to
331d783
Compare
| | Lident last -> [suff; (last :: suff)] | ||
| | Ldot(p, s) -> suff :: loop p (s :: suff) | ||
| | Lapply _ -> raise apply_exn | ||
| in |
There was a problem hiding this comment.
I think that the two functions above could be merged since computing the candidates from the Path.t is not that different from computing them from a Longident.t.
There was a problem hiding this comment.
Both functions are in fact relatively tricky. (The second one is your standard tricky recursive function. The first tries to find the "last" component of a path, and this is more difficult than it should be.) I just tried to merge them and failed. Maybe we could leave them as-is, or you want to propose your own merged version?
There was a problem hiding this comment.
Ah, maybe I have a version that works (untested):
(* [candidates exn M.N.t] is [Foo; N.Foo; M.N.Foo].
@raise [exn] on functor application. *)
let candidates apply_exn path =
(* [loop M.N.t [Foo]] is [[Foo]; [N; Foo]; [M; N; Foo]] *)
let rec loop ~is_last path suff = match path with
| Pident id -> [suff; (if is_last then name else id) :: suff]
| Pdot(p, s) -> suff :: loop ~is_last:false p ((if is_last then name else s) :: suff)
| Papply _ -> raise apply_exn
| Pextra_ty (p, _) ->
(* These can only appear directly inside of the associated
constructor so we can just drop the prefix *)
loop path p suff
in
loop ~is_last:true path [] (* [[]; [Foo]; [N; Foo]; [M; N; Foo]] *)
|> List.filter_map Longident.unflatten
inI personally find this unreadable and would have a preference for keeping the two functions separate.
There was a problem hiding this comment.
Ok, I agree that this function is mixing too many concern. Last light suggestion with code:
let lid = match Untypeast.lident_of_path ty_path with
| Lident _ -> Lident name
| Ldot (p,_) -> Ldot(p,name)
| x -> x
inwhile keeping candidates, that you can use if you wish and then I will stop side-nitpicking on the lack of helper functions in the typechecker.
There was a problem hiding this comment.
Nice! I rebased the PR -- and added your "hellish example" below in the testsuite.
| let it = Q.Sub.A | ||
| [%%expect {| | ||
| val it : Q.Sub.t = Q.Sub.A | ||
| |}] |
There was a problem hiding this comment.
A hellish example that we might want to keep in a corner:
module M = struct
module N = struct
module A = struct
module B = struct
type t = X
end
end
module F(X:sig type t end) = struct type t = A of X.t end
end
end
open M open N open A open B
let x = let module FB = F(B) in FB.A X
Octachron
left a comment
There was a problem hiding this comment.
I agree that the implementation works, and it will not be a performance bottleneck ever, so we should merge it.
0bfb92e to
ccb069c
Compare
Note: `tree_of_qualified` now take a `name : string` instead of a `name : out_name` that had to sometimes be transformed back without being used for printing.
ccb069c to
09d6137
Compare
This is my attempt at fixing #12642. Reminder of what the issue is:
Currently, to print
M.N.Foo, the toplevel printer tries to get lucky and see if justFooworks, and otherwise it printsM.N.Foo. With the proposed changes, it will try all ofFoo,N.FooandM.N.Fooin order.(There is a surprising amount of list gymnastics involved, because paths/idents are snoc lists, and computing the list [Foo; N.Foo; M.N.Foo] is harder than I would have guessed.)
At the risk of putting too much weight on the same camel, I'm afraid that @Octachron is the perfect reviewer for this.