Fix Elixir 1.20 type warnings: dead code and latent bugs#561
Merged
Conversation
- fixes for dead code - fixed true latent bugs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Elixir 1.20-rc.6 ships an improved set-theoretic type system whose inference can now prove dead code and latent logic bugs at compile time. A
mix compile --forcesurfaced 16 warnings across the library, and the test suite surfaced 2 more. Each warning was either provably-unreachable code (safe to remove) or a clause whose intent was silently defeated by an over-broad guard or shadowing — the latter being real bugs. This change resolves all of them so the codebase compiles cleanly under the new compiler.Solution
Each warning was triaged into one of two buckets and fixed accordingly, never suppressed:
Dead code — removed or simplified with identical runtime behavior. This covers 10 unused
require Loggerdirectives, a byte-identical duplicatefor_api/1clause inChatGoogleAI, an unreachable{:error, _reason}catch-all (Message.new/1only ever returns changeset errors), an always-truetype == :objectsub-term inside acondwhose earlier clauses already eliminated:object, and two|| defaultfallbacks whose left side was type-proven non-nil.Latent bugs — fixed to honor the original intent:
MessageDelta.append_to_merged_content/2: thecontent in [nil, []]"nothing to merge" guard shadowed a later clause meant to merge an already-processedmerged_contentContentPart, silently dropping it. Reordered the specific clause ahead of the broad guard.ChatOrqTest: the streaming test readmerged.content, whichmerge_delta/2always clears tonil(text lives inmerged_content). The assertion only ever passed on its""fallback. Now reads the real text viaContentPart.content_to_string/1.ChatTemplates.prep_and_validate_messages/1: every[]/malformed branchraised, so the compiler inferred the domain asnon_empty_listfrom the returning clauses only, making the intentional "raises on empty" test look type-incompatible. Lifted the empty-list case into its own function head so[]is a declared part of the signature.Changes
lib/message_delta.ex— Reorderedappend_to_merged_content/2so the merged_content ContentPart clause precedes the empty-content guard; removed unreachable{:error, _reason}clause into_message/1lib/utils/chat_templates.ex— Promoted the empty-list case ofprep_and_validate_messages/1to its own function headlib/function_param.ex— Dropped the always-true!(type == :object)sub-term invalidate_object_type/1lib/message.ex— Removed the shadowed{:ok, []}clause invalidate_content_type/1lib/message/content_part.ex— Simplified(primary.content || "")to the guard-proven binarylib/chat_models/chat_open_ai_responses.ex— Dropped deadpart.options || []fallbacklib/chat_models/chat_google_ai.ex— Removed duplicatefor_api/1clauselib/chat_models/{chat_model,chat_aws_mantle,chat_grok,chat_bumblebee}.ex,lib/{token_usage,function_param,images/generated_image,routing/prompt_route}.ex,lib/message/{tool_call,tool_result}.ex— Removed unusedrequire Loggertest/chat_models/chat_orq_test.exs— Read merged delta text frommerged_contentinstead of the always-nilcontentTesting
mix compile --force(bothdevandtestenvs) is now warning-free. Full suite green: 1804 tests pass (31 doctests), 143 excluded live tests. The clause reorder and function-head refactor were verified against the existingmessage_delta,message,function_param,content_part,chat_google_ai, andchat_templatestest files with no regressions.