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

Kernel#lambda raises when given non-lambda, non-literal block #3753

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -44,6 +44,7 @@ Compatibility:
* Support `symbolize_names` argument to `MatchData#named_captures` (#3681, @rwstauner).
* Support `Proc#initialize_{dup,copy}` for subclasses (#3681, @rwstauner).
* Remove deprecated `Encoding#replicate` method (#3681, @rwstauner).
* `Kernel#lambda` with now raises `ArgumentError` when given a non-lambda, non-literal block (#3681, @Th3-M4jor).

Performance:

Expand Down
1 change: 0 additions & 1 deletion spec/tags/core/kernel/lambda_tags.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
fails:Kernel.lambda creates a lambda-style Proc when called with zsuper
fails:Kernel.lambda does not create lambda-style Procs when captured with #method
fails:Kernel.lambda when called without a literal block raises when proc isn't a lambda
20 changes: 7 additions & 13 deletions src/main/java/org/truffleruby/core/kernel/KernelNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -1148,23 +1148,17 @@ RubyProc lambdaFromProcBlock(RubyProc block) {
return ProcOperations.createLambdaFromBlock(getContext(), getLanguage(), block);
}

@Specialization(guards = { "!isLiteralBlock(block)", "block.isProc()" })
RubyProc lambdaFromExistingProc(RubyProc block,
@Cached WarnNode warnNode) {
if (warnNode.shouldWarnForDeprecation()) {
warnNode.warningMessage(
getContext().getCallStack().getTopMostUserSourceSection(),
"lambda without a literal block is deprecated; use the proc without lambda instead");
}

// If the argument isn't a literal, its original behaviour (proc or lambda) is preserved.
@Specialization(guards = { "!isLiteralBlock(block)", "block.isLambda()" })
RubyProc lambdaFromExistingLambda(RubyProc block) {
// If the argument isn't a literal, its original behaviour is preserved only if its a lambda.
return block;
}

@Specialization(guards = { "!isLiteralBlock(block)", "block.isLambda()" })
@Specialization(guards = { "!isLiteralBlock(block)", "block.isProc()" })
RubyProc lambdaFromExistingProc(RubyProc block) {
// If the argument isn't a literal, its original behaviour (proc or lambda) is preserved.
return block;
throw new RaiseException(
getContext(),
coreExceptions().argumentError("the lambda method requires a literal block", this));
}

@TruffleBoundary
Expand Down
Loading