script: Remove unreachable assertions in xpath parser (#39588)

`relative_path_expr` can only ever return `Expr::Path`. If we make it
return a `PathExpr` then callers can rely on this invariant.

Testing: There's only limited testing for this change due to
https://github.com/servo/servo/issues/39551. But I think that's okay,
since the change is fairly trivial.
Part of https://github.com/servo/servo/issues/34527

Signed-off-by: Simon Wülker <simon.wuelker@arcor.de>
This commit is contained in:
Simon Wülker 2025-09-30 15:20:31 +02:00 committed by GitHub
parent 6ffc0cd482
commit 0a1633c52a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -394,40 +394,32 @@ fn path_expr(input: &str) -> IResult<&str, Expr> {
// "//" RelativePathExpr
map(
pair(tag("//"), move |i| relative_path_expr(true, i)),
|(_, rel_path)| {
|(_, relative_path)| {
Expr::Path(PathExpr {
is_absolute: true,
is_descendant: true,
steps: match rel_path {
Expr::Path(p) => p.steps,
_ => unreachable!(),
},
steps: relative_path.steps,
})
},
),
// "/" RelativePathExpr?
map(
pair(char('/'), opt(move |i| relative_path_expr(false, i))),
|(_, rel_path)| {
|(_, relative_path)| {
Expr::Path(PathExpr {
is_absolute: true,
is_descendant: false,
steps: rel_path
.map(|p| match p {
Expr::Path(p) => p.steps,
_ => unreachable!(),
})
.unwrap_or_default(),
steps: relative_path.map(|path| path.steps).unwrap_or_default(),
})
},
),
// RelativePathExpr
move |i| relative_path_expr(false, i),
map(move |i| relative_path_expr(false, i), Expr::Path),
)))
.parse(input)
}
fn relative_path_expr(is_descendant: bool, input: &str) -> IResult<&str, Expr> {
fn relative_path_expr(is_descendant: bool, input: &str) -> IResult<&str, PathExpr> {
let (input, first) = step_expr(is_descendant, input)?;
let (input, steps) = many0(pair(
ws(alt((value(true, tag("//")), value(false, char('/'))))),
@ -450,11 +442,11 @@ fn relative_path_expr(is_descendant: bool, input: &str) -> IResult<&str, Expr> {
Ok((
input,
Expr::Path(PathExpr {
PathExpr {
is_absolute: false,
is_descendant: false,
steps: all_steps,
}),
},
))
}