Skip to content

An unsafe fn used as a value is callable without unsafe #1829

Closed

Description

@ehartford

Spec §16.11: an unsafe fn may not coerce to a safe callable type. The coercion check (check_ident, SemaCheck.w) runs only when an extern "C" fn type is expected; an unannotated binding takes the fn's own TY_FN type, and a call through it needs no unsafe:

unsafe fn danger(x: i32) -> i32: x + 1
fn main:
    let f = danger
    print(f(1))

Expected: an error (the value keeps its unsafety, or the call through it requires unsafe). Actual (with check, v0.15.2.1 and the D75 branch): ok.

Found while implementing D75 variadic definitions (#1678). A ... definition is refused as a value outright there (it has no variadic function-pointer type), so it does not go through this hole; every other unsafe fn does.

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