Skip to content

Ensure that parse errors are nonempty (part 2/2)#945

Merged
khieta merged 4 commits intomainfrom
khieta/parse-errors-nonempty
Jun 4, 2024
Merged

Ensure that parse errors are nonempty (part 2/2)#945
khieta merged 4 commits intomainfrom
khieta/parse-errors-nonempty

Conversation

@khieta
Copy link
Copy Markdown
Contributor

@khieta khieta commented Jun 3, 2024

Description of changes

Followup to #922. Sorry in advance for the enormous diff!

The general goal of this PR was to change every function in cst_to_ast.rs to: 1) not use a &mut ToASTErrors input and 2) return a Result instead of an Option. I tried to preserve the current error behavior (i.e., which errors and how many are returned) as much as possible.

Two questions for reviewers:

  • I added some new cloning in the translation functions for cst::And, cst::Or, and cst::Relation due to my use of .split_first(). Open to suggestions for how to restructure this to avoid clones.
  • I deleted the comment referencing Eliminate the "false errors" mentioned in a TODO comment in cst_to_ast.rs #439. I don't remember what problem this issue was referring to -- should we just close it?

Issue #, if available

Checklist for requesting a review

The change in this PR is (choose one, and delete the other options):

  • A change "invisible" to users (e.g., documentation, changes to "internal" crates like cedar-policy-core, cedar-validator, etc.)

I confirm that this PR (choose one, and delete the other options):

  • Does not update the CHANGELOG because my change does not significantly impact released code.

I confirm that cedar-spec (choose one, and delete the other options):

  • Does not require updates because my change does not impact the Cedar formal model or DRT infrastructure.

Copy link
Copy Markdown
Contributor

@john-h-kastner-aws john-h-kastner-aws left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks pretty good

Comment thread cedar-policy-core/src/parser/cst_to_ast.rs Outdated
// Starting off with a failure and filtering items from the accessor list
// can cause false error messages. We consider this acceptable for now because
// they only occur along side a real error.
// TODO(#439): eliminate the false errors (likely with `Option`s inside `AstAccessor`)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you break immediately on an error now I feel like this issue might actually be fixed.

Regardless, I don't have a good idea of what the issue was to begin with, so I'm in favor of closing.

let maybe_rest = ParseErrors::transpose(or.extended.iter().map(|i| i.to_expr()));

let (first, rest) = flatten_tuple_2(maybe_first, maybe_rest)?;
match rest.split_first() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the clone be avoided by something like

let mut rest = rest.into_iter();
let second = rest.next();
match second {
    ....
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that works, thanks.

Copy link
Copy Markdown
Contributor

@cdisselkoen cdisselkoen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great. This is a huge improvement.

Comment thread cedar-policy-core/src/est.rs Outdated
let ident = maybe_is_when.map(|is_when| {
cst::Ident::Ident(if is_when { "when" } else { "unless" }.into())
});
})?;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not positive we want to ? out here -- if possible should we return both this is_when error and the EmptyClause error below? I'm trying to think what policy would end up in this case and I think it would be something like

permit(principal, action, resource) onlyWhen {};

or

permit(principal, action, resource) context {};

which conceivably has both error behaviors

Copy link
Copy Markdown
Contributor

@john-h-kastner-aws john-h-kastner-aws Jun 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the CST->EST pass, so this specific error case shouldn't be visible to users.

We did discuss returning both errors when adding the corresponding code to the cst->ast transform, but ended up not returning both errors. (I was and am i favor of returning both).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Easy enough to return both errors, so I fixed this in the latest commit.

Comment thread cedar-policy-core/src/est/expr.rs
Comment thread cedar-policy-core/src/parser/cst_to_ast.rs Outdated
Comment thread cedar-policy-core/src/parser/cst_to_ast.rs Outdated
Comment thread cedar-policy-core/src/parser/cst_to_ast.rs Outdated
Comment thread cedar-policy-core/src/parser/cst_to_ast.rs
@khieta
Copy link
Copy Markdown
Contributor Author

khieta commented Jun 4, 2024

I believe I've addressed all reviewer comments now -- let me know if I missed anything! Thanks for the reviews, I know this was a time sink ❤️

@khieta khieta merged commit 8a11783 into main Jun 4, 2024
@khieta khieta deleted the khieta/parse-errors-nonempty branch June 4, 2024 18:08
benelser pushed a commit to benelser/cedar that referenced this pull request Jan 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants