Skip to content

Keep @tag and runtime annotations when printing a type declaration - #9136

Merged
cknitt merged 3 commits into
masterfrom
print-type-annotations
Oct 30, 2026
Merged

cknitt merged 3 commits into
masterfrom
print-type-annotations

Conversation

@cknitt

@cknitt cknitt commented Oct 30, 2026 •

Copy link
Copy Markdown
Member

Fixes #9134.

Printtyp.tree_of_type_decl passed on only @immediate and @unboxed. The other annotations of a declaration, @tag, @notUndefined, @instanceOf and @typeOf, were dropped wherever a declaration was printed:

  • Error messages printed the declarations a mismatch was about without the annotation the message named; with @tag, both sides looked the same next to "Their @tag annotations differ."
  • createInterface wrote type wrapped = … without @tag("kind") for a type of an applied functor, and the generated interface did not compile against its implementation.
  • Hover hid them too (@notUndefined type Jsx.element now shows its annotation).

The printed declaration now carries a list of annotations (otype_attributes) in place of the two booleans, filled from what the declaration itself states, and Res_outcome_printer prints them before type.

New test: CreateInterfaceFunctor.res / CreateInterfaceFunctorUse.res (analysis tests) apply a functor with a @tag type, and the generated interface keeps it. The changed error snapshots show the annotations on the expected and actual declarations.

With the annotations printed, two declarations can still read the same and not match, when a type they mention differs between the two sides, e.g. a base class that the interface leaves abstract. The reasons then start with a note that replaces the lead-in of a difference in runtime representation:

Actual declaration (Classes.res:2:20-44):
  @instanceOf("Dog") type dog = private animal

Expected declaration (Classes.resi:2:20-44):
  @instanceOf("Dog") type dog = private animal

The declarations read the same, but a type they mention differs:
  Actual declaration: instances of Dog, a subclass of Animal
  Expected declaration: instances of Dog

This is the bottom of a stack with #9133 on top.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gLpf7RN1MiRcubPgRn

@github-actions

github-actions Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • Keep @tag, @notUndefined, @instanceOf and @typeOf when printing a type declaration, in error messages, hover and generated interfaces.

Semantic Diff

3 commits · Full page

Keep @tag and runtime annotations when printing a type declaration

08279c2 · cknitt + Claude · Oct 30, 2026, 07:14 UTC

Why

