Skip to content

Commit f4f0560

Browse files
authored
Fix content location on Prism MatchCurLine nodes (sorbet#9596)
* Fix content location on `MatchCurLine` nodes * Remove no-op length check * Clarify Regopt comment, refactor loc logic
1 parent b094731 commit f4f0560

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

parser/prism/Translator.cc

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2851,8 +2851,10 @@ unique_ptr<parser::Node> Translator::translate(pm_node_t *node, bool preserveCon
28512851
// ...that implicitly checks against the last read line by an IO object, e.g. `if /wat/`
28522852
auto matchLastLineNode = down_cast<pm_match_last_line_node>(node);
28532853

2854+
auto contentLoc = translateLoc(matchLastLineNode->content_loc);
2855+
28542856
auto regex =
2855-
translateRegexp(location, location, matchLastLineNode->unescaped, matchLastLineNode->closing_loc);
2857+
translateRegexp(location, contentLoc, matchLastLineNode->unescaped, matchLastLineNode->closing_loc);
28562858

28572859
return make_unsupported_node<parser::MatchCurLine>(location, move(regex));
28582860
}
@@ -4707,21 +4709,22 @@ core::NameRef Translator::nextUniqueDesugarName(core::NameRef original) {
47074709
// Had to widen the type from `parser::Assign` to `parser::Node` to handle `make_node_with_expr` correctly.
47084710
// TODO: narrow the type back after direct desugaring is complete. https://github.com/Shopify/sorbet/issues/671
47094711
unique_ptr<parser::Node> Translator::translateRegexpOptions(pm_location_t closingLoc) {
4710-
auto length = closingLoc.end - closingLoc.start;
4711-
4712-
// Chop off `/` from Regopt location, so the location only spans the options themselves:
4713-
// `/foo/im`
4714-
// ^^
4715-
constexpr uint32_t offset = "/"sv.size();
4716-
auto location = translateLoc(closingLoc.start + offset, closingLoc.end);
4717-
4718-
string_view options;
4719-
4720-
if (length > 0) {
4721-
options = sliceLocation(closingLoc).substr(1); // one character after the closing `/`
4722-
} else {
4723-
options = string_view();
4724-
}
4712+
ENFORCE(closingLoc.start && closingLoc.end);
4713+
4714+
// Chop off Regexp's closing delimiter from the start of the Regopt location,
4715+
// so the location only spans the options themselves:
4716+
// /foo/im
4717+
// ^^
4718+
// $r(foo)im
4719+
// ^^
4720+
// $r[foo]im
4721+
// ^^
4722+
// $r{foo}im
4723+
// ^^
4724+
auto prismLoc = pm_location_t{.start = closingLoc.start + 1, .end = closingLoc.end};
4725+
auto location = translateLoc(prismLoc);
4726+
4727+
string_view options = sliceLocation(prismLoc);
47254728

47264729
if (!directlyDesugar) {
47274730
return make_unique<parser::Regopt>(location, options);

test/BUILD

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ prism_location_test_suite(
140140
"prism_regression/error_recovery/assign.rb",
141141
"prism_regression/if_elsif.rb",
142142
"prism_regression/lambda.rb",
143-
"prism_regression/match_last_line.rb",
144143
"prism_regression/multi_target.rb",
145144
"prism_regression/multi_write.rb",
146145
"prism_regression/numbered_params.rb",

0 commit comments

Comments
 (0)