| 1 | // Copyright 2018 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package checker |
| 16 | |
| 17 | import ( |
| 18 | "github.com/google/cel-go/common" |
| 19 | "github.com/google/cel-go/common/ast" |
| 20 | "github.com/google/cel-go/common/types" |
| 21 | ) |
| 22 | |
| 23 | // typeErrors is a specialization of Errors. |
| 24 | type typeErrors struct { |
| 25 | errs *common.Errors |
| 26 | } |
| 27 | |
| 28 | func (e *typeErrors) fieldTypeMismatch(id int64, l common.Location, name string, field, value *types.Type) { |
| 29 | e.errs.ReportErrorAtID(id, l, "expected type of field '%s' is '%s' but provided type is '%s'", |
| 30 | name, FormatCELType(field), FormatCELType(value)) |
| 31 | } |
| 32 | |
| 33 | func (e *typeErrors) incompatibleType(id int64, l common.Location, ex ast.Expr, prev, next *types.Type) { |
| 34 | e.errs.ReportErrorAtID(id, l, |
| 35 | "incompatible type already exists for expression: %v(%d) old:%v, new:%v", ex, ex.ID(), prev, next) |
| 36 | } |
| 37 | |
| 38 | func (e *typeErrors) noMatchingOverload(id int64, l common.Location, name string, args []*types.Type, isInstance bool) { |
| 39 | signature := formatFunctionDeclType(nil, args, isInstance) |
| 40 | e.errs.ReportErrorAtID(id, l, "found no matching overload for '%s' applied to '%s'", name, signature) |
| 41 | } |
| 42 | |
| 43 | func (e *typeErrors) notAComprehensionRange(id int64, l common.Location, t *types.Type) { |
| 44 | e.errs.ReportErrorAtID(id, l, "expression of type '%s' cannot be range of a comprehension (must be list, map, or dynamic)", |
| 45 | FormatCELType(t)) |
| 46 | } |
| 47 | |
| 48 | func (e *typeErrors) notAnOptionalFieldSelectionCall(id int64, l common.Location, err string) { |
| 49 | e.errs.ReportErrorAtID(id, l, "unsupported optional field selection: %s", err) |
| 50 | } |
| 51 | |
| 52 | func (e *typeErrors) notAnOptionalFieldSelection(id int64, l common.Location, field ast.Expr) { |
| 53 | e.errs.ReportErrorAtID(id, l, "unsupported optional field selection: %v", field) |
| 54 | } |
| 55 | |
| 56 | func (e *typeErrors) notAType(id int64, l common.Location, typeName string) { |
| 57 | e.errs.ReportErrorAtID(id, l, "'%s' is not a type", typeName) |
| 58 | } |
| 59 | |
| 60 | func (e *typeErrors) notAMessageType(id int64, l common.Location, typeName string) { |
| 61 | e.errs.ReportErrorAtID(id, l, "'%s' is not a message type", typeName) |
| 62 | } |
| 63 | |
| 64 | func (e *typeErrors) referenceRedefinition(id int64, l common.Location, ex ast.Expr, prev, next *ast.ReferenceInfo) { |
| 65 | e.errs.ReportErrorAtID(id, l, |
| 66 | "reference already exists for expression: %v(%d) old:%v, new:%v", ex, ex.ID(), prev, next) |
| 67 | } |
| 68 | |
| 69 | func (e *typeErrors) typeDoesNotSupportFieldSelection(id int64, l common.Location, t *types.Type) { |
| 70 | e.errs.ReportErrorAtID(id, l, "type '%s' does not support field selection", FormatCELType(t)) |
| 71 | } |
| 72 | |
| 73 | func (e *typeErrors) typeMismatch(id int64, l common.Location, expected, actual *types.Type) { |
| 74 | e.errs.ReportErrorAtID(id, l, "expected type '%s' but found '%s'", |
| 75 | FormatCELType(expected), FormatCELType(actual)) |
| 76 | } |
| 77 | |
| 78 | func (e *typeErrors) undefinedField(id int64, l common.Location, field string) { |
| 79 | e.errs.ReportErrorAtID(id, l, "undefined field '%s'", field) |
| 80 | } |
| 81 | |
| 82 | func (e *typeErrors) undeclaredReference(id int64, l common.Location, container string, name string) { |
| 83 | e.errs.ReportErrorAtID(id, l, "undeclared reference to '%s' (in container '%s')", name, container) |
| 84 | } |
| 85 | |
| 86 | func (e *typeErrors) unexpectedFailedResolution(id int64, l common.Location, typeName string) { |
| 87 | e.errs.ReportErrorAtID(id, l, "unexpected failed resolution of '%s'", typeName) |
| 88 | } |
| 89 | |
| 90 | func (e *typeErrors) unexpectedASTType(id int64, l common.Location, kind, typeName string) { |
| 91 | e.errs.ReportErrorAtID(id, l, "unexpected %s type: %v", kind, typeName) |
| 92 | } |