diff options
| author | yukang <moorekang@gmail.com> | 2023-06-06 23:51:09 +0800 |
|---|---|---|
| committer | yukang <moorekang@gmail.com> | 2023-06-10 06:28:35 +0800 |
| commit | 3983881d4e00c2b12d1b5b0319b4c61d72926917 (patch) | |
| tree | accd8e6288d3730eabfb2c5e92843fa87cb712a1 /compiler/rustc_parse/src/parser | |
| parent | 43062c43d2a63cf4e261c6eddc417575c4f3062f (diff) | |
| download | rust-3983881d4e00c2b12d1b5b0319b4c61d72926917.tar.gz rust-3983881d4e00c2b12d1b5b0319b4c61d72926917.zip | |
take care module name for suggesting surround the struct literal in parentheses
Diffstat (limited to 'compiler/rustc_parse/src/parser')
| -rw-r--r-- | compiler/rustc_parse/src/parser/diagnostics.rs | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index c1454039685..2abd485b1be 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -751,10 +751,18 @@ impl<'a> Parser<'a> { tail.could_be_bare_literal = true; if maybe_struct_name.is_ident() && can_be_struct_literal { // Account for `if Example { a: one(), }.is_pos() {}`. + // expand `before` so that we take care of module path such as: + // `foo::Bar { ... } ` + // we expect to suggest `(foo::Bar { ... })` instead of `foo::(Bar { ... })` + let sm = self.sess.source_map(); + let before = maybe_struct_name.span.shrink_to_lo(); + let extend_before = sm.span_extend_prev_while(before, |t| { + t.is_alphanumeric() || t == ':' || t == '_' + }); Err(self.sess.create_err(StructLiteralNeedingParens { span: maybe_struct_name.span.to(expr.span), sugg: StructLiteralNeedingParensSugg { - before: maybe_struct_name.span.shrink_to_lo(), + before: extend_before.unwrap().shrink_to_lo(), after: expr.span.shrink_to_hi(), }, })) |
