Skip to content

Sema: any fn type is compatible with any other fn type (params and result never compared) — garbage results, invalid MIR #1772

Closed

Description

@ehartford

Sema.types_compatible accepts any fn type for any other fn type: for TY_FN against TY_FN both types_compatible_fast and types_compatible return callable_unsafe_coercion_ok — only unsafe-ness is compared, never the parameters or the result. (TY_EXTERN_FN does compare them, through fn_types_compatible.)

Repros (stage1 of main 09a0efb)

fn add1(x: i32) -> i32: x + 1
fn apply64(f: fn(i32) -> i64, v: i32) -> i64: f(v)
fn main:
    print(apply64(add1, -10))

Prints 4294967287: the i32 result is read as an i64.

fn add1(x: i32) -> i32: x + 1
fn main:
    let f: fn(i32) -> str = add1
    print(f(1))
error: invalid MIR before codegen: use rvalue type is incompatible with assign destination (dest ty=399 kind=12, src ty=983 kind=12)

Sema accepted it; MIR's validator caught what Sema should have (D65: post-Sema invalid MIR is a compiler bug).

Where

src/Sema.w, types_compatible_fast (if exp_k == TypeKind.TY_FN and act_k == TypeKind.TY_FN: return self.callable_unsafe_coercion_ok(...)) and the same branch in types_compatible.

Note

Comparing fn types structurally everywhere may reject code that leans on the hole today (closures typed against inferred signatures, generic callables); it needs a sweep. The #1508 fix (a closure's declared -> T) checks the closure's declared result against the result its context expects itself (Sema.types_identical), so that path does not reach this hole.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions