]> git.lizzy.rs Git - rust.git/commitdiff
use rustc_hir_pretty::qpath_to_string to avoid span_to_snippet when rendering path
authorMichael Goulet <michael@errs.io>
Sun, 17 Jul 2022 04:03:30 +0000 (21:03 -0700)
committerMichael Goulet <michael@errs.io>
Sun, 17 Jul 2022 04:58:38 +0000 (04:58 +0000)
compiler/rustc_hir_pretty/src/lib.rs
compiler/rustc_typeck/src/check/callee.rs
compiler/rustc_typeck/src/check/expr.rs
compiler/rustc_typeck/src/check/mod.rs
compiler/rustc_typeck/src/check/pat.rs
src/test/ui/methods/method-path-in-pattern.stderr
src/test/ui/qualified/qualified-path-params.stderr
src/test/ui/suggestions/issue-99240-2.rs [new file with mode: 0644]
src/test/ui/suggestions/issue-99240-2.stderr [new file with mode: 0644]

index 18b671d410dc7129205ab35dac08fd187d75a829..e0179bd3ed1e85afc11a512ad3dc452dcf1a0621 100644 (file)
@@ -211,6 +211,10 @@ pub fn path_to_string(segment: &hir::Path<'_>) -> String {
     to_string(NO_ANN, |s| s.print_path(segment, false))
 }
 
