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

Magic comment "immutable: string" makes "literal".freeze the default for that file #487

Closed
wants to merge 4 commits into
base: trunk
from
Jump to file or symbol
Failed to load files and symbols.
+20 −1
Diff settings

Always

Just for now

View
21 parse.y
@@ -267,6 +267,7 @@ struct parser_params {
int parser_ruby_sourceline; /* current line no. */
VALUE parser_ruby_sourcefile_string;
rb_encoding *enc;
int immutable_strings;
int parser_yydebug;
@@ -3839,6 +3840,11 @@ string1 : tSTRING_BEG string_contents tSTRING_END
{
/*%%%*/
$$ = $2;
if (parser->immutable_strings) {
$$ = NEW_CALL($2, idFreeze, 0);

This comment has been minimized.

@charliesome

charliesome Dec 24, 2013

Member

We should probably just create a new NODE_LIT for the string here if immutable_strings is non-zero.

@charliesome

charliesome Dec 24, 2013

Member

We should probably just create a new NODE_LIT for the string here if immutable_strings is non-zero.

This comment has been minimized.

@ColinDKelley

ColinDKelley Dec 24, 2013

@charliesome Thanks for the feedback. We are adding a call to .freeze here so we get the frozen literal optimization as added by https://bugs.ruby-lang.org/issues/9042. Could NODE_LIT achieve that same effect?

@ColinDKelley

ColinDKelley Dec 24, 2013

@charliesome Thanks for the feedback. We are adding a call to .freeze here so we get the frozen literal optimization as added by https://bugs.ruby-lang.org/issues/9042. Could NODE_LIT achieve that same effect?

This comment has been minimized.

@tmm1

tmm1 Dec 24, 2013

Contributor

Yes, the NODE_LIT will be frozen by the compiler.

@tmm1

tmm1 Dec 24, 2013

Contributor

Yes, the NODE_LIT will be frozen by the compiler.

This comment has been minimized.

@ColinDKelley
@ColinDKelley

ColinDKelley Dec 26, 2013

} else {
$$ = $2;
}
/*%
$$ = dispatch1(string_literal, $2);
%*/
@@ -3865,7 +3871,12 @@ xstring : tXSTRING_BEG xstring_contents tSTRING_END
break;
}
}
$$ = node;
if (parser->immutable_strings) {
$$ = NEW_CALL(node, idFreeze, 0);

This comment has been minimized.

@charliesome

charliesome Dec 24, 2013

Member

ditto

@charliesome

charliesome Dec 24, 2013

Member

ditto

} else {
$$ = node;
}
/*%
$$ = dispatch1(xstring_literal, $2);
%*/
@@ -6653,6 +6664,12 @@ parser_set_encode(struct parser_params *parser, const char *name)
#endif
}
static void
parser_set_immutable(struct parser_params *parser, const char *name, const char *val)
{
if (strcmp(val, "string") == 0) { parser->immutable_strings = 1; }
}
static int
comment_at_top(struct parser_params *parser)
{
@@ -6710,6 +6727,7 @@ static const struct magic_comment magic_comments[] = {
{"coding", magic_comment_encoding, parser_encode_length},
{"encoding", magic_comment_encoding, parser_encode_length},
{"warn_indent", parser_set_token_info},
{"immutable", parser_set_immutable}
};
#endif
@@ -10862,6 +10880,7 @@ parser_initialize(struct parser_params *parser)
parser->heap = NULL;
#endif
parser->enc = rb_utf8_encoding();
parser->immutable_strings = 0;
}
#ifdef RIPPER
ProTip! Use n and p to navigate between commits in a pull request.