Skip to content
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
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "nightly-2025-01-02"
channel = "nightly-2025-04-01"
components = ["llvm-tools", "rustc-dev"]
15 changes: 10 additions & 5 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ fn make_opts() -> Options {
"Set options from command line. These settings take priority over .rustfmt.toml",
"[key1=val1,key2=val2...]",
);
opts.optopt(
"",
"style-edition",
"The edition of the Style Guide.",
"[2015|2018|2021|2024]",
);

if is_nightly {
opts.optflag(
Expand Down Expand Up @@ -818,7 +824,6 @@ mod test {
options.inline_config = HashMap::from([("version".to_owned(), "Two".to_owned())]);
let config = get_config(None, Some(options));
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
assert_eq!(config.overflow_delimited_expr(), true);
}

#[nightly_only_test]
Expand All @@ -828,7 +833,6 @@ mod test {
let config_file = Some(Path::new("tests/config/style-edition/just-version"));
let config = get_config(config_file, Some(options));
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
assert_eq!(config.overflow_delimited_expr(), true);
}

#[nightly_only_test]
Expand Down Expand Up @@ -873,7 +877,6 @@ mod test {
]);
let config = get_config(None, Some(options));
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
assert_eq!(config.overflow_delimited_expr(), true);
}

#[nightly_only_test]
Expand Down Expand Up @@ -939,7 +942,6 @@ mod test {
options.style_edition = Some(StyleEdition::Edition2024);
let config = get_config(None, Some(options));
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
assert_eq!(config.overflow_delimited_expr(), true);
}

#[nightly_only_test]
Expand All @@ -949,6 +951,8 @@ mod test {
let config_file = Some(Path::new("tests/config/style-edition/overrides"));
let config = get_config(config_file, Some(options));
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
// FIXME: this test doesn't really exercise anything, since
// `overflow_delimited_expr` is disabled by default in edition 2024.
assert_eq!(config.overflow_delimited_expr(), false);
}

Expand All @@ -960,7 +964,8 @@ mod test {
options.inline_config =
HashMap::from([("overflow_delimited_expr".to_owned(), "false".to_owned())]);
let config = get_config(config_file, Some(options));
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
// FIXME: this test doesn't really exercise anything, since
// `overflow_delimited_expr` is disabled by default in edition 2024.
assert_eq!(config.overflow_delimited_expr(), false);
}
}
10 changes: 9 additions & 1 deletion src/chains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ enum ChainItemKind {
StructField(symbol::Ident),
TupleField(symbol::Ident, bool),
Await,
Yield,
Comment(String, CommentPosition),
}

