Skip to content

Fix DCE false positives for exception aliases raised across modules - #220

Merged
JonoPrest merged 21 commits into
masterfrom
jono-false-exn-alias
Sep 21, 2026
Merged

JonoPrest merged 21 commits into
masterfrom
jono-false-exn-alias

Conversation

@JonoPrest

Copy link
Copy Markdown
Contributor

Stacked on #218. An exception alias can be reported as never raised even when live code raises it from another module.

(* errors.ml *)
exception Used = Not_found
exception Unused = Not_found

(* main.ml *)
let () =
  try raise Errors.Used with _ -> ()

Before: Used can be reported as never raised. Expected: only Unused.

References to exception aliases can have ghost declaration locations, requiring deferred lookup by module path. Dune wrappers and mismatched compilation-unit names prevented these references from reaching the alias declaration.

Fix:

  • Index exception declarations by their actual compilation unit and follow explicit module aliases to resolve Dune wrappers.
  • Record resolved references in the liveness tracking used for exceptions.
  • Use the constructor’s location when the compiler leaves the outer exception location empty.

Regression fixtures cover used and unused aliases across modules, including wrapped libraries, and ensure references to external exceptions do not keep unrelated local aliases live.

Validated with dune build and npm test on OCaml 4.14.2, 5.2.1, 5.3.0, and 5.5.0. Also verified that the ReScript compiler’s used Location.Already_displayed_error alias is no longer reported.

@JonoPrest
JonoPrest added this pull request to stack #219 September 17, 2026 12:14
@JonoPrest
JonoPrest marked this pull request as draft September 17, 2026 12:16
@JonoPrest

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 17, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-09-18T13:47:38.426966Z e50da11 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 968019b2b5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/DeadException.ml Outdated
@JonoPrest

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4e165ee725

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/DeadException.ml Outdated
Comment thread src/DeadException.ml Outdated
@JonoPrest
JonoPrest force-pushed the jono-false-exn-alias branch 2 times, most recently from 83fd515 to 39f1c8f Compare September 18, 2026 10:13
@JonoPrest

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 39f1c8fef9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/DeadException.ml Outdated
@JonoPrest

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: e50da11a29

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Base automatically changed from jono-functor-precise-attribution to master September 18, 2026 13:50
An exception declared as an alias (exception Alias = A) and raised from
another module via the alias name is reported as never raised. The
expected output only reports DeadAlias, which is genuinely unused.
For exception X = Y, the constructor description at a raise site in
another module carries the location of Y (ghost for predefined
exceptions), so DeadException falls back to resolving the declaration by
path. That lookup never matched for OCaml projects:

- The declaration key used the source file name ("location"), while
  the path in the typed tree uses the module name ("Location").
- Paths in dune wrapped libraries and executables are prefixed with the
  wrapper module (Dune__exe, Deadcode_fixture), which is not part of the
  declaration key.
- Exception declarations count references from the type reference
  table, but the delayed path only recorded a value reference.

Normalize the module name in the key, retry the lookup with the
outermost module dropped, and record a type reference like the
non-delayed case.
@JonoPrest
JonoPrest marked this pull request as ready for review September 18, 2026 13:53
@JonoPrest

Copy link
Copy Markdown
Contributor Author

@cristianoc 2 more prs to merge in this stack. I've just rebased after merging the 2 below. All codex reviews were satisfied and the rebase doesn't change any code.

@cristianoc

Copy link
Copy Markdown
Collaborator

@cristianoc 2 more prs to merge in this stack. I've just rebased after merging the 2 below. All codex reviews were satisfied and the rebase doesn't change any code.

Great. Go ahead!
Qualitatively, how are things going? Is it basically working on the compiler sources now, or is there more you're aware of?

@JonoPrest

Copy link
Copy Markdown
Contributor Author

Great. Go ahead! Qualitatively, how are things going? Is it basically working on the compiler sources now, or is there more you're aware of?

Awesome, this seems to be the last of at least the false positives that were coming through on the compiler. There may well be more unreported dead code. But I will do one more session on the compiler for clean up and then I think we could use it in CI etc 👍🏼

@JonoPrest
JonoPrest merged commit a6b2df1 into master Sep 21, 2026
7 checks passed
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.

2 participants