The printer of type declarations kept only @immediate and @unboxed. @tag, @notUndefined, @instanceOf and @typeOf were dropped, so a signature mismatch about them printed two declarations that looked the same, hover hid them, and createInterface wrote an interface for an applied functor that does not match its implementation (#9134).

Diagnostic modified (@tag mismatch)

// Foo.res
@tag("type")
type t =
  | @as("a") A
  | B
// Foo.resi
@tag("kind")
type t =
  | @as("a") A
  | B

diagnostic

 Actual declaration (Foo.res:2:1-4:5):
-  type t = @as("a") A | B
+  @tag("type") type t = @as("a") A | B
 
 Expected declaration (Foo.resi:2:1-4:5):
-  type t = @as("a") A | B
+  @tag("kind") type t = @as("a") A | B
 
 Their @tag annotations differ.

Diagnostic modified (@notUndefined functor parameter)

module Make = (
  X: {
    @notUndefined type t
  },
) => {
  type u = X.t
}
module Applied = Make({
  type t = option<int>
})

diagnostic

 Actual declaration (not_undefined_functor_argument.res:10:3-22):
   type t = option<int>
 
 Expected declaration (not_undefined_functor_argument.res:3:19-24):
-  type t
+  @notUndefined type t
 
 Their runtime representations differ:
   Actual declaration: can hold undefined
   Expected declaration: @notUndefined

Format modified (createInterface for an applied functor)

module Tagged = (X: Type) => {
  @tag("kind") type wrapped = Value({value: X.t}) | Empty({reason: string})
}
module IntTagged = Tagged({
  type t = int
})

rescript · Before: the interface does not compile against the implementation

 module IntTagged: {
-  type wrapped = Value({value: int}) | Empty({reason: string})
+  @tag("kind") type wrapped = Value({value: int}) | Empty({reason: string})
 }

Representation modified (printed declaration annotations, Printtyp.tree_of_type_decl)

representation

-otype_immediate: bool         ↦ @immediate
-otype_unboxed: bool           ↦ @unboxed
+otype_attributes: string list ↦ @immediate @unboxed @tag("…") @notUndefined | @instanceOf("…") | @typeOf("…")
+source: what the declaration itself states, not what it inherits from an abbreviated type

Review

  • Why read the attributes rather than the declaration's runtime shape? The shape of an abbreviation can come from the type it abbreviates; the printed declaration should show what it states itself, as the source does.
  • Can printing raise for a malformed annotation? No. The annotations are parsed by the functions that typing uses, which raise on malformed payloads; those were reported when the declaration was typed, and the printer then leaves the annotation out.
Say when declarations that differ read the same

922b663 · cknitt + Claude · Oct 30, 2026, 15:50 UTC

Why

With the annotations printed, two declarations can still read the same and not match, when a type they mention differs between the two sides, e.g. a base class that the interface leaves abstract. The error then showed the same text twice, which reads as a bug in the compiler. The reasons now start with a note saying so, which replaces the lead-in of a difference in runtime representation.

Diagnostic modified (declarations that print the same)

// Classes.res
@instanceOf("Animal") type animal
@instanceOf("Dog") type dog = private animal
// Classes.resi
type animal
@instanceOf("Dog") type dog = private animal

diagnostic

 Actual declaration (Classes.res:2:20-44):
   @instanceOf("Dog") type dog = private animal
 
 Expected declaration (Classes.resi:2:20-44):
   @instanceOf("Dog") type dog = private animal
 
-Their runtime representations differ:
+The declarations read the same, but a type they mention differs:
   Actual declaration: instances of Dog, a subclass of Animal
   Expected declaration: instances of Dog

Review

  • Why compare the printed text? It is what the reader sees, and the case to catch is exactly two declarations that look the same.
✨ Escape a @tag name when printing it

6176c4d · cknitt + Claude · Oct 30, 2026, 16:06 UTC · new

Codex review of #9136: the printed @tag put the decoded name between quotes as it is, so a tag with a quote printed as invalid source and a backslash changed its value when reparsed, also in interfaces written by createInterface. It is now encoded as a string literal, which keeps non-ASCII letters as they are.

Diagnostic modified (@tag names with a quote, a backslash and a non-ASCII letter)

module M: {
  @tag("k\"ind") type t = A({x: int}) | B({y: int})
} = {
  @tag("typ\\é") type t = A({x: int}) | B({y: int})
}

diagnostic

 Actual declaration (tag_name_escaped.res:6:18-51):
-  @tag("typ\é") type t = A({x: int}) | B({y: int})
+  @tag("typ\\é") type t = A({x: int}) | B({y: int})
 
 Expected declaration (tag_name_escaped.res:4:18-51):
-  @tag("k"ind") type t = A({x: int}) | B({y: int})
+  @tag("k\"ind") type t = A({x: int}) | B({y: int})

@cknitt
cknitt force-pushed the print-type-annotations branch from d7ddc65 to bef0fe8 Compare October 30, 2026 09:07
@codecov

codecov Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.59459% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.70%. Comparing base (acdf563) to head (6176c4d).

Files with missing lines Patch % Lines
compiler/ml/includecore.ml 94.73% 1 Missing ⚠️
compiler/ml/printtyp.ml 91.66% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master    #9136   +/-   ##
=======================================
  Coverage   81.69%   81.70%           
=======================================
  Files         460      460           
  Lines       64855    64877   +22     
=======================================
+ Hits        52984    53008   +24     
+ Misses      11871    11869    -2     
Files with missing lines Coverage Δ
compiler/ml/includemod.ml 75.44% <100.00%> (+0.14%) ⬆️
compiler/syntax/src/res_outcome_printer.ml 82.45% <100.00%> (+0.18%) ⬆️
compiler/ml/includecore.ml 79.47% <94.73%> (+0.69%) ⬆️
compiler/ml/printtyp.ml 72.43% <91.66%> (+0.21%) ⬆️

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pkg-pr-new

pkg-pr-new Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/belt

npm i https://pkg.pr.new/@rescript/belt@9136

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: 6176c4d

@github-actions

Copy link
Copy Markdown

@cknitt

cknitt commented Oct 30, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 30, 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-10-30T16:10:26.790809Z 6176c4d 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: bef0fe85a2

ℹ️ 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 compiler/ml/printtyp.ml Resolved
cknitt and others added 2 commits October 30, 2026 15:48
Why:
    The printer of type declarations kept only @Immediate and @unboxed.
    @tag, @notundefined, @instanceof and @typeof were dropped, so a
    signature mismatch about them printed two declarations that looked the
    same, hover hid them, and createInterface wrote an interface for an
    applied functor that does not match its implementation (#9134).

Diagnostic modified (@tag mismatch):
Example:
    // Foo.res
    @tag("type")
    type t =
      | @as("a") A
      | B
    // Foo.resi
    @tag("kind")
    type t =
      | @as("a") A
      | B
Before (diagnostic):
    Actual declaration (Foo.res:2:1-4:5):
      type t = @as("a") A | B

    Expected declaration (Foo.resi:2:1-4:5):
      type t = @as("a") A | B

    Their @tag annotations differ.
After (diagnostic):
    Actual declaration (Foo.res:2:1-4:5):
      @tag("type") type t = @as("a") A | B

    Expected declaration (Foo.resi:2:1-4:5):
      @tag("kind") type t = @as("a") A | B

    Their @tag annotations differ.

Diagnostic modified (@notundefined functor parameter):
Example:
    module Make = (
      X: {
        @notundefined type t
      },
    ) => {
      type u = X.t
    }
    module Applied = Make({
      type t = option<int>
    })
Before (diagnostic):
    Actual declaration (not_undefined_functor_argument.res:10:3-22):
      type t = option<int>

    Expected declaration (not_undefined_functor_argument.res:3:19-24):
      type t

    Their runtime representations differ:
      Actual declaration: can hold undefined
      Expected declaration: @notundefined
After (diagnostic):
    Actual declaration (not_undefined_functor_argument.res:10:3-22):
      type t = option<int>

    Expected declaration (not_undefined_functor_argument.res:3:19-24):
      @notundefined type t

    Their runtime representations differ:
      Actual declaration: can hold undefined
      Expected declaration: @notundefined

Format modified (createInterface for an applied functor):
Example:
    module Tagged = (X: Type) => {
      @tag("kind") type wrapped = Value({value: X.t}) | Empty({reason: string})
    }
    module IntTagged = Tagged({
      type t = int
    })
Before (rescript): the interface does not compile against the implementation
    module IntTagged: {
      type wrapped = Value({value: int}) | Empty({reason: string})
    }
After (rescript):
    module IntTagged: {
      @tag("kind") type wrapped = Value({value: int}) | Empty({reason: string})
    }

Representation modified (printed declaration annotations, Printtyp.tree_of_type_decl):
Before (representation):
    otype_immediate: bool ↦ @Immediate
    otype_unboxed: bool ↦ @unboxed
After (representation):
    otype_attributes: string list ↦ @Immediate @unboxed @tag("…") @notundefined | @instanceof("…") | @typeof("…")
    source: what the declaration itself states, not what it inherits from an abbreviated type

Review: Why read the attributes rather than the declaration's runtime shape? The shape of an abbreviation can come from the type it abbreviates; the printed declaration should show what it states itself, as the source does.
Review: Can printing raise for a malformed annotation? No. The annotations are parsed by the functions that typing uses, which raise on malformed payloads; those were reported when the declaration was typed, and the printer then leaves the annotation out.

Fixes: #9134
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gLpf7RN1MiRcubPgRn
Signed-off-by: Christoph Knittel <ck@cca.io>
Why:
    With the annotations printed, two declarations can still read the same
    and not match, when a type they mention differs between the two sides,
    e.g. a base class that the interface leaves abstract. The error then
    showed the same text twice, which reads as a bug in the compiler. The
    reasons now start with a note saying so, which replaces the lead-in of a
    difference in runtime representation.

Diagnostic modified (declarations that print the same):
Example:
    // Classes.res
    @instanceof("Animal") type animal
    @instanceof("Dog") type dog = private animal
    // Classes.resi
    type animal
    @instanceof("Dog") type dog = private animal
Before (diagnostic):
    Actual declaration (Classes.res:2:20-44):
      @instanceof("Dog") type dog = private animal

    Expected declaration (Classes.resi:2:20-44):
      @instanceof("Dog") type dog = private animal

    Their runtime representations differ:
      Actual declaration: instances of Dog, a subclass of Animal
      Expected declaration: instances of Dog
After (diagnostic):
    Actual declaration (Classes.res:2:20-44):
      @instanceof("Dog") type dog = private animal

    Expected declaration (Classes.resi:2:20-44):
      @instanceof("Dog") type dog = private animal

    The declarations read the same, but a type they mention differs:
      Actual declaration: instances of Dog, a subclass of Animal
      Expected declaration: instances of Dog

Review: Why compare the printed text? It is what the reader sees, and the case to catch is exactly two declarations that look the same.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gLpf7RN1MiRcubPgRn
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt

cknitt commented Oct 30, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: 922b663646

ℹ️ 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".

Codex review of #9136: the printed @tag put the decoded name between quotes
as it is, so a tag with a quote printed as invalid source and a backslash
changed its value when reparsed, also in interfaces written by
createInterface. It is now encoded as a string literal, which keeps
non-ASCII letters as they are.

Diagnostic modified (@tag names with a quote, a backslash and a non-ASCII letter):
Example:
    module M: {
      @tag("k\"ind") type t = A({x: int}) | B({y: int})
    } = {
      @tag("typ\\é") type t = A({x: int}) | B({y: int})
    }
Before (diagnostic):
    Actual declaration (tag_name_escaped.res:6:18-51):
      @tag("typ\é") type t = A({x: int}) | B({y: int})

    Expected declaration (tag_name_escaped.res:4:18-51):
      @tag("k"ind") type t = A({x: int}) | B({y: int})
After (diagnostic):
    Actual declaration (tag_name_escaped.res:6:18-51):
      @tag("typ\\é") type t = A({x: int}) | B({y: int})

    Expected declaration (tag_name_escaped.res:4:18-51):
      @tag("k\"ind") type t = A({x: int}) | B({y: int})

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gLpf7RN1MiRcubPgRn
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt

cknitt commented Oct 30, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 6176c4d28f

ℹ️ 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".

@cknitt
cknitt merged commit b23f95b into master Oct 30, 2026
26 checks passed
@cknitt
cknitt deleted the print-type-annotations branch October 30, 2026 17:15
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.

Printed type declarations drop @tag, @notUndefined and runtime annotations

2 participants