Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/main/java/dev/trufflecel/EvalContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,16 @@ Object attributeRead(Attribute path, boolean presence) {
: evaluation.partial.override(path, presence);
}

/**
* Overlap-based unknown fallback for SELECT/INDEX hops; see {@link
* PartialState#unknownIntersect}.
*/
Object attributeIntersect(Attribute path) {
return evaluation == null || evaluation.partial == null
? PartialState.NONE
: evaluation.partial.unknownIntersect(path);
}

@com.oracle.truffle.api.CompilerDirectives.TruffleBoundary
static Attribute qualify(Attribute parent, Object qualifier) {
if (parent == null) return null;
Expand Down
54 changes: 47 additions & 7 deletions src/main/java/dev/trufflecel/ExprNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,16 @@
&& (Boolean.TRUE.equals(value) || a instanceof OptionalValue)
? OptionalValue.of(override)
: override;
if (a == PartialState.MISSING) throw missingAttribute(parent);
if (a == PartialState.MISSING) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reference equality is intentional here: PartialState.MISSING/NONE are identity-only static final Object sentinels, so .equals() would be a no-op at best and NPE-prone at worst (the receiver can be null, e.g. when an earlier read threw and was captured). This matches the existing convention throughout ExprNode (e.g. override != PartialState.NONE on the line above). Not changing.

// Mirror the SELECT hop: a deep unknown pattern intersecting this read path produces an
// Unknown instead of a hard UNDECLARED error so resolve-and-retry can make progress.
// `attributeIntersect` only ever returns NONE or an Unknown, so the optional-lifting form
// used for `override` above is not needed here.
Object intersect = ctx.attributeIntersect(path);
if (intersect != PartialState.NONE) yield intersect;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reference equality is intentional here: PartialState.NONE is an identity-only static final Object sentinel, and per the comment on the preceding lines ctx.attributeIntersect(path) only ever returns NONE or an Unknown, so .equals() would be a no-op at best. This matches the existing convention throughout ExprNode (e.g. intersect != PartialState.NONE on the line above and the same pattern at the other call sites). Not changing.

if (b instanceof Unknown u) yield u;
throw missingAttribute(parent);
}
if (a instanceof Unknown u) yield b instanceof Unknown v ? u.merge(v) : u;
if (b instanceof Unknown u) yield u;
if (error != null) throw error;
Expand All @@ -706,6 +715,15 @@
if (optional
&& (e.code() == CelException.Code.NO_SUCH_KEY || e.code() == CelException.Code.INDEX))
yield OptionalValue.none();
if (!optional
&& (e.code() == CelException.Code.NO_SUCH_KEY
|| e.code() == CelException.Code.INDEX)) {
// Container present but the key/index is missing: a deep unknown pattern intersecting
// this read path turns the hard error into an Unknown for resolve-and-retry; a
// resolution that supplies (or has supplied) an ancestor still raises the error.
Object intersect = ctx.attributeIntersect(path);
if (intersect != PartialState.NONE) yield intersect;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reference equality is intentional here: PartialState.MISSING/NONE are identity-only static final Object sentinels, so .equals() would be a no-op at best and NPE-prone at worst (the receiver can be null, e.g. when an earlier read threw and was captured). This matches the existing convention throughout ExprNode (e.g. override != PartialState.NONE on the line above). Not changing.

}
throw e;
}
}
Expand Down Expand Up @@ -749,18 +767,29 @@
private Object select(VirtualFrame frame, EvalContext ctx, boolean presence) {
Object operand = children[0].execute(frame, ctx);
Attribute parent = ctx.attribute();
Object override = ctx.attributeRead(ctx.qualify(parent, name), presence);
Attribute path = ctx.qualify(parent, name);
Object override = ctx.attributeRead(path, presence);
if (presence) ctx.attribute(null);
if (override != PartialState.NONE)
return !presence
&& !(override instanceof Unknown)
&& (Boolean.TRUE.equals(value) || operand instanceof OptionalValue)
? OptionalValue.of(override)
: override;
if (operand == PartialState.MISSING) throw missingAttribute(parent);
if (operand == PartialState.MISSING) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reference equality is intentional here: PartialState.MISSING/NONE are identity-only static final Object sentinels, so .equals() would be a no-op at best and NPE-prone at worst (the receiver can be null, e.g. when an earlier read threw and was captured). This matches the existing convention throughout ExprNode (e.g. override != PartialState.NONE on the line above). Not changing.

// A deep unknown pattern (deeper than this hop) was invisible to `override` because
// AttributePattern.matches is one-directional; fall back to overlap semantics so that a
// missing receiver at this hop yields an Unknown instead of a hard UNDECLARED error,
// mirroring cel-go / cel-java. A resolution that has supplied an ancestor is left to
// surface the existing error. `attributeIntersect` only ever returns NONE or an Unknown,
// so the optional-lifting form used for `override` above is not needed here.
Object intersect = ctx.attributeIntersect(path);
if (intersect != PartialState.NONE) return intersect;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reference equality is intentional here: PartialState.NONE is an identity-only static final Object sentinel, and per the comment on the preceding lines ctx.attributeIntersect(path) only ever returns NONE or an Unknown, so .equals() would be a no-op at best. This matches the existing convention throughout ExprNode (e.g. intersect != PartialState.NONE on the line above and the same pattern at the other call sites). Not changing.

throw missingAttribute(parent);
}
if (fieldRead != null && fieldRead.accepts(operand))
return presence ? fieldRead.has(operand) : fieldRead.read(operand);
return selectValue(ctx, presence, operand);
return selectValue(ctx, presence, operand, path);
}

