Skip to content

[tooling] Distinct uids for interfaces#13286

Merged
Octachron merged 6 commits into
ocaml:trunkfrom
voodoos:distinct-uids-for-interfaces
Aug 2, 2024
Merged

[tooling] Distinct uids for interfaces#13286
Octachron merged 6 commits into
ocaml:trunkfrom
voodoos:distinct-uids-for-interfaces

Conversation

@voodoos

@voodoos voodoos commented Jul 10, 2024

Copy link
Copy Markdown
Contributor

Uids are supposed to be unique inside a compilation unit. But if we consider that a compilation unit is made of an interface file and an implementation file, then this is not actually true: declarations are numbered with uids starting from 0 in both the interface and the implementation. This can be a source of confusion for external tools such as Merlin or the occurrences indexer.

This PR proposes annotating uids with their origin (Impl of Intf). To make the change less invasive I used a reference that defaults to Impl and is switched to Intf when Typemod.type_interface is called.

Additionally, to make the testsuite more stable, commit 238d54f sorts the declarations in ocamlobjinfo's printing... This is a noisy commit that should make future changes less noisy. Reviewing commit-by-commit should filter the noise.

This change is important for a PR that I plan to open soon that generalizes the cmt_format.cmt_value_dependencies field so that external tools can keep track of dependencies between declarations. This will allow Merlin to jump between ml and mli files, have the project-wide occurrences feature also return declarations, etc.

Pinging the people who were involved in the recent indexing work: @Octachron @gasche
CC @lpw25

@voodoos voodoos force-pushed the distinct-uids-for-interfaces branch 2 times, most recently from 5737de9 to 9f71a63 Compare July 10, 2024 15:34
voodoos added a commit to voodoos/ocaml that referenced this pull request Jul 16, 2024
voodoos added a commit to voodoos/ocaml that referenced this pull request Jul 16, 2024
@voodoos voodoos changed the title Distinct uids for interfaces [tooling] Distinct uids for interfaces Jul 16, 2024
voodoos added a commit to voodoos/ocaml that referenced this pull request Jul 16, 2024
voodoos added a commit to voodoos/ocaml that referenced this pull request Jul 16, 2024
voodoos added a commit to voodoos/ocaml that referenced this pull request Jul 16, 2024
voodoos added a commit to voodoos/ocaml that referenced this pull request Jul 16, 2024
voodoos added a commit to voodoos/ocaml that referenced this pull request Jul 16, 2024
Comment thread typing/shape.mli Outdated

(** [set_from] allow changing the [from] information stored with uids
indicating if they emanate from an interface or an implementation file. *)
val set_from : intf_or_impl -> unit

@lpw25 lpw25 Jul 19, 2024

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think it would be better to store this information in the same place we store the current unit name (i.e. in Env). Possibly it would be worth creating a current_unit type containing the name and the intf_or_impl and then have mk take one of those and then have Env.get_current_unit return one of those and replace the vast majority of Env.get_unit_names with Env.get_current_unit. (Also maybe rename Env.get_unit_name to Env.get_current_unit_name for clarity).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It seems like a reasonable thing to do, but I am going to face circular dependencies issues if the current_unit type is defined in Env and refered to in Shape.Uid. I dug a bit more and there might be an even more appropriate place for that type definition: the Unit_info module introduced by @Octachron. Currently, the unit_name in env is set from the corresponding field of Unit_info.t:

let with_info ~native ~tool_name ~source_file ~output_prefix ~dump_ext k =
  Compmisc.init_path ();
  let target = Unit_info.make ~source_file output_prefix in
  Env.set_unit_name (Unit_info.modname target);
  let env = Compmisc.initial_env() in
  let dump_file = String.concat "." [output_prefix; dump_ext] in
  Compmisc.with_ppf_dump ~file_prefix:dump_file @@ fun ppf_dump ->
  k {
    target;
    env;
    ppf_dump;
    tool_name;
    native;
  }

Maybe we could simply add the impl_or_intf field to the Unit_info.t and store all this in the Env reference: Env.set_unit_info target ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sounds good to me

@voodoos voodoos Jul 24, 2024

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I made changes in that direction in e05aae4

@gasche

gasche commented Jul 24, 2024

Copy link
Copy Markdown
Member

Ping @lpw25 : are you planning to do a second review iteration on this PR?

@voodoos voodoos force-pushed the distinct-uids-for-interfaces branch from 0bb4209 to e05aae4 Compare July 24, 2024 14:29
voodoos added a commit to voodoos/ocaml that referenced this pull request Jul 24, 2024
@lpw25

lpw25 commented Jul 25, 2024

Copy link
Copy Markdown
Contributor

I didn't really do a first pass, just looked at the part from the PR description that sounded like it could be improved. I'd be happy to review this PR but can't really commit to doing so promptly.

Comment thread driver/compile_common.ml Outdated

let with_info ~native ~tool_name ~source_file ~output_prefix ~dump_ext k =
let with_info
~native ~tool_name ~source_file ~source_kind ~output_prefix ~dump_ext k =

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I have a small interrogation, with the new source_kind this is the third argument that we pass to with_info which is only used to construct an Unit_info.t. Do we want to group together those arguments in a single unit_info argument.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That seems reasonable, I will give it a go, we can always revert later if we're not satisfied.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in 2f2b546

