Skip to content
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

Fixed comment dropped between & and type issue #4482

Merged
merged 5 commits into from
Nov 3, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
93 changes: 65 additions & 28 deletions src/formatting/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use std::iter::ExactSizeIterator;
use std::ops::Deref;

use rustc_ast::ast::{self, FnRetTy, Mutability};
use rustc_span::{symbol::kw, BytePos, Span};
use rustc_span::{symbol::kw, BytePos, Pos, Span};

use crate::config::{lists::*, IndentStyle, TypeDensity};
use crate::formatting::{
comment::{combine_strs_with_missing_comments, contains_comment},
comment::{combine_strs_with_missing_comments, contains_comment, FindUncommented},
expr::{
format_expr, rewrite_assign_rhs, rewrite_call, rewrite_tuple, rewrite_unary_prefix,
ExprType,
Expand Down Expand Up @@ -653,37 +653,74 @@ impl Rewrite for ast::Ty {
ast::TyKind::Rptr(ref lifetime, ref mt) => {
let mut_str = format_mutability(mt.mutbl);
let mut_len = mut_str.len();
Some(match *lifetime {
Some(ref lifetime) => {
let lt_budget = shape.width.checked_sub(2 + mut_len)?;
let lt_str = lifetime.rewrite(
let mut result = String::with_capacity(128);
result.push_str("&");
let ref_hi = context.snippet_provider.span_after(self.span(), "&");
let mut cmnt_lo = ref_hi;

if let Some(ref lifetime) = *lifetime {
let lt_budget = shape.width.checked_sub(2 + mut_len)?;
let lt_str = lifetime.rewrite(
context,
Shape::legacy(lt_budget, shape.indent + 2 + mut_len),
)?;
let before_lt_span = mk_sp(cmnt_lo, lifetime.ident.span.lo());
if contains_comment(context.snippet(before_lt_span)) {
result = combine_strs_with_missing_comments(
context,
Shape::legacy(lt_budget, shape.indent + 2 + mut_len),
&result,
&lt_str,
before_lt_span,
shape,
true,
)?;
let lt_len = lt_str.len();
let budget = shape.width.checked_sub(2 + mut_len + lt_len)?;
format!(
"&{} {}{}",
lt_str,
mut_str,
mt.ty.rewrite(
context,
Shape::legacy(budget, shape.indent + 2 + mut_len + lt_len)
)?
)
} else {
result.push_str(&lt_str);
}
None => {
let budget = shape.width.checked_sub(1 + mut_len)?;
format!(
"&{}{}",
result.push_str(" ");
cmnt_lo = lifetime.ident.span.hi();
}

let after_lt_span = mk_sp(cmnt_lo, mt.ty.span.lo());
let snip = context.snippet(after_lt_span);
if let Some(mut_pos) = snip.find_uncommented("mut") {
let mut_lo = cmnt_lo + BytePos::from_usize(mut_pos);
let before_mut_span = mk_sp(cmnt_lo, mut_lo);
whizsid marked this conversation as resolved.
Show resolved Hide resolved
if contains_comment(context.snippet(before_mut_span)) {
result = combine_strs_with_missing_comments(
context,
result.trim_end(),
mut_str,
mt.ty.rewrite(
context,
Shape::legacy(budget, shape.indent + 1 + mut_len)
)?
)
before_mut_span,
shape,
true,
)?;
} else {
result.push_str(mut_str);
}
})
cmnt_lo = cmnt_lo + BytePos::from_usize(mut_pos + 3);
whizsid marked this conversation as resolved.
Show resolved Hide resolved
}

let before_ty_span = mk_sp(cmnt_lo, mt.ty.span.lo());
if contains_comment(context.snippet(before_ty_span)) {
result = combine_strs_with_missing_comments(
context,
result.trim_end(),
&mt.ty.rewrite(&context, shape)?,
before_ty_span,
shape,
true,
)?;
} else {
let used_width = last_line_width(&result);
let budget = shape.width.checked_sub(used_width)?;
let ty_str = mt
.ty
.rewrite(&context, Shape::legacy(budget, shape.indent + used_width))?;
result.push_str(&ty_str);
}

Some(result)
}
// FIXME: we drop any comments here, even though it's a silly place to put
// comments.
Expand Down
26 changes: 26 additions & 0 deletions tests/source/issue-4245.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@


fn a(a: & // Comment
// Another comment
'a File) {}

fn b(b: & /* Another Comment */'a File) {}

fn c(c: &'a /*Comment */ mut /*Comment */ File){}

fn d(c: & // Comment
'b // Multi Line
// Comment
mut // Multi Line
// Comment
File
) {}

fn e(c: &// Comment
File) {}

fn d(c: &// Comment
mut // Multi Line
// Comment
File
) {}
34 changes: 34 additions & 0 deletions tests/target/issue-4245.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
fn a(
a: & // Comment
// Another comment
'a File,
) {
}

fn b(b: & /* Another Comment */ 'a File) {}

fn c(c: &'a /*Comment */ mut /*Comment */ File) {}

fn d(
c: & // Comment
'b // Multi Line
// Comment
mut // Multi Line
// Comment
File,
) {
}

fn e(
c: & // Comment
File,
) {
}

fn d(
c: & // Comment
mut // Multi Line
// Comment
File,
) {
}