@com.oracle.truffle.api.CompilerDirectives.TruffleBoundary
Expand All @@ -769,7 +798,7 @@
}

@com.oracle.truffle.api.CompilerDirectives.TruffleBoundary
private Object selectValue(EvalContext ctx, boolean presence, Object operand) {
private Object selectValue(EvalContext ctx, boolean presence, Object operand, Attribute path) {
if (operand instanceof Unknown) return operand;
if (extensionSelect) {
for (ObjectType type : ctx.environment.runtimeObjectTypes)
Expand All @@ -790,13 +819,24 @@
boolean present = map.containsKey(name);
if (presence) return present;
if (lifted) return present ? OptionalValue.of(map.get(name)) : OptionalValue.none();
if (!present) throw Values.error(CelException.Code.NO_SUCH_KEY, "no such key: " + name);
if (!present) {
// The container is present but the key is missing: a deep unknown pattern intersecting
// this read path means the missing key is part of the unknown attribute subtree, so
// surface an Unknown for resolve-and-retry instead of a hard NO_SUCH_KEY; a resolution
// that supplies (or has supplied) an ancestor steers the existing error path.
Object intersect = ctx.attributeIntersect(path);
if (intersect != PartialState.NONE) return intersect;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reference equality is intentional here: PartialState.MISSING/NONE are identity-only static final Object sentinels, so .equals() would be a no-op at best and NPE-prone at worst (the receiver can be null, e.g. when an earlier read threw and was captured). This matches the existing convention throughout ExprNode (e.g. override != PartialState.NONE on the line above). Not changing.

throw Values.error(CelException.Code.NO_SUCH_KEY, "no such key: " + name);
}
return map.get(name);
}
for (ObjectType type : ctx.environment.runtimeObjectTypes)
if (type.accepts(operand)) {
if (!type.fields().containsKey(name))
if (!type.fields().containsKey(name)) {
Object intersect = ctx.attributeIntersect(path);
if (intersect != PartialState.NONE) return intersect;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reference equality is intentional here: PartialState.MISSING/NONE are identity-only static final Object sentinels, so .equals() would be a no-op at best and NPE-prone at worst (the receiver can be null, e.g. when an earlier read threw and was captured). This matches the existing convention throughout ExprNode (e.g. override != PartialState.NONE on the line above). Not changing.

throw Values.error(CelException.Code.NO_SUCH_FIELD, "no such field: " + name);
}
if (presence) return type.has(operand, name);
if (lifted)
return type.has(operand, name)
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/dev/trufflecel/PartialState.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,38 @@ Object override(Attribute path, boolean presence) {
return unknown == null ? NONE : unknown;
}

/**
* Overlap-based fallback used at SELECT/INDEX hops when {@link #override} did not match a
* shallower-or-equal pattern. Returns an {@link Unknown} rooted at the longest common prefix of
* the hop path and each intersecting pattern (i.e. at the hop path when the pattern is deeper,
* and at the pattern granularity when the pattern is shallower-or-equal, mirroring {@code
* override}), so that deep unknown patterns with missing ancestors surface an {@code Unknown} for
* the documented resolve-and-retry loop instead of a hard {@code UNDECLARED} / {@code
* NO_SUCH_KEY} / {@code NO_SUCH_FIELD} error. A resolution that supplies (or has supplied) the
* receiver is left to steer the normal selector read; the caller falls back to its existing error
* path. This matches the bidirectional overlap semantics already used by {@link #consume} and
* {@link #containsUnknown}, and mirrors cel-go / cel-java where the pattern check runs against
* the resolved attribute path before the activation is consulted.
*/
@TruffleBoundary
Object unknownIntersect(Attribute path) {
if (path == null) return NONE;
// A resolution that is an ancestor of (or equal to) the path has already supplied the
// receiver to its selector; surface NO_SUCH_KEY / NO_SUCH_FIELD rather than masking the
// missing descendant with an Unknown.
for (Attribute candidate : activation.resolutions.keySet())
if (candidate.prefixOf(path)) return NONE;
Unknown unknown = null;
for (AttributePattern pattern : activation.patterns)
if (pattern.intersects(path)) {
int common = Math.min(pattern.qualifiers().size(), path.qualifiers().size());
var root = new Attribute(path.variable(), path.qualifiers().subList(0, common));
Unknown next = Unknown.of(root.toString());
unknown = unknown == null ? next : unknown.merge(next);
}
return unknown == null ? NONE : unknown;
}

@TruffleBoundary
Object consume(Object value) {
Attribute path = attribute;
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/dev/trufflecel/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -685,3 +685,14 @@ java_test(
"@maven//:junit_junit",
],
)

java_test(
name = "deepPatternRepro",
srcs = ["DeepPatternReproTest.java"],
test_class = "dev.trufflecel.DeepPatternReproTest",
deps = [
":test_support",
"//:trufflecel",
"@maven//:junit_junit",
],
)
Loading