From 4cc6624cb009f1062da24696843d6ce15a3aaef5 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 7 Apr 2026 14:37:54 -1000 Subject: [PATCH] [core] Emit deprecation warning on TemplatableFn return type mismatch --- esphome/core/automation.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/esphome/core/automation.h b/esphome/core/automation.h index fbe7224b70..355968b284 100644 --- a/esphome/core/automation.h +++ b/esphome/core/automation.h @@ -47,8 +47,10 @@ template class TemplatableFn { // Convertible return type (e.g., int -> uint8_t) — casting trampoline. // Stateless lambdas are default-constructible in C++20, so F{} recreates the lambda inside // the trampoline without capturing. This compiles to the same code as a direct call + cast. + // Deprecated: codegen should use the correct output type to avoid the trampoline. template - TemplatableFn(F) requires(!std::convertible_to) && + [[deprecated("Lambda return type does not match TemplatableFn — use the correct type in " + "codegen")]] TemplatableFn(F) requires(!std::convertible_to) && std::invocable &&std::convertible_to, T> &&std::is_empty_v &&std::default_initializable : f_([](X... x) -> T { return static_cast(F{}(x...)); }) {}