From e81c393663daab11238944cddfbd97541630f178 Mon Sep 17 00:00:00 2001 From: Ashvin Arsakularatne Date: Thu, 26 Aug 2021 00:21:29 +0530 Subject: [PATCH] fix: remove wrong reformatting of qualified paths in struct patterns --- src/patterns.rs | 7 ++++--- tests/target/issue-4908-2.rs | 20 ++++++++++++++++++++ tests/target/issue-4908.rs | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 tests/target/issue-4908-2.rs create mode 100644 tests/target/issue-4908.rs diff --git a/src/patterns.rs b/src/patterns.rs index 062e9cef9bb..0c6a6f3e814 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -244,8 +244,8 @@ impl Rewrite for Pat { .collect(); Some(format!("[{}]", rw.join(", "))) } - PatKind::Struct(_, ref path, ref fields, ellipsis) => { - rewrite_struct_pat(path, fields, ellipsis, self.span, context, shape) + PatKind::Struct(ref qself, ref path, ref fields, ellipsis) => { + rewrite_struct_pat(qself, path, fields, ellipsis, self.span, context, shape) } PatKind::MacCall(ref mac) => { rewrite_macro(mac, None, context, shape, MacroPosition::Pat) @@ -258,6 +258,7 @@ impl Rewrite for Pat { } fn rewrite_struct_pat( + qself: &Option, path: &ast::Path, fields: &[ast::PatField], ellipsis: bool, @@ -267,7 +268,7 @@ fn rewrite_struct_pat( ) -> Option { // 2 = ` {` let path_shape = shape.sub_width(2)?; - let path_str = rewrite_path(context, PathContext::Expr, None, path, path_shape)?; + let path_str = rewrite_path(context, PathContext::Expr, qself.as_ref(), path, path_shape)?; if fields.is_empty() && !ellipsis { return Some(format!("{} {{}}", path_str)); diff --git a/tests/target/issue-4908-2.rs b/tests/target/issue-4908-2.rs new file mode 100644 index 00000000000..023b323cb27 --- /dev/null +++ b/tests/target/issue-4908-2.rs @@ -0,0 +1,20 @@ +#![feature(more_qualified_paths)] + +fn main() { + // destructure through a qualified path + let ::Assoc { br } = StructStruct { br: 2 }; +} + +struct StructStruct { + br: i8, +} + +struct Foo; + +trait A { + type Assoc; +} + +impl A for Foo { + type Assoc = StructStruct; +} diff --git a/tests/target/issue-4908.rs b/tests/target/issue-4908.rs new file mode 100644 index 00000000000..ac5357abe2a --- /dev/null +++ b/tests/target/issue-4908.rs @@ -0,0 +1,34 @@ +#![feature(more_qualified_paths)] + +mod foo_bar { + pub enum Example { + Example1 {}, + Example2 {}, + } +} + +fn main() { + foo!(crate::foo_bar::Example, Example1); + + let i1 = foo_bar::Example::Example1 {}; + + assert_eq!(i1.foo_example(), 1); + + let i2 = foo_bar::Example::Example2 {}; + + assert_eq!(i2.foo_example(), 2); +} + +#[macro_export] +macro_rules! foo { + ($struct:path, $variant:ident) => { + impl $struct { + pub fn foo_example(&self) -> i32 { + match self { + <$struct>::$variant { .. } => 1, + _ => 2, + } + } + } + }; +}