Expand All @@ -206,6 +207,7 @@ impl ChainItemKind {
| ChainItemKind::StructField(..)
| ChainItemKind::TupleField(..)
| ChainItemKind::Await
| ChainItemKind::Yield
| ChainItemKind::Comment(..) => false,
}
}
Expand Down Expand Up @@ -260,6 +262,10 @@ impl ChainItemKind {
let span = mk_sp(nested.span.hi(), expr.span.hi());
(ChainItemKind::Await, span)
}
ast::ExprKind::Yield(ast::YieldKind::Postfix(ref nested)) => {
let span = mk_sp(nested.span.hi(), expr.span.hi());
(ChainItemKind::Yield, span)
}
_ => {
return (
ChainItemKind::Parent {
Expand Down Expand Up @@ -307,6 +313,7 @@ impl Rewrite for ChainItem {
rewrite_ident(context, ident)
),
ChainItemKind::Await => ".await".to_owned(),
ChainItemKind::Yield => ".yield".to_owned(),
ChainItemKind::Comment(ref comment, _) => {
rewrite_comment(comment, false, shape, context.config)?
}
Expand Down Expand Up @@ -509,7 +516,8 @@ impl Chain {
}),
ast::ExprKind::Field(ref subexpr, _)
| ast::ExprKind::Try(ref subexpr)
| ast::ExprKind::Await(ref subexpr, _) => Some(SubExpr {
| ast::ExprKind::Await(ref subexpr, _)
| ast::ExprKind::Yield(ast::YieldKind::Postfix(ref subexpr)) => Some(SubExpr {
expr: Self::convert_try(subexpr, context),
is_method_call_receiver: false,
}),
Expand Down
1 change: 0 additions & 1 deletion src/closures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ fn rewrite_closure_with_block(
.first()
.map(|attr| attr.span.to(body.span))
.unwrap_or(body.span),
could_be_bare_literal: false,
};
let block = crate::expr::rewrite_block_with_visitor(
context,
Expand Down
4 changes: 2 additions & 2 deletions src/config/file_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
use itertools::Itertools;
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use std::{cmp, fmt, iter, str};

use rustc_data_structures::sync::Lrc;
use rustc_span::SourceFile;
use serde::{Deserialize, Deserializer, Serialize, Serializer, ser};
use serde_json as json;
use thiserror::Error;

/// A range of lines in a file, inclusive of both ends.
pub struct LineRange {
pub(crate) file: Lrc<SourceFile>,
pub(crate) file: Arc<SourceFile>,
pub(crate) lo: usize,
pub(crate) hi: usize,
}
Expand Down
2 changes: 1 addition & 1 deletion src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,7 @@ binop_separator = "Front"
remove_nested_parens = true
combine_control_expr = true
short_array_element_width_threshold = 10
overflow_delimited_expr = true
overflow_delimited_expr = false
struct_field_align_threshold = 0
enum_discrim_align_threshold = 0
match_arm_blocks = true
Expand Down
4 changes: 2 additions & 2 deletions src/config/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ config_option_with_style_edition_default!(
RemoveNestedParens, bool, _ => true;
CombineControlExpr, bool, _ => true;
ShortArrayElementWidthThreshold, usize, _ => 10;
OverflowDelimitedExpr, bool, Edition2024 => true, _ => false;
OverflowDelimitedExpr, bool, _ => false;
StructFieldAlignThreshold, usize, _ => 0;
EnumDiscrimAlignThreshold, usize, _ => 0;
MatchArmBlocks, bool, _ => true;
Expand All @@ -677,7 +677,7 @@ config_option_with_style_edition_default!(
BlankLinesLowerBound, usize, _ => 0;
EditionConfig, Edition, _ => Edition::Edition2015;
StyleEditionConfig, StyleEdition,
Edition2024 => StyleEdition::Edition2024, _ => StyleEdition::Edition2015;
Edition2024 => StyleEdition::Edition2024, _ => StyleEdition::Edition2015;
VersionConfig, Version, Edition2024 => Version::Two, _ => Version::One;
InlineAttributeWidth, usize, _ => 0;
FormatGeneratedFiles, bool, _ => true;
Expand Down
9 changes: 7 additions & 2 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ pub(crate) fn format_expr(
ast::ExprKind::Tup(ref items) => {
rewrite_tuple(context, items.iter(), expr.span, shape, items.len() == 1)
}
ast::ExprKind::Use(_, _) => {
// FIXME: properly implement this
Ok(context.snippet(expr.span()).to_owned())
}
ast::ExprKind::Let(ref pat, ref expr, _span, _) => rewrite_let(context, shape, pat, expr),
ast::ExprKind::If(..)
| ast::ExprKind::ForLoop { .. }
Expand Down Expand Up @@ -249,7 +253,7 @@ pub(crate) fn format_expr(
Ok(format!("break{id_str}"))
}
}
ast::ExprKind::Yield(ref opt_expr) => {
ast::ExprKind::Yield(ast::YieldKind::Prefix(ref opt_expr)) => {
if let Some(ref expr) = *opt_expr {
rewrite_unary_prefix(context, "yield ", &**expr, shape)
} else {
Expand All @@ -271,7 +275,8 @@ pub(crate) fn format_expr(
ast::ExprKind::Try(..)
| ast::ExprKind::Field(..)
| ast::ExprKind::MethodCall(..)
| ast::ExprKind::Await(_, _) => rewrite_chain(expr, context, shape),
| ast::ExprKind::Await(_, _)
| ast::ExprKind::Yield(ast::YieldKind::Postfix(_)) => rewrite_chain(expr, context, shape),
ast::ExprKind::MacCall(ref mac) => {
rewrite_macro(mac, None, context, shape, MacroPosition::Expression).or_else(|_| {
wrap_str(
Expand Down
94 changes: 43 additions & 51 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,19 +327,19 @@ impl<'a> FnSig<'a> {
defaultness: ast::Defaultness,
) -> FnSig<'a> {
match *fn_kind {
visit::FnKind::Fn(visit::FnCtxt::Assoc(..), _, fn_sig, vis, generics, _) => {
let mut fn_sig = FnSig::from_method_sig(fn_sig, generics, vis);
visit::FnKind::Fn(visit::FnCtxt::Assoc(..), _, vis, ast::Fn { sig, generics, .. }) => {
let mut fn_sig = FnSig::from_method_sig(sig, generics, vis);
fn_sig.defaultness = defaultness;
fn_sig
}
visit::FnKind::Fn(_, _, fn_sig, vis, generics, _) => FnSig {
visit::FnKind::Fn(_, _, vis, ast::Fn { sig, generics, .. }) => FnSig {
decl,
generics,
ext: fn_sig.header.ext,
constness: fn_sig.header.constness,
coroutine_kind: Cow::Borrowed(&fn_sig.header.coroutine_kind),
ext: sig.header.ext,
constness: sig.header.constness,
coroutine_kind: Cow::Borrowed(&sig.header.coroutine_kind),
defaultness,
safety: fn_sig.header.safety,
safety: sig.header.safety,
visibility: vis,
},
_ => unreachable!(),
Expand Down Expand Up @@ -2343,6 +2343,21 @@ impl Rewrite for ast::Param {
}
}

fn rewrite_opt_lifetime(
context: &RewriteContext<'_>,
lifetime: Option<ast::Lifetime>,
) -> RewriteResult {
let Some(l) = lifetime else {
return Ok(String::new());
};
let mut result = l.rewrite_result(
context,
Shape::legacy(context.config.max_width(), Indent::empty()),
)?;
result.push(' ');
Ok(result)
}

fn rewrite_explicit_self(
context: &RewriteContext<'_>,
explicit_self: &ast::ExplicitSelf,
Expand All @@ -2351,58 +2366,34 @@ fn rewrite_explicit_self(
shape: Shape,
has_multiple_attr_lines: bool,
) -> RewriteResult {
match explicit_self.node {
let self_str = match explicit_self.node {
ast::SelfKind::Region(lt, m) => {
let mut_str = format_mutability(m);
match lt {
Some(ref l) => {
let lifetime_str = l.rewrite_result(
context,
Shape::legacy(context.config.max_width(), Indent::empty()),
)?;
Ok(combine_strs_with_missing_comments(
context,
param_attrs,
&format!("&{lifetime_str} {mut_str}self"),
span,
shape,
!has_multiple_attr_lines,
)?)
}
None => Ok(combine_strs_with_missing_comments(
context,
param_attrs,
&format!("&{mut_str}self"),
span,
shape,
!has_multiple_attr_lines,
)?),
}
let lifetime_str = rewrite_opt_lifetime(context, lt)?;
format!("&{lifetime_str}{mut_str}self")
}
ast::SelfKind::Pinned(lt, m) => {
let mut_str = m.ptr_str();
let lifetime_str = rewrite_opt_lifetime(context, lt)?;
format!("&{lifetime_str}pin {mut_str} self")
}
ast::SelfKind::Explicit(ref ty, mutability) => {
let type_str = ty.rewrite_result(
context,
Shape::legacy(context.config.max_width(), Indent::empty()),
)?;

Ok(combine_strs_with_missing_comments(
context,
param_attrs,
&format!("{}self: {}", format_mutability(mutability), type_str),
span,
shape,
!has_multiple_attr_lines,
)?)
format!("{}self: {}", format_mutability(mutability), type_str)
}
ast::SelfKind::Value(mutability) => Ok(combine_strs_with_missing_comments(
context,
param_attrs,
&format!("{}self", format_mutability(mutability)),
span,
shape,
!has_multiple_attr_lines,
)?),
}
ast::SelfKind::Value(mutability) => format!("{}self", format_mutability(mutability)),
};
Ok(combine_strs_with_missing_comments(
context,
param_attrs,
&self_str,
span,
shape,
!has_multiple_attr_lines,
)?)
}

pub(crate) fn span_lo_for_param(param: &ast::Param) -> BytePos {
Expand Down Expand Up @@ -3440,6 +3431,7 @@ impl Rewrite for ast::ForeignItem {
ref sig,
ref generics,
ref body,
..
} = **fn_kind;
if body.is_some() {
let mut visitor = FmtVisitor::from_context(context);
Expand All @@ -3448,7 +3440,7 @@ impl Rewrite for ast::ForeignItem {
let inner_attrs = inner_attributes(&self.attrs);
let fn_ctxt = visit::FnCtxt::Foreign;
visitor.visit_fn(
visit::FnKind::Fn(fn_ctxt, &self.ident, sig, &self.vis, generics, body),
visit::FnKind::Fn(fn_ctxt, &self.ident, &self.vis, fn_kind),
&sig.decl,
self.span,
defaultness,
Expand Down
Loading
Loading