{"record":{"id":"788392e4ce870197","repo":"PyO3/pyo3","slug":"cannot-fail-due-to-mask","errorCode":null,"errorMessage":"cannot fail due to mask","messagePattern":"cannot fail due to mask","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pyo3-ffi/src/cpython/abstract_.rs","lineNumber":56,"sourceCode":"        callable: *mut PyObject,\n        args: *const *mut PyObject,\n        nargs: Py_ssize_t,\n        keywords: *mut PyObject,\n    ) -> *mut PyObject;\n}\n\n#[cfg(not(Py_3_12))]\nconst PY_VECTORCALL_ARGUMENTS_OFFSET: size_t = (1 as size_t)\n    .checked_shl((8 * core::mem::size_of::<size_t>() - 1) as u32)\n    .expect(\"size_t should fit the flag bits\");\n\n#[cfg(Py_3_12)] // public API from 3.12\nuse crate::PY_VECTORCALL_ARGUMENTS_OFFSET;\n\n#[inline(always)]\npub unsafe fn PyVectorcall_NARGS(n: size_t) -> Py_ssize_t {\n    let n = n & !PY_VECTORCALL_ARGUMENTS_OFFSET;\n    n.try_into().expect(\"cannot fail due to mask\")\n}\n\n#[cfg(any(PyPy, Py_3_11))]\nextern_libpython! {\n    #[cfg_attr(PyPy, link_name = \"PyPyVectorcall_Function\")]\n    pub fn PyVectorcall_Function(callable: *mut PyObject) -> Option<vectorcallfunc>;\n}\n\n#[cfg(not(any(PyPy, Py_3_11)))]\n#[inline(always)]\npub unsafe fn PyVectorcall_Function(callable: *mut PyObject) -> Option<vectorcallfunc> {\n    assert!(!callable.is_null());\n    let tp = crate::Py_TYPE(callable);\n    if PyType_HasFeature(tp, Py_TPFLAGS_HAVE_VECTORCALL) == 0 {\n        return None;\n    }\n    assert!(PyCallable_Check(callable) > 0);\n    let offset = (*tp).tp_vectorcall_offset;","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/PyO3/pyo3/blob/ac9b6899d348be4d54614d060dea53a645a12e36/pyo3-ffi/src/cpython/abstract_.rs#L38-L74","documentation":"PyVectorcall_NARGS masks off the PY_VECTORCALL_ARGUMENTS_OFFSET flag bit from a vectorcall nargsf value and converts the result to Py_ssize_t. Since the mask guarantees the value fits in Py_ssize_t (the flag is the top bit and the remainder fits ssize_t), the try_into cannot fail; the expect is an internal invariant assertion.","triggerScenarios":"Practically unreachable: it would require a size_t/Py_ssize_t width relationship where a masked vectorcall argument count doesn't fit Py_ssize_t — not possible on supported CPython builds.","commonSituations":"Seen in panic backtraces only if a FFI caller passes a corrupted nargsf on a hypothetical platform with mismatched size_t/ssize_t widths.","solutions":["Ensure the size_t value passed in is a genuine nargsf from a vectorcall, not arbitrary data","Use a supported platform/toolchain","Audit FFI boundaries for corrupted argument values"],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":"// validate nargsf looks like a vectorcall count before passing on\nfn valid_nargsf(n: usize) -> bool { n & !PY_VECTORCALL_ARGUMENTS_OFFSET <= isize::MAX as usize }","tryCatchPattern":null,"preventionTips":["Only pass genuine CPython nargsf values into PyVectorcall_NARGS","Don't fabricate size_t values at FFI boundaries","Use supported CPython versions"],"tags":["rust","pyo3-ffi","invariant","vectorcall"],"backgroundTag":"internal-invariant-panic","analyzedSha":"ac9b6899d348be4d54614d060dea53a645a12e36","analyzedAt":"2026-09-05T09:20:35.319Z","contentChangedAt":"2026-09-05T09:20:35.319Z","schemaVersion":2},"datasetVersion":"2026-09-12T12:17:11.808Z"}