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

Add specs for regex literal expansion #13253

Conversation

straight-shoota
Copy link
Member

@straight-shoota straight-shoota commented Mar 31, 2023

This is a useful addition in general and particularly prepares for #13252

Edit: Also discovers and fixes a bug (?) introduced in #13223.

Previous to #13223, the base type of Regex::Options was the default Int32. That PR changed it to UInt64 to gain more space.
The parser creates an instance of Regex::Options from the modifier flags of a regex literal. When the compiler expands them, the option value is expressed by the base type value of the Regex::Options type. The resulting number literal is typed, so the expression for the MULTILINE options value would change from ::Regex::Options.new(6) to ::Regex::Options.new(6_u64).
Now this isn't an immediate problem when the base type of Regex::Options in the compiler and stdlib align. And even if they don't, autocasting of integer literals should allow the generated code to be compatible with older Regex::Options implementations with Int32 base type.
Still, I think it's better to avoid such unintended side effects and keep the generated code consistent until we explicitly chose to change it for good as discussed in #13252.
The different domain range of Int32 and UInt64 is irrelevant for this because the compiler only handles the three modifiers and the biggest integer value needed to represent them is 15 (for imx).

end

private def regex_options(node)
Call.new(Path.global(["Regex", "Options"]).at(node), "new", NumberLiteral.new(node.options.value.to_s).at(node)).at(node)
Copy link
Member Author

Choose a reason for hiding this comment

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

The to_s is necessary to avoid a tight coupling between the value type of the compiler and stdlib. The base type of Regex::Options was changed to UInt64 in #13223.

@straight-shoota straight-shoota added kind:bug kind:regression Something that used to correctly work but no longer works labels Apr 3, 2023
@straight-shoota straight-shoota added this to the 1.8.0 milestone Apr 4, 2023
@straight-shoota straight-shoota merged commit 706075e into crystal-lang:master Apr 5, 2023
@straight-shoota straight-shoota deleted the spec/regex-literal-expander branch April 5, 2023 07:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug kind:regression Something that used to correctly work but no longer works kind:specs topic:compiler:semantic
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants