lowering: matching Some/None pulls in the Option classes - #29
Merged
Conversation
`needs_option` decides whether the runtime import (project build) or the inlined Option prelude (single file) is emitted, and only *construction* sites set it. `lower_pattern`'s Pattern::Ctor arm set `needs_result` for Ok/Error but had no matching line for Some/None, so a module that merely consumed an Option emitted `case None_():` with nothing defining or importing that name: NameError at runtime, after `check` and `compile` both passed. Cross-module made it easy to hit (the producer is in the other file), but a single file that only matches -- a function whose Option comes in as a parameter -- emitted the same broken output. Fixes #25
simontreanor
force-pushed
the
fix/option-import-on-match
branch
from
July 30, 2026 15:30
05fa6ce to
a4357aa
Compare
Merged
simontreanor
added a commit
that referenced
this pull request
Jul 31, 2026
Two user-visible additions since 0.2.0 -- the `input` prelude builtin
(`string ->{io} string`, #19) and caller-supplied `extern` keyword slots
(`kw = ...`, #21) -- plus five fixes, four of them from the first program
written in Pyfun in anger:
* imported schemes reused the exporting module's type-variable ids, silently
blocking generalization in the importing module and cascading to its
consumers (#26 / #27)
* a recursive function of arity >= 2 could not perform any effect -- the shape
of every loop in a language with no `while` (#24 / #28)
* a module that only matched an imported `Option` emitted `None_` without
importing it (#25 / #29)
* user identifiers collided with Python keywords, builtins the emitter calls,
and modules an `extern` imports (#23 / #30)
* the local-binder module-alias shadowing residual (#18)
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.
Fixes #25.
needs_optiondecides whether the runtime import (project build) or the inlinedOptionprelude (single file) is emitted, and only construction sites set it.lower_pattern''sPattern::Ctorarm setsneeds_resultforOk/Errorbut had no matching line forSome/None, so a module that merely consumed anOptionemittedcase None_():with nothing defining or importing that name —NameErrorat runtime, aftercheckandcompileboth passed.Cross-module is where this is easy to hit, because the producer lives in the other file:
But it was never really about modules. A single file that only matches — a function whose
Optionarrives as a parameter — emitted the same broken output onmain:Fix
Set
needs_optionforSome/Nonein the constructor-pattern arm, mirroring theOk/Errorline directly above it.Tests
tests/compile.rs::matching_an_option_pulls_in_the_option_prelude— the single-file case.tests/project.rs::a_module_that_only_matches_an_imported_option_imports_it— the cross-module case: asserts thefrom _pyfun_rt import Some, None_header and runs the program end to end.Both verified to fail on
main.cargo testgreen,cargo clippy --all-targetsclean,cargo fmtapplied.