Skip to content

Commit a178776

Browse files
authored
fix 'fails with conditional paths that include' (#3955)
2 parents 04a37b5 + 61c1601 commit a178776

File tree

11 files changed

+37
-2
lines changed

11 files changed

+37
-2
lines changed

src/missed_spans.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ impl<'a> FmtVisitor<'a> {
9595
let snippet = self.snippet(span);
9696

9797
// Do nothing for spaces in the beginning of the file
98-
if start == BytePos(0) && end.0 as usize == snippet.len() && snippet.trim().is_empty() {
98+
let is_start_span =
99+
(start == BytePos(0) && end.0 as usize == snippet.len()) || self.is_start_span(span);
100+
if is_start_span && snippet.trim().is_empty() {
99101
return;
100102
}
101103

src/spanned.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ macro_rules! span_with_attrs_lo_hi {
3131
if attrs.is_empty() {
3232
mk_sp($lo, $hi)
3333
} else {
34-
mk_sp(attrs[0].span.lo(), $hi)
34+
// this path is already gone at latest rust-ap-* 627.0.0
35+
if attrs[0].item.path.to_string() == "warn_directory_ownership" {
36+
mk_sp($lo, $hi)
37+
} else {
38+
mk_sp(attrs[0].span.lo(), $hi)
39+
}
3540
}
3641
}};
3742
}

src/visitor.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ impl SnippetProvider {
4646
Some(&self.big_snippet[start_index..end_index])
4747
}
4848

49+
pub(crate) fn is_start_span(&self, span: Span) -> bool {
50+
span.lo().to_usize() == self.start_pos
51+
}
52+
4953
pub(crate) fn new(start_pos: BytePos, end_pos: BytePos, big_snippet: Rc<String>) -> Self {
5054
let start_pos = start_pos.to_usize();
5155
let end_pos = end_pos.to_usize();
@@ -751,6 +755,10 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
751755
self.opt_snippet(span).unwrap()
752756
}
753757

758+
pub(crate) fn is_start_span(&'b self, span: Span) -> bool {
759+
self.snippet_provider.is_start_span(span)
760+
}
761+
754762
// Returns true if we should skip the following item.
755763
pub(crate) fn visit_attrs(&mut self, attrs: &[ast::Attribute], style: ast::AttrStyle) -> bool {
756764
for attr in attrs {

tests/source/issue-3933/imp-a.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mod utils;
2+
pub
3+
struct A;

tests/source/issue-3933/imp-b.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub struct B;

tests/source/issue-3933/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[cfg_attr(windows, path = "imp-a.rs" )]
2+
#[cfg_attr(not(windows), path = "imp-b.rs")]
3+
mod imp;
4+
pub use imp::A;

tests/source/issue-3933/utils.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pub fn fuga() {
2+
3+
4+
}

tests/target/issue-3933/imp-a.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mod utils;
2+
pub struct A;

tests/target/issue-3933/imp-b.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub struct B;

tests/target/issue-3933/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[cfg_attr(windows, path = "imp-a.rs")]
2+
#[cfg_attr(not(windows), path = "imp-b.rs")]
3+
mod imp;
4+
pub use imp::A;

tests/target/issue-3933/utils.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub fn fuga() {}

0 commit comments

Comments
 (0)