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
ast: Deprecating any() and all() built-in functions #4271
Changes from 2 commits
a4dba1d
2a9b8c0
1aac9de
284a47d
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 |
---|---|---|
|
@@ -103,6 +103,7 @@ type Compiler struct { | |
builtins map[string]*Builtin // universe of built-in functions | ||
customBuiltins map[string]*Builtin // user-supplied custom built-in functions (deprecated: use capabilities) | ||
unsafeBuiltinsMap map[string]struct{} // user-supplied set of unsafe built-ins functions to block (deprecated: use capabilities) | ||
deprecatedBuiltinsMap map[string]struct{} // set of deprecated, but not removed, built-in functions | ||
enablePrintStatements bool // indicates if print statements should be elided (default) | ||
comprehensionIndices map[*Term]*ComprehensionIndex // comprehension key index | ||
initialized bool // indicates if init() has been called | ||
|
@@ -240,11 +241,12 @@ func NewCompiler() *Compiler { | |
}, func(x util.T) int { | ||
return x.(Ref).Hash() | ||
}), | ||
maxErrs: CompileErrorLimitDefault, | ||
after: map[string][]CompilerStageDefinition{}, | ||
unsafeBuiltinsMap: map[string]struct{}{}, | ||
comprehensionIndices: map[*Term]*ComprehensionIndex{}, | ||
debug: debug.Discard(), | ||
maxErrs: CompileErrorLimitDefault, | ||
after: map[string][]CompilerStageDefinition{}, | ||
unsafeBuiltinsMap: map[string]struct{}{}, | ||
deprecatedBuiltinsMap: map[string]struct{}{}, | ||
comprehensionIndices: map[*Term]*ComprehensionIndex{}, | ||
debug: debug.Discard(), | ||
} | ||
|
||
c.ModuleTree = NewModuleTree(nil) | ||
|
@@ -1165,7 +1167,7 @@ func (c *Compiler) checkTypes() { | |
|
||
func (c *Compiler) checkUnsafeBuiltins() { | ||
for _, name := range c.sorted { | ||
errs := checkUnsafeBuiltins(c.unsafeBuiltinsMap, c.Modules[name]) | ||
errs := checkUnsafeBuiltins(c.unsafeBuiltinsMap, c.deprecatedBuiltinsMap, c.Modules[name]) | ||
for _, err := range errs { | ||
c.err(err) | ||
} | ||
|
@@ -1224,6 +1226,9 @@ func (c *Compiler) init() { | |
|
||
for _, bi := range c.capabilities.Builtins { | ||
c.builtins[bi.Name] = bi | ||
if c.strict && bi.IsDeprecated() { | ||
c.deprecatedBuiltinsMap[bi.Name] = struct{}{} | ||
} | ||
} | ||
|
||
for name, bi := range c.customBuiltins { | ||
|
@@ -2126,7 +2131,7 @@ func (qc *queryCompiler) checkUnsafeBuiltins(_ *QueryContext, body Body) (Body, | |
} else { | ||
unsafe = qc.compiler.unsafeBuiltinsMap | ||
} | ||
errs := checkUnsafeBuiltins(unsafe, body) | ||
errs := checkUnsafeBuiltins(unsafe, qc.compiler.deprecatedBuiltinsMap, body) | ||
if len(errs) > 0 { | ||
return nil, errs | ||
} | ||
|
@@ -4485,14 +4490,17 @@ func safetyErrorSlice(unsafe unsafeVars, rewritten map[Var]Var) (result Errors) | |
return | ||
} | ||
|
||
func checkUnsafeBuiltins(unsafeBuiltinsMap map[string]struct{}, node interface{}) Errors { | ||
func checkUnsafeBuiltins(unsafeBuiltinsMap map[string]struct{}, deprecatedBuiltinsMap map[string]struct{}, node interface{}) Errors { | ||
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. Perhaps I should rename this function as "unsafe" has a special meaning here. How are we on renaming existing compiler stages? 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. Renaming the internal function is fine, but renaming the stage could break callers because we have an API that lets them register custom stages before/after a stage name... I agree that "safe" is not the best term to use here. That said, the "unsafe function" support is deprecated in favor of using capabilities--so, I'd probably just leave the stage name as is (we can remove it entirely eventually.) |
||
errs := make(Errors, 0) | ||
WalkExprs(node, func(x *Expr) bool { | ||
if x.IsCall() { | ||
operator := x.Operator().String() | ||
if _, ok := unsafeBuiltinsMap[operator]; ok { | ||
errs = append(errs, NewError(TypeErr, x.Loc(), "unsafe built-in function calls in expression: %v", operator)) | ||
} | ||
if _, ok := deprecatedBuiltinsMap[operator]; ok { | ||
errs = append(errs, NewError(TypeErr, x.Loc(), "deprecated built-in function calls in expression: %v", operator)) | ||
} | ||
} | ||
return false | ||
}) | ||
|
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.
Just moving these down to the "deprecated" section.