Skip to content

Fix directive @warning("-102") not working#8322

Draft
cknitt wants to merge 2 commits intomasterfrom
warning-102
Draft

Fix directive @warning("-102") not working#8322
cknitt wants to merge 2 commits intomasterfrom
warning-102

Conversation

@cknitt
Copy link
Copy Markdown
Member

@cknitt cknitt commented Apr 2, 2026

Fixes #8160.

Symptom

The reported repro was:

let x = (a, b) => {
  @warning("-102") (a->Pair.second > b->Pair.second)
}

With warning 102 enabled, this still produced:

Warning number 102
Polymorphic comparison introduced (maybe unsafe)

Root Cause

Warning 102 (Bs_polymorphic_comparison) was emitted too late, in compiler/core/lam_compile_primitive.ml.

By the time that code ran, local warning scopes created by source attributes such as @warning("-102") had already been restored. As a result, file-wide warning settings still worked, but expression-level suppression did not affect warning 102.

Fix

The warning emission was moved into typedtree-to-lambda translation in compiler/ml/translcore.ml, where it still runs under the relevant source-level warning scopes.

Concretely:

  • warning 102 is emitted when the polymorphic comparison primitive is selected during translation
  • the late warning emission was removed from compiler/core/lam_compile_primitive.ml
  • translation re-enters warning scopes so local source attributes apply at the warning site

This keeps warning 102 enabled by default, while making local suppression behave correctly.

Tests

Regression tests were added under tests/build_tests/super_errors/:

  • warning102_expr_attribute.res
  • warning102_structure_attribute.res
  • warning102_value_attribute.res

These cover:

  • expression-level @warning("-102")
  • file-level @@warning("-102")
  • direct value use such as let v = @warning("-102") compare

All corresponding snapshots are empty, because successful suppression means there is no warning output.

Result

After the fix:

  • @warning("-102") works for local polymorphic comparison expressions
  • @@warning("-102") still works
  • warning 102 still appears normally when no suppression is present

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Apr 2, 2026

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript@8322

@rescript/darwin-arm64

npm i https://pkg.pr.new/@rescript/darwin-arm64@8322

@rescript/darwin-x64

npm i https://pkg.pr.new/@rescript/darwin-x64@8322

@rescript/linux-arm64

npm i https://pkg.pr.new/@rescript/linux-arm64@8322

@rescript/linux-x64

npm i https://pkg.pr.new/@rescript/linux-x64@8322

@rescript/runtime

npm i https://pkg.pr.new/@rescript/runtime@8322

@rescript/win32-x64

npm i https://pkg.pr.new/@rescript/win32-x64@8322

commit: 1654767

@cknitt
Copy link
Copy Markdown
Member Author

cknitt commented Apr 2, 2026

@codex review

Copy link
Copy Markdown

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

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: 8f4f487f3f

ℹ️ 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 on lines +451 to +453
| Tstr_attribute x ->
Builtin_attributes.warning_attribute ~ppwarning:false
~report_attribute_errors:false x;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Preserve source-order scope for @@warning directives

Calling Builtin_attributes.warning_attribute here mutates the global warning state, but transl_structure translates rem before translating many current items (for example Tstr_value and Tstr_module), so a @@warning placed mid-structure can incorrectly apply to declarations that appear earlier in source order. In practice this can wrongly suppress or promote warnings (including @@warnerror) on the wrong bindings, which changes build outcomes. The warning setting should be applied with proper scoping/restoration that matches source order instead of mutating global state in this branch.

Useful? React with 👍 / 👎.

@cknitt
Copy link
Copy Markdown
Member Author

cknitt commented Apr 2, 2026

@codex review

Copy link
Copy Markdown

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

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: 165476741a

ℹ️ 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 on lines +407 to +409
let warn_on_polymorphic_comparison loc = function
| Pobjcomp _ | Pobjorder | Pobjmin | Pobjmax ->
Location.prerr_warning loc Warnings.Bs_polymorphic_comparison
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Do not warn on null/undefined equality primitives

Emitting Bs_polymorphic_comparison for every Pobjcomp here regresses the previous behavior for null/undefined checks: primitives like %equal_null / %equal_undefined / %equal_nullable (and ==/!= cases with a null/undefined constant) used to bypass warning 102 in lam_compile_primitive because they compile to dedicated null checks, but this early unconditional warning now fires before arguments are inspected. In projects using -warn-error +102, valid null/undefined comparisons can now fail compilation.

Useful? React with 👍 / 👎.

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.

@warning("-102") doesn't work

1 participant