Skip to content

Fix regex literal loc #7683

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

- Fix `--create-sourcedirs` generation with for a single project. https://github.com/rescript-lang/rescript/pull/7671
- Fix rewatch not recompiling on changes under windows. https://github.com/rescript-lang/rescript/pull/7690
- Fix locations of regex literals. https://github.com/rescript-lang/rescript/pull/7683

# 12.0.0-beta.2

Expand Down
9 changes: 4 additions & 5 deletions compiler/syntax/src/res_core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1812,8 +1812,7 @@ and parse_constrained_expr_region p =
| _ -> Some expr)
| _ -> None

and parse_regex p pattern flags =
let start_pos = p.Parser.start_pos in
and parse_regex ~start_pos p pattern flags =
Parser.next p;
let loc = mk_loc start_pos p.prev_end_pos in
let payload =
Expand All @@ -1826,7 +1825,7 @@ and parse_regex p pattern flags =
if p.mode = ParseForTypeChecker then Some "js" else None )));
]
in
Ast_helper.Exp.extension (Location.mknoloc "re", payload)
Ast_helper.Exp.extension (Location.mkloc "re" loc, payload)

(* Atomic expressions represent unambiguous expressions.
* This means that regardless of the context, these expressions
Expand Down Expand Up @@ -1903,13 +1902,13 @@ and parse_atomic_expr p =
| Forwardslash -> (
Parser.next_regex_token p;
match p.token with
| Regex (pattern, flags) -> parse_regex p pattern flags
| Regex (pattern, flags) -> parse_regex ~start_pos p pattern flags
| _ -> Ast_helper.Exp.extension (Location.mknoloc "re", Parsetree.PStr [])
)
| ForwardslashDot -> (
Parser.next_regex_token p;
match p.token with
| Regex (pattern, flags) -> parse_regex p ("." ^ pattern) flags
| Regex (pattern, flags) -> parse_regex ~start_pos p ("." ^ pattern) flags
| _ -> Ast_helper.Exp.extension (Location.mknoloc "re", Parsetree.PStr [])
)
| token -> (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

We've found a bug for you!
/.../fixtures/extract_from_none_file.res
/.../fixtures/regex_literal.res:1:7-11

1 │ while /foo/ {
2 │ ()
3 │ }

This has type: RegExp.t
But a while loop condition must always be of type: bool

This file was deleted.

3 changes: 3 additions & 0 deletions tests/build_tests/super_errors/fixtures/regex_literal.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
while /foo/ {
()
}