Skip to content

chore: use const fn in Span #10320

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions crates/ast_node/src/spanned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,15 @@ fn make_body_for_variant(v: &VariantBinder<'_>, bindings: Vec<BindedField<'_>>)
match (lo, hi) {
(Some((lo_field, _)), Some((hi_field, _))) => {
// Create a new span from lo_field.lo(), hi_field.hi()
Box::new(parse_quote!(swc_common::Spanned::span(#lo_field)
.with_hi(swc_common::Spanned::span(#hi_field).hi())))
Box::new(parse_quote!({
let lo = swc_common::Spanned::span(#lo_field).lo();
let hi = swc_common::Spanned::span(#hi_field).hi();
if lo < hi {
Span::new(lo, hi)
} else {
Span::new(hi, lo)
}
}))
}
_ => panic!("#[derive(Spanned)]: #[span(lo)] and #[span(hi)] is required"),
}
Expand Down
21 changes: 13 additions & 8 deletions crates/jsdoc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,15 @@ pub fn parse(i: Input) -> IResult<Input, JsDoc> {
}

let hi = i.span().hi;
let span = if lo < hi {
Span::new(lo, hi)
} else {
Span::new(hi, lo)
};
Ok((
i,
JsDoc {
span: Span::new(lo, hi),
span,
tags,
description,
},
Expand Down Expand Up @@ -560,13 +565,13 @@ fn parse_name_path(mut i: Input) -> IResult<Input, NamePath> {
return Err(err);
}

return Ok((
i,
NamePath {
span: Span::new(lo, i.span().hi),
components,
},
));
let hi = i.span().hi;
let span = if lo < hi {
Span::new(lo, hi)
} else {
Span::new(hi, lo)
};
return Ok((i, NamePath { span, components }));
}
};
i = input;
Expand Down
7 changes: 2 additions & 5 deletions crates/swc_common/src/syntax_pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,11 +390,8 @@ impl Span {
}

#[inline]
pub fn new(mut lo: BytePos, mut hi: BytePos) -> Self {
if lo > hi {
Copy link
Member

Choose a reason for hiding this comment

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

Actually #9962 is about removing this swap

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see. But I have two questions:
• Does this change break existing code?
• Should we add assert!(lo < hi) or debug_assert!(lo < hi)?

Copy link
Member

Choose a reason for hiding this comment

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

Yes for the first question. (It’s a bug IMO)

And debug assert would be enough considering the number of parser tests

Copy link
Member

Choose a reason for hiding this comment

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

Moving swap to the caller code will make it slower. That's why simply changing it to a const fn slowed it. (swap operation is inlined into call side with constant swap)

image

std::mem::swap(&mut lo, &mut hi);
}

pub const fn new(lo: BytePos, hi: BytePos) -> Self {
debug_assert!(lo.0 <= hi.0);
Span { lo, hi }
}

Expand Down
10 changes: 9 additions & 1 deletion crates/swc_ecma_ast/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1348,7 +1348,15 @@ impl Spanned for ExprOrSpread {
fn span(&self) -> Span {
let expr = self.expr.span();
match self.spread {
Some(spread) => expr.with_lo(spread.lo()),
Some(spread) => {
let lo = spread.lo();
let hi = expr.hi();
if lo < hi {
Span::new(lo, hi)
} else {
Span::new(hi, lo)
}
}
None => expr,
}
}
Expand Down
10 changes: 9 additions & 1 deletion crates/swc_ecma_ast/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,15 @@ pub struct BindingIdent {
impl Spanned for BindingIdent {
fn span(&self) -> Span {
match &self.type_ann {
Some(ann) => Span::new(self.id.span.lo(), ann.span().hi()),
Some(ann) => {
let lo = self.id.span.lo();
let hi = ann.span().hi();
if lo > hi {
Span::new(hi, lo)
} else {
Span::new(lo, hi)
}
}
None => self.id.span,
}
}
Expand Down
10 changes: 9 additions & 1 deletion crates/swc_ecma_compat_es2015/src/template_literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,15 @@ impl VisitMut for TemplateLiteral {
obj
} else {
CallExpr {
span: span.with_hi(expr_span.hi() + BytePos(1)),
span: {
let lo = span.lo;
let hi = expr_span.hi() + BytePos(1);
if lo < hi {
swc_common::Span::new(lo, hi)
} else {
swc_common::Span::new(hi, lo)
}
},
callee: MemberExpr {
span: DUMMY_SP,
obj,
Expand Down
6 changes: 5 additions & 1 deletion crates/swc_html_parser/src/parser/macros.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
macro_rules! span {
($parser:expr, $start:expr) => {{
let last_pos = $parser.input.last_pos()?;
swc_common::Span::new($start, last_pos)
if last_pos > $start {
swc_common::Span::new($start, last_pos)
} else {
swc_common::Span::new(last_pos, $start)
}
}};
}

Expand Down
Loading