checker,gen: fix generic type inference from a callback with Result return type#27679
Open
rilaaax wants to merge 1 commit into
Open
checker,gen: fix generic type inference from a callback with Result return type#27679rilaaax wants to merge 1 commit into
rilaaax wants to merge 1 commit into
Conversation
80a5506 to
36250cf
Compare
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 #27108
Problem
fails with a C compilation error:
The issue is specific to the combination of: a generic parameter that is itself a callback (
fn () !T), where the callback's return type carries the generic marker inside a Result (!T).Note: the wrong inference happens unconditionally whenever a generic function takes a callback parameter typed
fn () !T— it isn't specific to usingT{}in the body. It only surfaces as a visible compile error when the body does something that needs T's plain (unwrapped) C representation, like zero-initializingT{}; without that, the mismatch can go unnoticed since the code still happens to work despite T being wrong internally.Cause
V infers
There by structurally comparing the parameter's function type against the argument's actual function type. Two separate places do this comparison (once in the checker, once in a duplicate implementation in cgen), and in both, the branch that extracts the concrete type from the return position took the argument's return type as-is, without stripping its.resultflag first. So for a closure returning!int,Tgot inferred as!int(the Result-wrapped type) instead ofint. This produced two inconsistent monomorphized versions of the generic function — one correctly named/typed forT=int, and a second, wrongly named forT=!int, which is the one actually invoked at the call site, leading to invalid C.Fix
Strip the option/result flags before binding the inferred type, in both places, using the existing
clear_option_and_result()helper (already used a few lines above for the analogous case where the parameter itself, rather than just its return type, is Result-wrapped).Test
Regression test added.
NB: credits to Claude