The invariants of ReturnOp are directly tied to FuncOp, making ReturnOp invalid in any other context.
PiperOrigin-RevId:
258421200
/// Return the operation that this refers to.
Operation *getOperation() { return state; }
- /// Return the closes surrounding parent operation that is of type 'OpTy'.
+ /// Returns the closest surrounding operation that contains this operation
+ /// or nullptr if this is a top-level operation.
+ Operation *getParentOp() { return getOperation()->getParentOp(); }
+
+ /// Return the closest surrounding parent operation that is of type 'OpTy'.
template <typename OpTy> OpTy getParentOfType() {
return getOperation()->getParentOfType<OpTy>();
}
}
static LogicalResult verify(ReturnOp op) {
- // TODO(b/137008268): Return op should verify that it is nested directly
- // within a function operation.
- auto function = op.getParentOfType<FuncOp>();
+ auto function = dyn_cast_or_null<FuncOp>(op.getParentOp());
+ if (!function)
+ return op.emitOpError() << "must be nested within a 'func' region";
// The operand number and types must match the function signature.
const auto &results = function.getType().getResults();
}, {}): (i1) -> ()
return
}
+
+// -----
+
+func @return_not_in_function() {
+ "foo.region"() ({
+ // expected-error@+1 {{must be nested within a 'func' region}}
+ return
+ }): () -> ()
+ return
+}