-
Notifications
You must be signed in to change notification settings - Fork 14
Description
There is a blanket implementation From<T> for OpType
for all T: MakeRegisteredOp
. This implementation hides panics that can occur when the T
is not valid. Because these are panics on invalid inputs to the API and not internal errors, this is a bug in the API design.
Ideally the API should acknowledge that the conversion can fail by implementing TryFrom
instead. If TryFrom
is too inconvenient, the panic should include a helpful message and not simply unwrap an Option
. Moreover the fact that this API can panic (and the cases in which it does) should be documented.
When does it panic?
The panic can happen in <OpType as From<T>>::from
when unwrapping the Option
returned by <T as MakeRegisteredOp>::to_extension_op
. The default implementation of MakeRegisteredOp::to_extension_op
uses the blanket implementation From<T> for RegisteredOp
for all T: MakeRegisteredOp
and then calls <RegisteredOp as MakeRegisteredOp>::to_extension_op
. This method has three ways in which a None
can be returned:
- The
Weak<Extension>
is upgraded. Since it is obtained from<T as MakeRegisteredOp>::extension_ref
it could be considered an internal error, since valid implementations ofMakeRegisteredOp
should never return extension references that can't be upgraded. However, this requirement is not documented inMakeRegisteredOp::extension_ref
. Moreover, the upgrade failing should panic with a message that indicates that this is happened. Extension::get_op
might fail given the operation id reported by<T as MakeExtensionOp>::op_id
. Analogous to the previous case, this indicates an erroneous implementation and therefore should at least be indicated by a panic with an explicit message.- Finally,
ExtensionOp::new
can return aSignatureError
in case that the type arguments passed to the operation are invalid. The signature error is dropped by converting theResult
to anOption
. This is a case of invalid user input: I've seen this fail intket2
when constructing an invalidWasmOp
. This error should be propagated.