diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/EChain.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/EChain.java index 4cfb35e48a8..463c9a5a4ff 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/EChain.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/EChain.java @@ -242,6 +242,11 @@ public final class EChain extends AExpression { private void analyzeRead() { final ALink last = links.get(links.size() - 1); + // If the load node is a DEF node, we adapt its after type to use _this_ expected output type: + if (last instanceof ADefLink && this.expected != null) { + last.after = this.expected; + } + constant = last.string; statement = last.statement; actual = last.after;