Skip to content

Fix Elixir 1.20 type warnings: dead code and latent bugs#561

Merged
brainlid merged 1 commit into
mainfrom
me-elixir-1.20-rc.6-type-fixes
Jun 9, 2026
Merged

Fix Elixir 1.20 type warnings: dead code and latent bugs#561
brainlid merged 1 commit into
mainfrom
me-elixir-1.20-rc.6-type-fixes

Conversation

@brainlid

@brainlid brainlid commented Jun 9, 2026

Copy link
Copy Markdown
Owner

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 --force surfaced 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 Logger directives, a byte-identical duplicate for_api/1 clause in ChatGoogleAI, an unreachable {:error, _reason} catch-all (Message.new/1 only ever returns changeset errors), an always-true type == :object sub-term inside a cond whose earlier clauses already eliminated :object, and two || default fallbacks whose left side was type-proven non-nil.

  • Latent bugs — fixed to honor the original intent:

    • MessageDelta.append_to_merged_content/2: the content in [nil, []] "nothing to merge" guard shadowed a later clause meant to merge an already-processed merged_content ContentPart, silently dropping it. Reordered the specific clause ahead of the broad guard.
    • ChatOrqTest: the streaming test read merged.content, which merge_delta/2 always clears to nil (text lives in merged_content). The assertion only ever passed on its "" fallback. Now reads the real text via ContentPart.content_to_string/1.
    • ChatTemplates.prep_and_validate_messages/1: every []/malformed branch raised, so the compiler inferred the domain as non_empty_list from 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 — Reordered append_to_merged_content/2 so the merged_content ContentPart clause precedes the empty-content guard; removed unreachable {:error, _reason} clause in to_message/1
  • lib/utils/chat_templates.ex — Promoted the empty-list case of prep_and_validate_messages/1 to its own function head
  • lib/function_param.ex — Dropped the always-true !(type == :object) sub-term in validate_object_type/1
  • lib/message.ex — Removed the shadowed {:ok, []} clause in validate_content_type/1
  • lib/message/content_part.ex — Simplified (primary.content || "") to the guard-proven binary
  • lib/chat_models/chat_open_ai_responses.ex — Dropped dead part.options || [] fallback
  • lib/chat_models/chat_google_ai.ex — Removed duplicate for_api/1 clause
  • lib/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 unused require Logger
  • test/chat_models/chat_orq_test.exs — Read merged delta text from merged_content instead of the always-nil content

Testing

mix compile --force (both dev and test envs) 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 existing message_delta, message, function_param, content_part, chat_google_ai, and chat_templates test files with no regressions.

- fixes for dead code
- fixed true latent bugs
@brainlid brainlid merged commit 054b018 into main Jun 9, 2026
2 checks passed
@brainlid brainlid deleted the me-elixir-1.20-rc.6-type-fixes branch June 9, 2026 04:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant