Skip to content

Commit cd31830

Browse files
implement does_restricted_expr_implement_schematype without using schematype_of_restricted_expr
Signed-off-by: Andrew Wells <[email protected]>
1 parent 0337bf4 commit cd31830

File tree

2 files changed

+29
-28
lines changed

2 files changed

+29
-28
lines changed

cedar-policy-core/src/entities/conformance.rs

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -182,43 +182,36 @@ pub fn typecheck_value_against_schematype(
182182
/// Check whether the given `RestrictedExpr` is a valid instance of `SchemaType`
183183
pub fn does_restricted_expr_implement_schematype(
184184
expr: BorrowedRestrictedExpr<'_>,
185-
expr_ty: &SchemaType,
186185
expected_ty: &SchemaType,
187186
extensions: &Extensions<'_>,
188187
) -> bool {
189188
use SchemaType::*;
190-
if expr_ty == expected_ty {
191-
return true;
192-
}
193189

194-
match (expr_ty, expected_ty) {
195-
(Set { .. }, EmptySet) => true,
196-
(EmptySet, Set { .. }) => true,
197-
(
198-
Set {
199-
element_ty: expr_elm_ty,
200-
},
201-
Set { element_ty: elty },
202-
) => match expr.as_set_elements() {
203-
Some(mut els) => els.all(|e| {
204-
does_restricted_expr_implement_schematype(e, expr_elm_ty, elty, extensions)
205-
}),
190+
match expected_ty {
191+
Bool => expr.as_bool().is_some(),
192+
Long => expr.as_long().is_some(),
193+
String => expr.as_string().is_some(),
194+
EmptySet => expr.as_set_elements().is_some(),
195+
Set { .. }
196+
if expr.as_set_elements().is_some() && expr.as_set_elements().unwrap().count() == 0 =>
197+
{
198+
true
199+
}
200+
201+
Set { element_ty: elty } => match expr.as_set_elements() {
202+
Some(mut els) => {
203+
els.all(|e| does_restricted_expr_implement_schematype(e, elty, extensions))
204+
}
206205
None => false,
207206
},
208-
(
209-
Record {
210-
attrs: expr_attrs, ..
211-
},
212-
Record { attrs, open_attrs },
213-
) => match expr.as_record_pairs() {
207+
Record { attrs, open_attrs } => match expr.as_record_pairs() {
214208
Some(pairs) => {
215209
let pairs_map: BTreeMap<&SmolStr, BorrowedRestrictedExpr<'_>> = pairs.collect();
216210
let all_req_schema_attrs_in_record = attrs.iter().all(|(k, v)| {
217211
!v.required
218212
|| match pairs_map.get(k) {
219213
Some(inner_e) => does_restricted_expr_implement_schematype(
220214
*inner_e,
221-
&expr_attrs.get(k).unwrap().attr_type,
222215
&v.attr_type,
223216
extensions,
224217
),
@@ -229,7 +222,6 @@ pub fn does_restricted_expr_implement_schematype(
229222
pairs_map.iter().all(|(k, inner_e)| match attrs.get(*k) {
230223
Some(sch_ty) => does_restricted_expr_implement_schematype(
231224
*inner_e,
232-
&expr_attrs.get(*k).unwrap().attr_type,
233225
&sch_ty.attr_type,
234226
extensions,
235227
),
@@ -239,7 +231,17 @@ pub fn does_restricted_expr_implement_schematype(
239231
}
240232
None => false,
241233
},
242-
_ => false,
234+
Extension { name } => match expr.as_extn_fn_call() {
235+
Some((actual_name, _)) => match format!("{:?}", name.0.id).as_str() {
236+
"Id(\"ipaddr\")" => format!("{:?}", actual_name.0.id).as_str() == "Id(\"ip\")",
237+
_ => name == actual_name,
238+
},
239+
None => false,
240+
},
241+
Entity { ty } => match expr.as_euid() {
242+
Some(actual_euid) => actual_euid.entity_type() == ty,
243+
None => false,
244+
},
243245
}
244246
}
245247

@@ -257,8 +259,7 @@ pub fn typecheck_restricted_expr_against_schematype(
257259
// directly?
258260
match schematype_of_restricted_expr(expr, extensions) {
259261
Ok(actual_ty) => {
260-
if does_restricted_expr_implement_schematype(expr, &actual_ty, expected_ty, extensions)
261-
{
262+
if does_restricted_expr_implement_schematype(expr, expected_ty, extensions) {
262263
// typecheck passes
263264
Ok(())
264265
} else {

cedar-policy/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3946,7 +3946,7 @@ mod issue_618 {
39463946
round_trip(r#"permit(principal, action, resource) when { principal["\n"] };"#);
39473947
round_trip(r#"permit(principal, action, resource) when { {"\n": 0} };"#);
39483948
round_trip(
3949-
r#"@annotation("\n")
3949+
r#"@annotation("\n")
39503950
permit(principal, action, resource) when { {"\n": 0} };"#,
39513951
);
39523952
}

0 commit comments

Comments
 (0)