Skip to content
Open

--- #1504

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
32 changes: 7 additions & 25 deletions checker/src/main/java/dev/cel/checker/Env.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ public class Env {
*/
private final ArrayList<DeclGroup> decls = new ArrayList<>();

private int comprehensionScopeCount = 0;

/** A map from expression ids into references resolved for the overall tree so far. */
private final Map<Long, CelReference> referenceMap = new LinkedHashMap<>();

Expand Down Expand Up @@ -294,18 +292,6 @@ public void exitScope() {
decls.remove(decls.size() - 1);
}

/** Enters a scope for comprehension variables, which leading-dot identifiers skip. */
void enterComprehensionScope() {
enterScope();
comprehensionScopeCount++;
}

/** Exits a scope entered with {@link #enterComprehensionScope}. */
void exitComprehensionScope() {
exitScope();
comprehensionScopeCount--;
}

/** Return the current scope depth for the environment. */
public int scopeDepth() {
return decls.size() - 1;
Expand Down Expand Up @@ -515,7 +501,7 @@ public Env add(String name, Type type) {
}

for (String cand : container.resolveCandidateNames(name)) {
CelVarDecl decl = tryLookupCelIdent(cand, name.startsWith("."));
CelVarDecl decl = tryLookupCelIdent(cand);
if (decl != null) {
return decl;
}
Expand All @@ -524,9 +510,9 @@ public Env add(String name, Type type) {
return null;
}

private @Nullable CelVarDecl tryLookupCelIdent(String cand, boolean skipComprehensionScopes) {
private @Nullable CelVarDecl tryLookupCelIdent(String cand) {
// First determine whether we know this name already.
CelVarDecl decl = findIdentDecl(cand, skipComprehensionScopes);
CelVarDecl decl = findIdentDecl(cand);
if (decl != null) {
return decl;
}
Expand Down Expand Up @@ -764,14 +750,10 @@ private void addOverload(CelFunctionDecl.Builder builder, CelOverloadDecl overlo
builder.addOverloads(overload);
}

/**
* Search for the named identifier declaration, optionally skipping the comprehension scopes on
* top of the stack.
*/
private @Nullable CelVarDecl findIdentDecl(String name, boolean skipComprehensionScopes) {
int topScope = decls.size() - 1 - (skipComprehensionScopes ? comprehensionScopeCount : 0);
for (int i = topScope; i >= 0; i--) {
CelVarDecl ident = decls.get(i).getIdent(name);
/** Search for the named identifier declaration. */
private @Nullable CelVarDecl findIdentDecl(String name) {
for (DeclGroup declGroup : Lists.reverse(decls)) {
CelVarDecl ident = declGroup.getIdent(name);
if (ident != null) {
return ident;
}
Expand Down
8 changes: 4 additions & 4 deletions checker/src/main/java/dev/cel/checker/ExprChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -535,10 +535,10 @@ private void visit(CelMutableExpr expr, CelMutableComprehension compre) {
}

// Declare accumulation variable on outer scope.
env.enterComprehensionScope();
env.enterScope();
env.add(CelVarDecl.newVarDeclaration(compre.accuVar(), accuType));
// Declare iteration variable on inner scope.
env.enterComprehensionScope();
env.enterScope();
env.add(CelVarDecl.newVarDeclaration(compre.iterVar(), varType));
if (!Strings.isNullOrEmpty(compre.iterVar2())) {
env.add(CelVarDecl.newVarDeclaration(compre.iterVar2(), varType2));
Expand All @@ -548,9 +548,9 @@ private void visit(CelMutableExpr expr, CelMutableComprehension compre) {
visit(compre.loopStep());
assertType(compre.loopStep(), accuType);
// Forget iteration variable, as result expression must only depend on accu.
env.exitComprehensionScope();
env.exitScope();
visit(compre.result());
env.exitComprehensionScope();
env.exitScope();

env.setType(expr, inferenceContext.specialize(env.getType(compre.result())));
}
Expand Down
29 changes: 0 additions & 29 deletions checker/src/test/java/dev/cel/checker/ExprCheckerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,11 @@
import dev.cel.common.CelMutableAst;
import dev.cel.common.CelProtoAbstractSyntaxTree;
import dev.cel.common.CelProtoDeclConverter;
import dev.cel.common.CelVarDecl;
import dev.cel.common.ast.CelConstant;
import dev.cel.common.internal.EnvVisitable;
import dev.cel.common.internal.EnvVisitor;
import dev.cel.common.internal.Errors;
import dev.cel.common.types.CelType;
import dev.cel.common.types.CelTypeProvider;
import dev.cel.common.types.ListType;
import dev.cel.common.types.MapType;
import dev.cel.common.types.NullableType;
Expand Down Expand Up @@ -229,33 +227,6 @@ public void referenceTypeAbsolute() throws Exception {
declareVariable("app.config", SimpleType.INT);
source = "[0].exists(app, .app.config == 1)";
runTest();

source = "[1].all(int, type(int) == .int)";
runTest();
}

@Test
@SuppressWarnings("deprecation") // ExprChecker is used directly to add a non-comprehension scope.
public void referenceAbsolute_declInNonComprehensionScope() throws Exception {
source = "[1].all(x, .x == 'a') && .x == 'a'";
CelTypeProvider typeProvider = ProtoMessageTypeProvider.newBuilder().build();
prepareCompiler(typeProvider);
Errors errors = new Errors("<input>", source);
Env env = Env.standard(errors, typeProvider, null, TEST_OPTIONS);
env.enterScope();
env.add(CelVarDecl.newVarDeclaration("x", SimpleType.STRING));

CelAbstractSyntaxTree ast =
ExprChecker.typecheck(
env, container, celCompiler.parse(source).getAst(), Optional.absent());

testOutput().println(errors.getAllErrorsAsString());
testOutput()
.println(
CelDebug.toAdornedDebugString(
CelProtoAbstractSyntaxTree.fromCelAst(ast).getExpr(),
new CheckedExprAdorner(
CelProtoAbstractSyntaxTree.fromCelAst(ast).toCheckedExpr())));
}

@Test
Expand Down

This file was deleted.

33 changes: 0 additions & 33 deletions checker/src/test/resources/referenceTypeAbsolute.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -34,36 +34,3 @@ __comprehension__(
)~bool^logical_or,
// Result
@result~bool^@result)~bool

Source: [1].all(int, type(int) == .int)
declare app.config {
value int
}
=====>
__comprehension__(
// Variable
int,
// Target
[
1~int
]~list(int),
// Accumulator
@result,
// Init
true~bool,
// LoopCondition
@not_strictly_false(
@result~bool^@result
)~bool^not_strictly_false,
// LoopStep
_&&_(
@result~bool^@result,
_==_(
type(
int~int^int
)~type(int)^type,
.int~type(int)^.int
)~bool^equals
)~bool^logical_and,
// Result
@result~bool^@result)~bool
Loading