Skip to content

Commit

Permalink
Adjust enum variant spans to exclude any explicit discriminant
Browse files Browse the repository at this point in the history
Fixes 5686

For reference, explicit discriminants were proposed in [RFC-2363].

`ast::Variant` spans extend to include explicit discriminants when they
are present.

Now we'll adjust the span of enum variants to exclude any explicit
discriminant.

[RFC-2363]: https://rust-lang.github.io/rfcs/2363-arbitrary-enum-discriminant.html
  • Loading branch information
ytmimi committed Mar 25, 2023
1 parent 34f9ca2 commit ba1f22c
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ impl<'a> FmtVisitor<'a> {
let variant_body = match field.data {
ast::VariantData::Tuple(..) | ast::VariantData::Struct(..) => format_struct(
&context,
&StructParts::from_variant(field),
&StructParts::from_variant(field, &context),
self.block_indent,
Some(one_line_width),
)?,
Expand Down Expand Up @@ -951,14 +951,14 @@ impl<'a> StructParts<'a> {
format_header(context, self.prefix, self.ident, self.vis, offset)
}

fn from_variant(variant: &'a ast::Variant) -> Self {
fn from_variant(variant: &'a ast::Variant, context: &RewriteContext<'_>) -> Self {
StructParts {
prefix: "",
ident: variant.ident,
vis: &DEFAULT_VISIBILITY,
def: &variant.data,
generics: None,
span: variant.span,
span: enum_variant_span(variant, context),
}
}

Expand All @@ -979,6 +979,25 @@ impl<'a> StructParts<'a> {
}
}

fn enum_variant_span(variant: &ast::Variant, context: &RewriteContext<'_>) -> Span {
use ast::VariantData::*;
if let Some(ref anon_const) = variant.disr_expr {
let span_before_consts = variant.span.until(anon_const.value.span);
let hi = match &variant.data {
Struct(..) => context
.snippet_provider
.span_after_last(span_before_consts, "}"),
Tuple(..) => context
.snippet_provider
.span_after_last(span_before_consts, ")"),
Unit(..) => variant.ident.span.hi(),
};
mk_sp(span_before_consts.lo(), hi)
} else {
variant.span
}
}

fn format_struct(
context: &RewriteContext<'_>,
struct_parts: &StructParts<'_>,
Expand Down
40 changes: 40 additions & 0 deletions tests/source/issue_5686.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#[repr(u8)]
enum MyEnum {
UnitWithExplicitDiscriminant = 0,
EmptyStructSingleLineBlockComment {
/* Comment */
} = 1,
EmptyStructMultiLineBlockComment {
/*
* Comment
*/
} = 2,
EmptyStructLineComment {
// comment
} = 3,
EmptyTupleSingleLineBlockComment(
/* Comment */
) = 4,
EmptyTupleMultiLineBlockComment(
/*
* Comment
*/
) = 5,
EmptyTupleLineComment(
// comment
) = 6,
}

enum Animal {
Dog(/* tuple variant closer in comment -> ) */) = 1,
#[hello(world)]
Cat(/* tuple variant close in leading attribute */) = 2,
Bee(/* tuple variant closer on associated field attribute */ #[hello(world)] usize) = 3,
Fox(/* tuple variant closer on const fn call */) = some_const_fn(),
Ant(/* tuple variant closer on macro call */) = some_macro!(),
Snake {/* stuct variant closer in comment -> } */} = 6,
#[hell{world}]
Cobra {/* struct variant close in leading attribute */} = 6,
Eagle {/* struct variant closer on associated field attribute */ #[hell{world}]value: Sting} = 7,
Koala {/* struct variant closer on macro call */} = some_macro!{}
}
42 changes: 42 additions & 0 deletions tests/target/issue_5686.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#[repr(u8)]
enum MyEnum {
UnitWithExplicitDiscriminant = 0,
EmptyStructSingleLineBlockComment {/* Comment */} = 1,
EmptyStructMultiLineBlockComment {
/*
* Comment
*/
} = 2,
EmptyStructLineComment {
// comment
} = 3,
EmptyTupleSingleLineBlockComment(/* Comment */) = 4,
EmptyTupleMultiLineBlockComment(
/*
* Comment
*/
) = 5,
EmptyTupleLineComment(
// comment
) = 6,
}

enum Animal {
Dog(/* tuple variant closer in comment -> ) */) = 1,
#[hello(world)]
Cat(/* tuple variant close in leading attribute */) = 2,
Bee(
/* tuple variant closer on associated field attribute */ #[hello(world)] usize,
) = 3,
Fox(/* tuple variant closer on const fn call */) = some_const_fn(),
Ant(/* tuple variant closer on macro call */) = some_macro!(),
Snake {/* stuct variant closer in comment -> } */} = 6,
#[hell{world}]
Cobra {/* struct variant close in leading attribute */} = 6,
Eagle {
/* struct variant closer on associated field attribute */
#[hell{world}]
value: Sting,
} = 7,
Koala {/* struct variant closer on macro call */} = some_macro! {},
}

0 comments on commit ba1f22c

Please sign in to comment.