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

Compiler: better error message for symbol against enum #12478

Merged
merged 9 commits into from
Sep 21, 2022
Prev Previous commit
Next Next commit
Don't list some options
  • Loading branch information
Ary Borenszweig committed Sep 13, 2022
commit 545302011c97695473cd84c003d07ea455003000
21 changes: 1 addition & 20 deletions spec/compiler/semantic/call_error_spec.cr
Original file line number Diff line number Diff line change
@@ -221,7 +221,7 @@ describe "Call errors" do
"expected argument #1 symbol to 'foo' to match a Color enum member.\n\nDid you mean :red?"
end

it "says type mismatch for symbol against enum (list all possibilities when 5 or less)" do
it "says type mismatch for symbol against enum (list all possibilities when 10 or less)" do
assert_error %(
enum Color
Red
@@ -239,25 +239,6 @@ describe "Call errors" do
"expected argument #1 symbol to 'foo' to match a Color enum member.\n\nOptions are: :red, :green, :blue, :violet and :purple"
end

it "says type mismatch for symbol against enum (list some possibilities when more than 5)" do
assert_error %(
enum Color
Red
Green
Blue
Violet
Purple
Cyan
end

def foo(x : Color)
end

foo(:hello_world)
),
"expected argument #1 symbol to 'foo' to match a Color enum member.\n\nSome options are: :red, :green, :blue, :violet and :purple"
end

it "says type mismatch for symbol against enum, named argument case" do
assert_error %(
enum Color
8 changes: 2 additions & 6 deletions src/compiler/crystal/semantic/call_error.cr
Original file line number Diff line number Diff line change
@@ -317,12 +317,8 @@ class Crystal::Call
similar_name = Levenshtein.find(symbol.underscore, options)
if similar_name
str << "Did you mean :#{similar_name}?"
else
if options.size <= 5
str << "Options are: "
else
str << "Some options are: "
end
elsif options.size <= 10
str << "Options are: "
to_sentence(str, options.first(5).map { |o| ":#{o}" }, " and ")
asterite marked this conversation as resolved.
Show resolved Hide resolved
end
else