Skip to content

Commit 83d5b02

Browse files
committed
Ignore internal frames in Kernel#warn
1 parent c89b34c commit 83d5b02

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/main/ruby/truffleruby/core/kernel.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,17 @@ def warn(*messages, uplevel: undefined)
641641
uplevel = Primitive.rb_to_int(uplevel)
642642
raise ArgumentError, "negative level (#{uplevel})" unless uplevel >= 0
643643

644-
caller, = Kernel.caller_locations(uplevel + 1, 1)
644+
uplevel += 1 # skip Kernel#warn itself
645+
initial, = Kernel.caller_locations(uplevel, 1)
646+
caller = initial
647+
# MRI would reuse the file:line of the user code caller for methods defined in C.
648+
# Similarly, we skip <internal:* calls, notably to skip Kernel#require calls.
649+
while caller and caller.path.start_with?('<internal:')
650+
uplevel += 1
651+
caller, = Kernel.caller_locations(uplevel, 1)
652+
end
653+
caller = initial unless caller
654+
645655
if caller
646656
"#{caller.path}:#{caller.lineno}: warning: "
647657
else

0 commit comments

Comments
 (0)