Skip to content
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

Translate conditionals in in statements #301

Merged
merged 3 commits into from
Oct 25, 2024
Merged

Translate conditionals in in statements #301

merged 3 commits into from
Oct 25, 2024

Conversation

st0012
Copy link
Member

@st0012 st0012 commented Oct 24, 2024

Motivation

Closes #204

When investigating #204 I found that Parser's IfGuard and UnlessGuard are associated with these comments:

Screenshot 2024-10-24 at 18 56 46

And I found that they're not handled by the current pattern translation yet. For example:

# pattern matching with if guards
case bar
in x if x == 1
  "in with if"
in a, b if b == 2
  "in with 2 args and if"
in c, d; c if c == 3
  "in with 2 args, semicolon, and if"
end

# pattern matching with unless guards
case baz
in x unless x == 1
  "in with unless"
in a, b unless b == 2
  "in with 2 args and unless"
in c, d; c unless c == 3
  "in with 2 args, semicolon, and unless"
end

So this PR adds support for those cases.

Test plan

See included automated tests.

@st0012 st0012 requested review from amomchilov and egiurleo October 24, 2024 17:58
@st0012 st0012 self-assigned this Oct 24, 2024
Comment on lines 1370 to 1367
if (prismStatements->body.size != 1) {
unreachable("In pattern-matching's `in` clause, a guard must have a single statement.");
}

Choose a reason for hiding this comment

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

Suggested change
if (prismStatements->body.size != 1) {
unreachable("In pattern-matching's `in` clause, a guard must have a single statement.");
}
ENFORCE(prismStatements->body.size == 1,
"In pattern-matching's `in` clause, a guard must have a single statement.");

Choose a reason for hiding this comment

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

Enforcing this is good for now, but we'll eventually want to tolerate this error and report it as a syntax error.

This is the current Sorbet's parser behaviour:

  case quux
# ^^^^ error: unexpected token "case"
  in x if 1, 2
  end

Interestingly, the , 2 makes the case invalid, not the in or if. Removing that makes it parse correctly.

Copy link
Member Author

Choose a reason for hiding this comment

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

in x if 1, 2 in this case doesn't enter this check though because the conditional still only has one statement (x)?

Choose a reason for hiding this comment

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

Are you sure it's not the 1, 2? Maybe I'm misunderstanding.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes I'm sure because the prismStatements is referring to the statements under the IfNode, not the InNode:

case bar
in x if 1, 2
end
Screenshot 2024-10-24 at 22 23 10

I will rename the variable to make that clearer.

@st0012 st0012 force-pushed the fix-#204 branch 2 times, most recently from 72f946e to 2463053 Compare October 24, 2024 20:41
@st0012 st0012 requested a review from amomchilov October 24, 2024 20:49
Copy link

@amomchilov amomchilov left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for rebasing on my big changes!

Can you please open an issue for use to follow up and diagnose this correctly?

  case quux
# ^^^^ error: unexpected token "case"
  in x if 1, 2
  end

Comment on lines 1370 to 1367
if (prismStatements->body.size != 1) {
unreachable("In pattern-matching's `in` clause, a guard must have a single statement.");
}

Choose a reason for hiding this comment

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

Are you sure it's not the 1, 2? Maybe I'm misunderstanding.

@st0012
Copy link
Member Author

st0012 commented Oct 24, 2024

Added the issue here: #303

@st0012 st0012 requested a review from amomchilov October 24, 2024 22:06
Copy link

@amomchilov amomchilov left a comment

Choose a reason for hiding this comment

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

Forgot to approve, myb

@st0012 st0012 merged commit a146baa into prism Oct 25, 2024
1 check passed
@st0012 st0012 deleted the fix-#204 branch October 25, 2024 16:02
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.

Handle if and unless modifiers correctly
2 participants