Skip to content

fix 'fails with conditional paths that include' #3955

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

Merged
merged 4 commits into from
Dec 9, 2019
Merged
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
4 changes: 3 additions & 1 deletion src/missed_spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ impl<'a> FmtVisitor<'a> {
let snippet = self.snippet(span);

// Do nothing for spaces in the beginning of the file
if start == BytePos(0) && end.0 as usize == snippet.len() && snippet.trim().is_empty() {
let is_start_span =
(start == BytePos(0) && end.0 as usize == snippet.len()) || self.is_start_span(span);
if is_start_span && snippet.trim().is_empty() {
return;
}

Expand Down
7 changes: 6 additions & 1 deletion src/spanned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ macro_rules! span_with_attrs_lo_hi {
if attrs.is_empty() {
mk_sp($lo, $hi)
} else {
mk_sp(attrs[0].span.lo(), $hi)
// this path is already gone at latest rust-ap-* 627.0.0
if attrs[0].item.path.to_string() == "warn_directory_ownership" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, does this mean that the rustc parser is adding an attribute internally to the AST node? That is...surprising 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. But it will be deleted at the current version rust-ap-*. Maybe because it's not good for AST.

mk_sp($lo, $hi)
} else {
mk_sp(attrs[0].span.lo(), $hi)
}
}
}};
}
Expand Down
8 changes: 8 additions & 0 deletions src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ impl SnippetProvider {
Some(&self.big_snippet[start_index..end_index])
}

pub(crate) fn is_start_span(&self, span: Span) -> bool {
span.lo().to_usize() == self.start_pos
}

pub(crate) fn new(start_pos: BytePos, end_pos: BytePos, big_snippet: Rc<String>) -> Self {
let start_pos = start_pos.to_usize();
let end_pos = end_pos.to_usize();
Expand Down Expand Up @@ -751,6 +755,10 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
self.opt_snippet(span).unwrap()
}

pub(crate) fn is_start_span(&'b self, span: Span) -> bool {
self.snippet_provider.is_start_span(span)
}

// Returns true if we should skip the following item.
pub(crate) fn visit_attrs(&mut self, attrs: &[ast::Attribute], style: ast::AttrStyle) -> bool {
for attr in attrs {
Expand Down
3 changes: 3 additions & 0 deletions tests/source/issue-3933/imp-a.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod utils;
pub
struct A;
1 change: 1 addition & 0 deletions tests/source/issue-3933/imp-b.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub struct B;
4 changes: 4 additions & 0 deletions tests/source/issue-3933/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[cfg_attr(windows, path = "imp-a.rs" )]
#[cfg_attr(not(windows), path = "imp-b.rs")]
mod imp;
pub use imp::A;
4 changes: 4 additions & 0 deletions tests/source/issue-3933/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub fn fuga() {


}
2 changes: 2 additions & 0 deletions tests/target/issue-3933/imp-a.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod utils;
pub struct A;
1 change: 1 addition & 0 deletions tests/target/issue-3933/imp-b.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub struct B;
4 changes: 4 additions & 0 deletions tests/target/issue-3933/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[cfg_attr(windows, path = "imp-a.rs")]
#[cfg_attr(not(windows), path = "imp-b.rs")]
mod imp;
pub use imp::A;
1 change: 1 addition & 0 deletions tests/target/issue-3933/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub fn fuga() {}