@gasche gasche added the tools label Aug 1, 2024
@voodoos voodoos force-pushed the distinct-uids-for-interfaces branch from 804be40 to 6c63c83 Compare August 1, 2024 12:28
voodoos added a commit to voodoos/ocaml that referenced this pull request Aug 1, 2024
voodoos added a commit to voodoos/ocaml that referenced this pull request Aug 1, 2024
@voodoos voodoos force-pushed the distinct-uids-for-interfaces branch from 6c63c83 to 95feea4 Compare August 1, 2024 12:40

@Octachron Octachron left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I agree that keeping the kind of the current unit is useful information in general, even more so if it helps keeping uid unique.
The PR is ready to merge from my point of view.

Comment thread driver/compile_common.mli Outdated
voodoos added a commit to voodoos/ocaml that referenced this pull request Aug 1, 2024
Since some uids are equal between interfaces and implementations, they are missing from the results until PR ocaml#13286 is merged.
voodoos added a commit to voodoos/ocaml that referenced this pull request Aug 1, 2024
voodoos added a commit to voodoos/ocaml that referenced this pull request Aug 1, 2024
voodoos added a commit to voodoos/ocaml that referenced this pull request Aug 1, 2024
Since some uids are equal between interfaces and implementations, they are missing from the results until PR ocaml#13286 is merged.
voodoos added 4 commits August 2, 2024 15:21
This actually make Uids unique accros a compilation unit made of an interface and an implementation.
@voodoos voodoos force-pushed the distinct-uids-for-interfaces branch 2 times, most recently from 5efdc1e to eefe479 Compare August 2, 2024 13:37
@Octachron Octachron merged commit f215b2a into ocaml:trunk Aug 2, 2024
voodoos added a commit to voodoos/flambda-backend that referenced this pull request Apr 8, 2025
[tooling] Distinct uids for interfaces

There should be a cleaner way to store the additional information. Upstream this info is
   part of a `Unit_info.t` record that is stored as the "current unit" in the
   environement. But here in flambda-backend it is `Compilation_unit.t` instead. This
   looks like conflicting refactorings that it would be useful to reconcile.

   Here, for the prototype, we use a tuple to add that information.
voodoos added a commit to voodoos/flambda-backend that referenced this pull request Apr 8, 2025
[tooling] Distinct uids for interfaces

There should be a cleaner way to store the additional information. Upstream this info is
   part of a `Unit_info.t` record that is stored as the "current unit" in the
   environement. But here in flambda-backend it is `Compilation_unit.t` instead. This
   looks like conflicting refactorings that it would be useful to reconcile.

   Here, for the prototype, we use a tuple to add that information.

sq
voodoos added a commit to voodoos/flambda-backend that referenced this pull request Apr 8, 2025
[tooling] Distinct uids for interfaces

There should be a cleaner way to store the additional information. Upstream this info is
   part of a `Unit_info.t` record that is stored as the "current unit" in the
   environement. But here in flambda-backend it is `Compilation_unit.t` instead. This
   looks like conflicting refactorings that it would be useful to reconcile.

   Here, for the prototype, we use a tuple to add that information.

sq
voodoos added a commit to voodoos/flambda-backend that referenced this pull request Apr 8, 2025
[tooling] Distinct uids for interfaces

There should be a cleaner way to store the additional information. Upstream this info is
   part of a `Unit_info.t` record that is stored as the "current unit" in the
   environement. But here in flambda-backend it is `Compilation_unit.t` instead. This
   looks like conflicting refactorings that it would be useful to reconcile.

   Here, for the prototype, we use a tuple to add that information.

sq
voodoos added a commit to voodoos/flambda-backend that referenced this pull request Apr 9, 2025
[tooling] Distinct uids for interfaces

There should be a cleaner way to store the additional information. Upstream this info is
   part of a `Unit_info.t` record that is stored as the "current unit" in the
   environement. But here in flambda-backend it is `Compilation_unit.t` instead. This
   looks like conflicting refactorings that it would be useful to reconcile.

   Here, for the prototype, we use a tuple to add that information.
voodoos added a commit to voodoos/flambda-backend that referenced this pull request Apr 10, 2025
[tooling] Distinct uids for interfaces

There should be a cleaner way to store the additional information. Upstream this info is
   part of a `Unit_info.t` record that is stored as the "current unit" in the
   environement. But here in flambda-backend it is `Compilation_unit.t` instead. This
   looks like conflicting refactorings that it would be useful to reconcile.

   Here, for the prototype, we use a tuple to add that information.
voodoos added a commit to voodoos/flambda-backend that referenced this pull request Apr 10, 2025
[tooling] Distinct uids for interfaces

There should be a cleaner way to store the additional information. Upstream this info is
   part of a `Unit_info.t` record that is stored as the "current unit" in the
   environement. But here in flambda-backend it is `Compilation_unit.t` instead. This
   looks like conflicting refactorings that it would be useful to reconcile.

   Here, for the prototype, we use a tuple to add that information.
NickBarnes pushed a commit to NickBarnes/ocaml that referenced this pull request Jul 1, 2025
* Merge pull request ocaml#13286 from voodoos/distinct-uids-for-interfaces

[tooling] Distinct uids for interfaces

* Merge pull request ocaml#13308 from voodoos/link-declarations

[tooling] Remember linked declarations

* Store declaration dependencies in CMS files

* Backport directionality fix from upstream ocaml#13956

* Undo format change
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants