Skip to content

raise compile error when inspect protocol options don't match struct #11801

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

Merged

Conversation

jwharrow
Copy link
Contributor

@jwharrow jwharrow commented May 6, 2022

No description provided.

defp validate_option(option_list, fields, module, caller) do
if not Enum.empty?(option_list -- fields) do
description = "When deriving inspect protocol of #{module}, values must match struct fields"
raise CompileError, file: caller.file, line: caller.line, description: description
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be a regular ArgumentError :) CompileError is typically only raise from within the compiler.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would also be nice to show which fields do not match. Perhaps convert the if to a case and pattern match accordingly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated dc203bf

@@ -478,6 +481,15 @@ defimpl Inspect, for: Any do
end
end

defp validate_option(option_list, fields, module, caller) do
if not Enum.empty?(option_list -- fields) do
description = "When deriving inspect protocol of #{module}, values must match struct fields"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
description = "When deriving inspect protocol of #{module}, values must match struct fields"
description = "unknown fields #{inspect(...)} given when deriving the Inspect protocol for #{inspect(module)}. :only and :except values must match struct fields"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Originally i had something like this, but the problem is that inspect/1 is not yet available. I looked at some of the erlang :io_lib libraries and at :erlang.display/1 but those weren't exactly right. I'll take another stab at it using Enum.join/2 which will trim off the :s

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, we unimport inspect to avoid conflcit. You can use Kernel.inspect(module) instead. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, we unimport inspect to avoid conflcit. You can use Kernel.inspect(module) instead. :)

Thanks! it's wild how tricky it gets when i think I'm missing one function :)
updated: dc203bf

@josevalim
Copy link
Member

Thank you! I have dropped some comments which are mostly nitpicks around the error message!

@josevalim josevalim merged commit c053b4b into elixir-lang:main May 8, 2022
@josevalim
Copy link
Member

💚 💙 💜 💛 ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants