Skip to content

Conversation

zth
Copy link
Member

@zth zth commented Aug 22, 2025

Closes #7579

Also adds error recovery so the parser can continue and spits out less irrelevant parse errors.

Copy link

pkg-pr-new bot commented Aug 22, 2025

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript-lang/rescript@7784

@rescript/darwin-arm64

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

@rescript/darwin-x64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/win32-x64

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

commit: 3d13dba

@zth zth marked this pull request as ready for review August 22, 2025 07:39
@zth zth requested review from cristianoc and Copilot August 22, 2025 07:39
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR improves error handling when developers attempt to use language keywords as record field names in ReScript. The change provides clearer error messages and better error recovery to continue parsing after encountering such errors.

  • Adds helpful error messages suggesting alternatives (e.g., type_) and the @as annotation for runtime compatibility
  • Implements error recovery to continue parsing and reduce cascading parse errors
  • Covers all record field contexts: type definitions, expressions, and patterns

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
res_core.ml Implements keyword detection and recovery logic for record fields with improved error messages
recordFieldKeywordInType.res Test case for keyword usage in record type definitions
recordFieldKeywordInExpr.res Test case for keyword usage in record expressions
recordFieldKeywordInPattern.res Test case for keyword usage in record patterns
expected/*.txt Expected error outputs showing improved error messages and recovery
CHANGELOG.md Documents the enhancement in the changelog

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@@ -398,6 +398,27 @@ let build_longident words =
| [] -> assert false
| hd :: tl -> List.fold_left (fun p s -> Longident.Ldot (p, s)) (Lident hd) tl

(* Recovers a keyword used as field name if it's probable that it's a full
Copy link
Preview

Copilot AI Aug 22, 2025

Choose a reason for hiding this comment

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

[nitpick] The comment has inconsistent spacing. There should be a space after 'full' to maintain proper spacing before the line break.

Copilot uses AI. Check for mistakes.

@@ -398,6 +398,27 @@ let build_longident words =
| [] -> assert false
| hd :: tl -> List.fold_left (fun p s -> Longident.Ldot (p, s)) (Lident hd) tl

(* Recovers a keyword used as field name if it's probable that it's a full
field name (not punning etc), by checking if there's a colon after it. *)
Copy link
Preview

Copilot AI Aug 22, 2025

Choose a reason for hiding this comment

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

[nitpick] The comment should end the sentence with a period before the closing comment marker for consistency.

Copilot uses AI. Check for mistakes.

@zth zth removed the request for review from cristianoc August 22, 2025 07:42
@zth zth marked this pull request as draft August 22, 2025 07:42
Copy link
Collaborator

@cristianoc cristianoc left a comment

Choose a reason for hiding this comment

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

Much cleaner.
Left some nitpick.

let keyword_txt = Token.to_string p.token in
let keyword_start = p.Parser.start_pos in
let keyword_end = p.Parser.end_pos in
let message =
Copy link
Collaborator

Choose a reason for hiding this comment

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

why identical message construction here and a few lines earlier?

Copy link
Member Author

Choose a reason for hiding this comment

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

Solved in a3870f7 (was just working on it when you commented).

@zth zth force-pushed the hint-keyword-as-record-field branch from 5ce8a8d to a3870f7 Compare August 22, 2025 08:49
@zth zth marked this pull request as ready for review August 22, 2025 08:59
@zth zth enabled auto-merge (squash) August 22, 2025 09:00
@zth zth merged commit fc7226b into master Aug 22, 2025
27 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.

Improve error message when using keyword as field name
2 participants