Skip to content

Commit

Permalink
Fix const stuff for updated rustc
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcelGarus committed Apr 20, 2023
1 parent 2708db2 commit b94e075
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 69 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions compiler/formatter/Cargo.toml
Expand Up @@ -11,5 +11,6 @@ candy_frontend = { path = "../frontend" }
derive_more = "0.99.17"
extension-trait = "1.0.1"
itertools = "0.10.0"
once_cell = "1.17.1"
traversal = "0.1.2"
unicode-width = "0.1.10"
13 changes: 7 additions & 6 deletions compiler/formatter/src/existing_parentheses.rs
Expand Up @@ -9,6 +9,7 @@ use candy_frontend::{
cst::{Cst, CstKind},
position::Offset,
};
use once_cell::sync::Lazy;
use std::borrow::Cow;

#[must_use]
Expand Down Expand Up @@ -151,9 +152,9 @@ impl<'a> ExistingParentheses<'a> {
let fits_in_one_line = !self.are_required_due_to_comments()
&& previous_width.last_line_fits(
info.indentation,
&(&SinglelineWidth::PARENTHESIS.into()
&(&(*SinglelineWidth::PARENTHESIS).into()
+ child.min_width(info.indentation.with_indent())
+ &SinglelineWidth::PARENTHESIS.into()),
+ &(*SinglelineWidth::PARENTHESIS).into()),
);
let child_trailing = if fits_in_one_line {
TrailingWhitespace::None
Expand All @@ -163,7 +164,7 @@ impl<'a> ExistingParentheses<'a> {
match self {
ExistingParentheses::None { child_start_offset } => {
let (opening, opening_width) = if fits_in_one_line {
(Cow::Borrowed("("), SinglelineWidth::PARENTHESIS)
(Cow::Borrowed("("), *SinglelineWidth::PARENTHESIS)
} else {
(
Cow::Owned(format!("(\n{}", info.indentation.with_indent())),
Expand All @@ -180,7 +181,7 @@ impl<'a> ExistingParentheses<'a> {
edits.insert(child_end_offset, ")");

FormattedCst::new(
&opening_width.into() + child_width + &SinglelineWidth::PARENTHESIS.into(),
&opening_width.into() + child_width + &(*SinglelineWidth::PARENTHESIS).into(),
ExistingWhitespace::empty(child_end_offset),
)
}
Expand All @@ -194,7 +195,7 @@ impl<'a> ExistingParentheses<'a> {
opening.whitespace.into_trailing_with_indentation(
edits,
&TrailingWithIndentationConfig::Trailing {
previous_width: previous_width + SinglelineWidth::PARENTHESIS,
previous_width: previous_width + *SinglelineWidth::PARENTHESIS,
indentation: info.indentation.with_indent(),
},
)
Expand All @@ -218,7 +219,7 @@ impl<'a> ExistingParentheses<'a> {
}

impl SinglelineWidth {
pub const PARENTHESIS: SinglelineWidth = 1.into();
pub const PARENTHESIS: Lazy<SinglelineWidth> = Lazy::new(|| 1.into());
}

fn split_whitespace(cst: &Cst) -> UnformattedCst {
Expand Down
14 changes: 7 additions & 7 deletions compiler/formatter/src/existing_whitespace.rs
Expand Up @@ -240,7 +240,7 @@ impl<'a> ExistingWhitespace<'a> {
} else {
edits.insert(self.start_offset, SPACE);
}
SinglelineWidth::SPACE
*SinglelineWidth::SPACE
}

#[must_use]
Expand Down Expand Up @@ -319,7 +319,7 @@ impl<'a> ExistingWhitespace<'a> {
trailing_range,
format!("{}{indentation}", NEWLINE.repeat(trailing_newline_count)),
);
comments_width + Width::NEWLINE + indentation.width()
comments_width + (*Width::NEWLINE).clone() + indentation.width()
}
fn format_trailing_comments(
edits: &mut TextEdits,
Expand Down Expand Up @@ -392,7 +392,7 @@ impl<'a> ExistingWhitespace<'a> {
};

comment_position = CommentPosition::NextLine(newline_count);
width += Width::NEWLINE;
width += Width::NEWLINE.clone();
}
CommentPosition::NextLine(_) if is_adopted => {
// We already encountered a newline (owned or adopted) and the new
Expand All @@ -419,7 +419,7 @@ impl<'a> ExistingWhitespace<'a> {
edits.delete(item.data.span.to_owned());
} else {
*count = count.checked_add(1).unwrap();
width += Width::NEWLINE;
width += Width::NEWLINE.clone();
}
}
},
Expand All @@ -441,7 +441,7 @@ impl<'a> ExistingWhitespace<'a> {
let space = match comment_position {
CommentPosition::FirstLine => {
let (space, space_width) = if ensure_space_before_first_comment {
(Cow::Borrowed(SPACE), SinglelineWidth::SPACE)
(Cow::Borrowed(SPACE), *SinglelineWidth::SPACE)
} else {
(Cow::default(), SinglelineWidth::default())
};
Expand All @@ -452,7 +452,7 @@ impl<'a> ExistingWhitespace<'a> {
width += &space_width.into();
space
} else {
width += Width::NEWLINE + indentation.width();
width += Width::NEWLINE.clone() + indentation.width();
Cow::Owned(format!("{NEWLINE}{indentation}"))
}
}
Expand All @@ -467,7 +467,7 @@ impl<'a> ExistingWhitespace<'a> {
.unwrap_or(item.data.span.start),
NEWLINE,
);
width += Width::NEWLINE + indentation.width();
width += Width::NEWLINE.clone() + indentation.width();
}
NewlineCount::Owned(_) => width += &indentation.width().into(),
}
Expand Down
54 changes: 28 additions & 26 deletions compiler/formatter/src/format.rs
Expand Up @@ -17,6 +17,7 @@ use candy_frontend::{
};
use extension_trait::extension_trait;
use itertools::Itertools;
use once_cell::sync::Lazy;
use traversal::dft_post_rev;

#[derive(Clone, Default)]
Expand Down Expand Up @@ -60,7 +61,7 @@ impl FormattingInfo {
Self {
indentation: if self.is_single_expression_in_assignment_body
&& previous_width
.last_line_fits(self.indentation, &SinglelineWidth::PARENTHESIS.into())
.last_line_fits(self.indentation, &(*SinglelineWidth::PARENTHESIS).into())
{
self.indentation.with_dedent()
} else {
Expand Down Expand Up @@ -310,8 +311,8 @@ pub(crate) fn format_cst<'a>(
(
&width_for_right_side
+ &bar_width
+ SinglelineWidth::PARENTHESIS
+ SinglelineWidth::PARENTHESIS,
+ *SinglelineWidth::PARENTHESIS
+ *SinglelineWidth::PARENTHESIS,
info.with_indent(),
)
} else {
Expand All @@ -325,9 +326,9 @@ pub(crate) fn format_cst<'a>(
edits,
&(previous_width
+ left.min_width(info.indentation)
+ SinglelineWidth::SPACE
+ *SinglelineWidth::SPACE
+ &bar_width
+ SinglelineWidth::SPACE),
+ *SinglelineWidth::SPACE),
right,
info,
)
Expand All @@ -338,7 +339,7 @@ pub(crate) fn format_cst<'a>(

let left_trailing = if let Some(right_first_line_width) = right_width.first_line_width()
&& (left.min_width(info.indentation)
+ SinglelineWidth::SPACE
+ *SinglelineWidth::SPACE
+ &bar_width
+ right_first_line_width)
.fits(info.indentation)
Expand Down Expand Up @@ -400,7 +401,7 @@ pub(crate) fn format_cst<'a>(
let min_width = &receiver.min_width(info.indentation)
+ arguments
.iter()
.map(|it| &SinglelineWidth::SPACE.into() + it.min_singleline_width())
.map(|it| &(*SinglelineWidth::SPACE).into() + it.min_singleline_width())
.sum::<Width>();
let (is_singleline, argument_info, trailing) =
if previous_width.last_line_fits(info.indentation, &min_width) {
Expand Down Expand Up @@ -574,7 +575,7 @@ pub(crate) fn format_cst<'a>(
{
Width::multiline(None, info.indentation.with_indent().width())
} else {
previous_width + SinglelineWidth::PARENTHESIS
previous_width + *SinglelineWidth::PARENTHESIS
};
(previous_width_for_struct, info.with_indent())
} else {
Expand Down Expand Up @@ -686,7 +687,7 @@ pub(crate) fn format_cst<'a>(
let (body_width, whitespace) = format_csts(
edits,
&(previous_width_for_arrow
+ SinglelineWidth::SPACE
+ *SinglelineWidth::SPACE
+ arrow.min_width(info.indentation.with_indent())),
body,
arrow.whitespace.end_offset(),
Expand All @@ -696,7 +697,7 @@ pub(crate) fn format_cst<'a>(

let arrow_trailing = if pattern_width.last_line_fits(
info.indentation,
&(arrow.min_width(info.indentation) + SinglelineWidth::SPACE + &body_width),
&(arrow.min_width(info.indentation) + *SinglelineWidth::SPACE + &body_width),
) {
TrailingWhitespace::Space
} else {
Expand Down Expand Up @@ -732,10 +733,10 @@ pub(crate) fn format_cst<'a>(
format_cst(edits, &previous_width_for_inner, arrow, &info.with_indent());

let parameters_trailing = if (opening_curly_brace.min_width(info.indentation)
+ SinglelineWidth::SPACE
+ *SinglelineWidth::SPACE
+ parameters
.iter()
.map(|it| it.min_width(info.indentation) + SinglelineWidth::SPACE)
.map(|it| it.min_width(info.indentation) + *SinglelineWidth::SPACE)
.sum::<Width>()
+ arrow.min_width(info.indentation))
.fits(info.indentation)
Expand All @@ -756,7 +757,7 @@ pub(crate) fn format_cst<'a>(
let trailing = if parameters_width.last_line_fits(
info.indentation,
&(it.min_width(info.indentation)
+ SinglelineWidth::SPACE
+ *SinglelineWidth::SPACE
+ arrow.child_width()),
) {
TrailingWhitespace::Space
Expand Down Expand Up @@ -800,7 +801,7 @@ pub(crate) fn format_cst<'a>(
.unwrap_or_default();
let body_min_width = body.min_width(info.indentation);
let width_until_arrow = opening_curly_brace.min_width(info.indentation)
+ SinglelineWidth::SPACE
+ *SinglelineWidth::SPACE
+ &parameters_and_arrow_min_width;

// Opening curly brace
Expand All @@ -810,7 +811,7 @@ pub(crate) fn format_cst<'a>(
} else {
&width_until_arrow
+ &body_min_width
+ SinglelineWidth::SPACE
+ *SinglelineWidth::SPACE
+ &closing_curly_brace_width
};
let opening_curly_brace_trailing =
Expand All @@ -824,14 +825,14 @@ pub(crate) fn format_cst<'a>(

// Body
let space_if_parameters = if parameters_width_and_arrow.is_some() {
SinglelineWidth::SPACE
*SinglelineWidth::SPACE
} else {
SinglelineWidth::default()
};
let space_if_body_not_empty = if body_min_width.is_empty() {
SinglelineWidth::default()
} else {
SinglelineWidth::SPACE
*SinglelineWidth::SPACE
};
let width_from_body =
body_min_width + space_if_body_not_empty + &closing_curly_brace_width;
Expand Down Expand Up @@ -900,7 +901,7 @@ pub(crate) fn format_cst<'a>(
edits,
&(previous_width_for_assignment_sign
+ assignment_sign.min_width(info.indentation)
+ SinglelineWidth::SPACE),
+ *SinglelineWidth::SPACE),
body,
assignment_sign.whitespace.end_offset(),
&body_info,
Expand All @@ -917,14 +918,14 @@ pub(crate) fn format_cst<'a>(

let assignment_sign_trailing = if left_width.last_line_fits(
info.indentation,
&(&assignment_sign.min_width(info.indentation) + SinglelineWidth::SPACE + &body_width + &body_whitespace_width),
&(&assignment_sign.min_width(info.indentation) + *SinglelineWidth::SPACE + &body_width + &body_whitespace_width),
) {
TrailingWhitespace::Space
} else if !body_whitespace_has_comments
&& let Some(body_first_line_width) = body_width.first_line_width()
&& left_width.last_line_fits(
info.indentation,
&(&assignment_sign.min_width(info.indentation) + SinglelineWidth::SPACE + body_first_line_width),
&(&assignment_sign.min_width(info.indentation) + *SinglelineWidth::SPACE + body_first_line_width),
) {
TrailingWhitespace::Space
} else {
Expand Down Expand Up @@ -975,12 +976,12 @@ impl<'a> Argument<'a> {
} else {
let argument = format_cst(edits, previous_width, argument, info);
let mut min_singleline_width = argument.min_width(info.indentation.with_indent());
const PARENTHESES_WIDTH: SinglelineWidth =
SinglelineWidth::PARENTHESIS + SinglelineWidth::PARENTHESIS;
let parentheses_width: SinglelineWidth =
*SinglelineWidth::PARENTHESIS + *SinglelineWidth::PARENTHESIS;
match precedence {
Some(PrecedenceCategory::High) => {}
Some(PrecedenceCategory::Low) => min_singleline_width += &PARENTHESES_WIDTH.into(),
None if parentheses.is_some() => min_singleline_width += &PARENTHESES_WIDTH.into(),
Some(PrecedenceCategory::Low) => min_singleline_width += &parentheses_width.into(),
None if parentheses.is_some() => min_singleline_width += &parentheses_width.into(),
None => {}
}
MaybeSandwichLikeArgument::Other {
Expand All @@ -996,11 +997,12 @@ impl<'a> Argument<'a> {
}

/// Width of the opening parenthesis / bracket / curly brace
const SANDWICH_LIKE_MIN_SINGLELINE_WIDTH: SinglelineWidth = SinglelineWidth::PARENTHESIS;
const SANDWICH_LIKE_MIN_SINGLELINE_WIDTH: Lazy<SinglelineWidth> =
Lazy::new(|| *SinglelineWidth::PARENTHESIS);
fn min_singleline_width(&self) -> Width {
match &self.argument {
MaybeSandwichLikeArgument::SandwichLike(_) => {
Self::SANDWICH_LIKE_MIN_SINGLELINE_WIDTH.into()
(*Self::SANDWICH_LIKE_MIN_SINGLELINE_WIDTH).into()
}
MaybeSandwichLikeArgument::Other {
min_singleline_width,
Expand Down

2 comments on commit b94e075

@jwbot
Copy link
Collaborator

@jwbot jwbot commented on b94e075 Apr 20, 2023

Choose a reason for hiding this comment

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

Compiler

Benchmark suite Current: b94e075 Previous: 69e44f8 Ratio
Time: Compiler/hello_world 1514191 ns/iter (± 3366) 19940474 ns/iter (± 263151) 0.07593555699829402
Time: Compiler/fibonacci 636787843 ns/iter (± 3235638) 181230857 ns/iter (± 766807) 3.51
Time: VM Runtime/hello_world 71497 ns/iter (± 1542) 18065919 ns/iter (± 369844) 0.003957562302808952
Time: VM Runtime/fibonacci/15 210692727 ns/iter (± 3057259) 199843679 ns/iter (± 4988324) 1.05

This comment was automatically generated by workflow using github-action-benchmark.

@jwbot
Copy link
Collaborator

@jwbot jwbot commented on b94e075 Apr 20, 2023

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'Compiler'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.

Benchmark suite Current: b94e075 Previous: 69e44f8 Ratio
Time: Compiler/fibonacci 636787843 ns/iter (± 3235638) 181230857 ns/iter (± 766807) 3.51

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.