Skip to content

Commit

Permalink
Fix tool implementations to handle gracefully a def with missing lo…
Browse files Browse the repository at this point in the history
…cation (#15273)

Without this change, `crystal tool implementations` would crash when a target def has no location.

The example uses an enum type's `.new` method which is defined by the compiler without a location.

This patch fixes the nil error. The result may still not be terribly useful, but it's better than crashing the program.
  • Loading branch information
straight-shoota authored Dec 15, 2024
1 parent 9b377c3 commit 9183bb0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
14 changes: 14 additions & 0 deletions spec/compiler/crystal/tools/implementations_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -476,4 +476,18 @@ describe "implementations" do
F‸oo
)
end

it "find implementation on def with no location" do
_, result = processed_implementation_visitor <<-CRYSTAL, Location.new(".", 5, 5)
enum Foo
FOO
end
Foo.new(42)
CRYSTAL

result.implementations.not_nil!.map do |e|
Location.new(e.filename, e.line, e.column).to_s
end.should eq ["<unknown>:0:0"]
end
end
6 changes: 4 additions & 2 deletions src/compiler/crystal/tools/implementations.cr
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ module Crystal
@line = macro_location.line_number + loc.line_number
@column = loc.column_number
else
raise "not implemented"
@line = loc.line_number
@column = loc.column_number
@filename = "<unknown>"
end
end

Expand Down Expand Up @@ -111,7 +113,7 @@ module Crystal

if target_defs = node.target_defs
target_defs.each do |target_def|
@locations << target_def.location.not_nil!
@locations << (target_def.location || Location.new(nil, 0, 0))
end
end
false
Expand Down

0 comments on commit 9183bb0

Please sign in to comment.