Repository navigation
Share the standard declarations scope across type checks #1512
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
35be08c
74bae63
566dbe8
7f74c60
9ace93d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -204,12 +204,45 @@ static Env standard( | |
| CelTypeProvider celTypeProvider, | ||
| @Nullable TypeProvider legacyTypeProvider, | ||
| CelOptions celOptions) { | ||
| Env env = Env.unconfigured(errors, celTypeProvider, legacyTypeProvider, celOptions); | ||
| // Isolate the standard declarations into their own scope for forward compatibility. | ||
| celStandardDeclaration.functionDecls().forEach(env::add); | ||
| celStandardDeclaration.identifierDecls().forEach(env::add); | ||
| return withDeclarations( | ||
| celStandardDeclaration, | ||
| ImmutableMap.of(), | ||
| ImmutableMap.of(), | ||
| errors, | ||
| celTypeProvider, | ||
| legacyTypeProvider, | ||
| celOptions); | ||
| } | ||
|
|
||
| env.enterScope(); | ||
| /** | ||
| * Creates an {@code Env} value with the standard declarations, when provided, and the given | ||
| * declarations, as previously added to an {@code Env} created the same way and read back from its | ||
| * top-most scope. | ||
| */ | ||
| static Env withDeclarations( | ||
| @Nullable CelStandardDeclarations celStandardDeclarations, | ||
| ImmutableMap<String, CelVarDecl> idents, | ||
| ImmutableMap<String, CelFunctionDecl> functions, | ||
| Errors errors, | ||
| CelTypeProvider celTypeProvider, | ||
| @Nullable TypeProvider legacyTypeProvider, | ||
| CelOptions celOptions) { | ||
| DeclGroup declGroup = new DeclGroup(idents, functions, /* isComprehensionScope= */ false); | ||
| if (celStandardDeclarations == null) { | ||
| return new Env(errors, celTypeProvider, legacyTypeProvider, declGroup, celOptions); | ||
| } | ||
| Env env = | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would do the setup in reverse -- Make |
||
| new Env( | ||
| errors, | ||
| celTypeProvider, | ||
| legacyTypeProvider, | ||
| new DeclGroup( | ||
| celStandardDeclarations.identifierDeclsByName(), | ||
| celStandardDeclarations.functionDeclsByName(), | ||
| /* isComprehensionScope= */ false), | ||
| celOptions); | ||
| // Isolate the standard declarations into their own scope for forward compatibility. | ||
| env.decls.add(declGroup); | ||
| return env; | ||
| } | ||
|
|
||
|
|
@@ -222,7 +255,11 @@ public static Env standard( | |
| return standard(celStandardDeclaration, errors, EMPTY_TYPE_PROVIDER, typeProvider, celOptions); | ||
| } | ||
|
|
||
| private static CelStandardDeclarations newStandardDeclarations(CelOptions celOptions) { | ||
| static CelStandardDeclarations newStandardDeclarations(CelOptions celOptions) { | ||
| // Nothing is filtered out with these options. | ||
| if (celOptions.enableUnsignedLongs() && celOptions.enableTimestampEpoch()) { | ||
| return CelStandardDeclarations.all(); | ||
| } | ||
| return CelStandardDeclarations.newBuilder() | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a short-circuit here to avoid filtering through all declarations (Skips enumerating 100+ idents/functions): if (celOptions.enableUnsignedLongs() && celOptions.enableTimestampEpoch()) {
return CelStandardDeclarations.ALL; // Prebuild this as a constant in advance
} |
||
| .filterFunctions( | ||
| (function, overload) -> { | ||
|
|
@@ -968,8 +1005,9 @@ public Decl build() { | |
| */ | ||
| public static class DeclGroup { | ||
|
|
||
| private final Map<String, CelVarDecl> idents; | ||
| private final Map<String, CelFunctionDecl> functions; | ||
| // Copied into mutable maps on first write, so immutable maps can be shared between groups. | ||
| private Map<String, CelVarDecl> idents; | ||
| private Map<String, CelFunctionDecl> functions; | ||
| private final boolean isComprehensionScope; | ||
|
|
||
| /** | ||
|
|
@@ -991,6 +1029,9 @@ public ImmutableMap<String, CelFunctionDecl> getFunctions() { | |
|
|
||
| /** Put an identifier declaration into the {@code DeclGroup}. */ | ||
| public void putIdent(CelVarDecl ident) { | ||
| if (idents instanceof ImmutableMap) { | ||
| idents = new HashMap<>(idents); | ||
| } | ||
| idents.put(ident.name(), ident); | ||
| } | ||
|
|
||
|
|
@@ -1001,12 +1042,22 @@ public void putIdent(CelVarDecl ident) { | |
|
|
||
| /** Put a function declaration into the {@code DeclGroup}. */ | ||
| public void putFunction(CelFunctionDecl function) { | ||
| if (functions instanceof ImmutableMap) { | ||
| functions = new HashMap<>(functions); | ||
| } | ||
| functions.put(function.name(), function); | ||
| } | ||
|
|
||
| private DeclGroup(boolean isComprehensionScope) { | ||
| this.idents = new HashMap<>(); | ||
| this.functions = new HashMap<>(); | ||
| this(ImmutableMap.of(), ImmutableMap.of(), isComprehensionScope); | ||
| } | ||
|
|
||
| private DeclGroup( | ||
| Map<String, CelVarDecl> idents, | ||
| Map<String, CelFunctionDecl> functions, | ||
| boolean isComprehensionScope) { | ||
| this.idents = idents; | ||
| this.functions = functions; | ||
| this.isComprehensionScope = isComprehensionScope; | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that we're primarily working with Map decls, let's remove the extraneous ImmutableSets altogether. We can build the ImmutableMap directly in the Builder.build() (so just make
celFunctionDeclsandcelIdentDeclsimmutable maps)