+pub fn qpath_to_string(segment: &hir::QPath<'_>) -> String {
+    to_string(NO_ANN, |s| s.print_qpath(segment, false))
+}
+
 pub fn fn_to_string(
     decl: &hir::FnDecl<'_>,
     header: hir::FnHeader,
index 7996fe5f363fa75738ef2ccea7be24fff08f7ca4..00c8aa3a1bbdae7df4c5817ddea93d464b1d5add 100644 (file)
@@ -397,7 +397,6 @@ fn confirm_builtin_call(
                         = self.typeck_results.borrow().qpath_res(qpath, callee_expr.hir_id)
                     // Only suggest removing parens if there are no arguments
                     && arg_exprs.is_empty()
-                    && let Ok(path) = self.tcx.sess.source_map().span_to_snippet(callee_expr.span)
                 {
                     let descr = match kind {
                         def::CtorOf::Struct => "struct",
@@ -406,7 +405,7 @@ fn confirm_builtin_call(
                     let removal_span =
                         callee_expr.span.shrink_to_hi().to(call_expr.span.shrink_to_hi());
                     unit_variant =
-                        Some((removal_span, descr, path));
+                        Some((removal_span, descr, rustc_hir_pretty::qpath_to_string(qpath)));
                 }
 
                 let callee_ty = self.resolve_vars_if_possible(callee_ty);
index b52cb8e99d186514a4506a1458ed1f0e52bc8885..6574b542e58a59f039809943817dc702ad6173b0 100644 (file)
@@ -531,7 +531,7 @@ pub(crate) fn check_expr_path(
                 tcx.ty_error()
             }
             Res::Def(DefKind::Ctor(_, CtorKind::Fictive), _) => {
-                report_unexpected_variant_res(tcx, res, expr.span);
+                report_unexpected_variant_res(tcx, res, qpath, expr.span);
                 tcx.ty_error()
             }
             _ => self.instantiate_value_path(segs, opt_ty, res, expr.span, expr.hir_id).0,
index d6160266dd7d9f4fbff298dfec38195a6f45fe80..0dcbdef9c7703aff2760fe301f3cfb471602ce39 100644 (file)
@@ -863,17 +863,14 @@ fn bad_non_zero_sized_fields<'tcx>(
     err.emit();
 }
 
-fn report_unexpected_variant_res(tcx: TyCtxt<'_>, res: Res, span: Span) {
+fn report_unexpected_variant_res(tcx: TyCtxt<'_>, res: Res, qpath: &hir::QPath<'_>, span: Span) {
     struct_span_err!(
         tcx.sess,
         span,
         E0533,
-        "expected unit struct, unit variant or constant, found {}{}",
+        "expected unit struct, unit variant or constant, found {} `{}`",
         res.descr(),
-        tcx.sess
-            .source_map()
-            .span_to_snippet(span)
-            .map_or_else(|_| String::new(), |s| format!(" `{s}`",)),
+        rustc_hir_pretty::qpath_to_string(qpath),
     )
     .emit();
 }
index fbfbfba5c2ad21c9407105425a27467fc6a1bac8..c7318cd6e531fd955b9102847a79061a3a699d9c 100644 (file)
@@ -183,7 +183,9 @@ fn check_pat(
             PatKind::TupleStruct(ref qpath, subpats, ddpos) => {
                 self.check_pat_tuple_struct(pat, qpath, subpats, ddpos, expected, def_bm, ti)
             }
-            PatKind::Path(_) => self.check_pat_path(pat, path_res.unwrap(), expected, ti),
+            PatKind::Path(ref qpath) => {
+                self.check_pat_path(pat, qpath, path_res.unwrap(), expected, ti)
+            }
             PatKind::Struct(ref qpath, fields, has_rest_pat) => {
                 self.check_pat_struct(pat, qpath, fields, has_rest_pat, expected, def_bm, ti)
             }
@@ -800,6 +802,7 @@ fn check_pat_struct(
     fn check_pat_path<'b>(
         &self,
         pat: &Pat<'_>,
+        qpath: &hir::QPath<'_>,
         path_resolution: (Res, Option<Ty<'tcx>>, &'b [hir::PathSegment<'b>]),
         expected: Ty<'tcx>,
         ti: TopInfo<'tcx>,
@@ -814,7 +817,7 @@ fn check_pat_path<'b>(
                 return tcx.ty_error();
             }
             Res::Def(DefKind::AssocFn | DefKind::Ctor(_, CtorKind::Fictive | CtorKind::Fn), _) => {
-                report_unexpected_variant_res(tcx, res, pat.span);
+                report_unexpected_variant_res(tcx, res, qpath, pat.span);
                 return tcx.ty_error();
             }
             Res::SelfCtor(..)
index ed3c0222c7542bdd71fbc961774c7e065df9550c..1d1bdb6b052a8d7e247f0f2501cd4ad2a0331d14 100644 (file)
@@ -4,13 +4,13 @@ error[E0533]: expected unit struct, unit variant or constant, found associated f
 LL |         Foo::bar => {}
    |         ^^^^^^^^
 
-error[E0533]: expected unit struct, unit variant or constant, found associated function `<Foo>::bar`
+error[E0533]: expected unit struct, unit variant or constant, found associated function `Foo::bar`
   --> $DIR/method-path-in-pattern.rs:19:9
    |
 LL |         <Foo>::bar => {}
    |         ^^^^^^^^^^
 
-error[E0533]: expected unit struct, unit variant or constant, found associated function `<Foo>::trait_bar`
+error[E0533]: expected unit struct, unit variant or constant, found associated function `Foo::trait_bar`
   --> $DIR/method-path-in-pattern.rs:23:9
    |
 LL |         <Foo>::trait_bar => {}
@@ -22,7 +22,7 @@ error[E0533]: expected unit struct, unit variant or constant, found associated f
 LL |     if let Foo::bar = 0u32 {}
    |            ^^^^^^^^
 
-error[E0533]: expected unit struct, unit variant or constant, found associated function `<Foo>::bar`
+error[E0533]: expected unit struct, unit variant or constant, found associated function `Foo::bar`
   --> $DIR/method-path-in-pattern.rs:28:12
    |
 LL |     if let <Foo>::bar = 0u32 {}
index 2be2deeb7554942470a19e15877b1adeec9b91be..82cc6e19f9d1ea759dc9507c22d2cf7b2be68811 100644 (file)
@@ -1,4 +1,4 @@
-error[E0533]: expected unit struct, unit variant or constant, found associated function `<S as Tr>::A::f::<u8>`
+error[E0533]: expected unit struct, unit variant or constant, found associated function `<<S as Tr>::A>::f<u8>`
   --> $DIR/qualified-path-params.rs:20:9
    |
 LL |         <S as Tr>::A::f::<u8> => {}
diff --git a/src/test/ui/suggestions/issue-99240-2.rs b/src/test/ui/suggestions/issue-99240-2.rs
new file mode 100644 (file)
index 0000000..0a418b5
--- /dev/null
@@ -0,0 +1,10 @@
+enum Enum {
+    Unit,
+}
+type Alias = Enum;
+
+fn main() {
+    Alias::
+    Unit();
+    //~^^ ERROR expected function, found enum variant `Alias::Unit`
+}
diff --git a/src/test/ui/suggestions/issue-99240-2.stderr b/src/test/ui/suggestions/issue-99240-2.stderr
new file mode 100644 (file)
index 0000000..2af60f5
--- /dev/null
@@ -0,0 +1,24 @@
+error[E0618]: expected function, found enum variant `Alias::Unit`
+  --> $DIR/issue-99240-2.rs:7:5
+   |
+LL |        Unit,
+   |        ---- enum variant `Alias::Unit` defined here
+...
+LL |        Alias::
+   |   _____^
+   |  |_____|
+   | ||
+LL | ||     Unit();
+   | ||________^_- call expression requires function
+   | |_________|
+   | 
+   |
+help: `Alias::Unit` is a unit enum variant, and does not take parentheses to be constructed
+   |
+LL -     Unit();
+LL +     Unit;
+   |
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0618`.