Skip to content

Commit

Permalink
Fix tools
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Sep 6, 2024
1 parent 5054e8c commit b6a86be
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/librustdoc/html/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,9 @@ impl<'src> Classifier<'src> {
| TokenKind::UnknownPrefix
| TokenKind::InvalidPrefix
| TokenKind::InvalidIdent => Class::Ident(self.new_span(before, text)),
TokenKind::Lifetime { .. } => Class::Lifetime,
TokenKind::Lifetime { .. }
| TokenKind::RawLifetime
| TokenKind::UnknownPrefixLifetime => Class::Lifetime,
TokenKind::Eof => panic!("Eof in advance"),
};
// Anything that didn't return above is the simple case where we the
Expand Down
7 changes: 7 additions & 0 deletions src/tools/rust-analyzer/crates/parser/src/lexed_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ impl<'a> Converter<'a> {
}
LIFETIME_IDENT
}
rustc_lexer::TokenKind::UnknownPrefixLifetime => {
err = "Unknown lifetime prefix";
LIFETIME_IDENT
}
rustc_lexer::TokenKind::RawLifetime => {
LIFETIME_IDENT
}

rustc_lexer::TokenKind::Semi => T![;],
rustc_lexer::TokenKind::Comma => T![,],
Expand Down
12 changes: 6 additions & 6 deletions src/tools/rustfmt/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ fn rewrite_empty_block(
return None;
}

let label_str = rewrite_label(label);
let label_str = rewrite_label(context, label);
if attrs.map_or(false, |a| !inner_attributes(a).is_empty()) {
return None;
}
Expand Down Expand Up @@ -527,7 +527,7 @@ fn rewrite_single_line_block(
if let Some(block_expr) = stmt::Stmt::from_simple_block(context, block, attrs) {
let expr_shape = shape.offset_left(last_line_width(prefix))?;
let expr_str = block_expr.rewrite(context, expr_shape)?;
let label_str = rewrite_label(label);
let label_str = rewrite_label(context, label);
let result = format!("{prefix}{label_str}{{ {expr_str} }}");
if result.len() <= shape.width && !result.contains('\n') {
return Some(result);
Expand Down Expand Up @@ -562,7 +562,7 @@ pub(crate) fn rewrite_block_with_visitor(
}

let inner_attrs = attrs.map(inner_attributes);
let label_str = rewrite_label(label);
let label_str = rewrite_label(context, label);
visitor.visit_block(block, inner_attrs.as_deref(), has_braces);
let visitor_context = visitor.get_context();
context
Expand Down Expand Up @@ -939,7 +939,7 @@ impl<'a> ControlFlow<'a> {
fresh_shape
};

let label_string = rewrite_label(self.label);
let label_string = rewrite_label(context, self.label);
// 1 = space after keyword.
let offset = self.keyword.len() + label_string.len() + 1;

Expand Down Expand Up @@ -1168,9 +1168,9 @@ impl<'a> Rewrite for ControlFlow<'a> {
}
}

fn rewrite_label(opt_label: Option<ast::Label>) -> Cow<'static, str> {
fn rewrite_label(context: &RewriteContext<'_>, opt_label: Option<ast::Label>) -> Cow<'static, str> {
match opt_label {
Some(label) => Cow::from(format!("{}: ", label.ident)),
Some(label) => Cow::from(format!("{}: ", context.snippet(label.ident.span))),
None => Cow::from(""),
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/tools/rustfmt/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1074,7 +1074,7 @@ fn force_space_before(tok: &TokenKind) -> bool {
fn ident_like(tok: &Token) -> bool {
matches!(
tok.kind,
TokenKind::Ident(..) | TokenKind::Literal(..) | TokenKind::Lifetime(_)
TokenKind::Ident(..) | TokenKind::Literal(..) | TokenKind::Lifetime(..)
)
}

Expand All @@ -1099,7 +1099,9 @@ fn next_space(tok: &TokenKind) -> SpaceState {
| TokenKind::OpenDelim(_)
| TokenKind::CloseDelim(_) => SpaceState::Never,

TokenKind::Literal(..) | TokenKind::Ident(..) | TokenKind::Lifetime(_) => SpaceState::Ident,
TokenKind::Literal(..) | TokenKind::Ident(..) | TokenKind::Lifetime(..) => {
SpaceState::Ident
}

_ => SpaceState::Always,
}
Expand Down
2 changes: 1 addition & 1 deletion src/tools/rustfmt/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ impl Rewrite for ast::AnonConst {

impl Rewrite for ast::Lifetime {
fn rewrite(&self, context: &RewriteContext<'_>, _: Shape) -> Option<String> {
Some(rewrite_ident(context, self.ident).to_owned())
Some(context.snippet(self.ident.span).to_owned())
}
}

Expand Down
15 changes: 15 additions & 0 deletions src/tools/rustfmt/tests/target/raw-lifetimes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// rustfmt-edition: 2021

// Simple idempotence test for raw lifetimes.

fn test<'r#gen>() -> &'r#gen () {
// Test raw lifetimes...
}

fn label() {
'r#label: {
// Test raw labels.
}
}

fn main() {}

0 comments on commit b6a86be

Please sign in to comment.