diff options
| -rw-r--r-- | compiler/rustc_hir_pretty/src/lib.rs | 4 | ||||
| -rw-r--r-- | compiler/rustc_typeck/src/check/callee.rs | 3 | ||||
| -rw-r--r-- | compiler/rustc_typeck/src/check/expr.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_typeck/src/check/mod.rs | 9 | ||||
| -rw-r--r-- | compiler/rustc_typeck/src/check/pat.rs | 7 | ||||
| -rw-r--r-- | src/test/ui/methods/method-path-in-pattern.stderr | 6 | ||||
| -rw-r--r-- | src/test/ui/qualified/qualified-path-params.stderr | 2 | ||||
| -rw-r--r-- | src/test/ui/suggestions/issue-99240-2.rs | 10 | ||||
| -rw-r--r-- | src/test/ui/suggestions/issue-99240-2.stderr | 24 |
9 files changed, 52 insertions, 15 deletions
diff --git a/compiler/rustc_hir_pretty/src/lib.rs b/compiler/rustc_hir_pretty/src/lib.rs index 18b671d410d..e0179bd3ed1 100644 --- a/compiler/rustc_hir_pretty/src/lib.rs +++ b/compiler/rustc_hir_pretty/src/lib.rs @@ -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, diff --git a/compiler/rustc_typeck/src/check/callee.rs b/compiler/rustc_typeck/src/check/callee.rs index 7996fe5f363..00c8aa3a1bb 100644 --- a/compiler/rustc_typeck/src/check/callee.rs +++ b/compiler/rustc_typeck/src/check/callee.rs @@ -397,7 +397,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { = 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 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { 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); diff --git a/compiler/rustc_typeck/src/check/expr.rs b/compiler/rustc_typeck/src/check/expr.rs index b52cb8e99d1..6574b542e58 100644 --- a/compiler/rustc_typeck/src/check/expr.rs +++ b/compiler/rustc_typeck/src/check/expr.rs @@ -531,7 +531,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { 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, diff --git a/compiler/rustc_typeck/src/check/mod.rs b/compiler/rustc_typeck/src/check/mod.rs index d6160266dd7..0dcbdef9c77 100644 --- a/compiler/rustc_typeck/src/check/mod.rs +++ b/compiler/rustc_typeck/src/check/mod.rs @@ -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(); } diff --git a/compiler/rustc_typeck/src/check/pat.rs b/compiler/rustc_typeck/src/check/pat.rs index fbfbfba5c2a..c7318cd6e53 100644 --- a/compiler/rustc_typeck/src/check/pat.rs +++ b/compiler/rustc_typeck/src/check/pat.rs @@ -183,7 +183,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { 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 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { 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 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { 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(..) diff --git a/src/test/ui/methods/method-path-in-pattern.stderr b/src/test/ui/methods/method-path-in-pattern.stderr index ed3c0222c75..1d1bdb6b052 100644 --- a/src/test/ui/methods/method-path-in-pattern.stderr +++ b/src/test/ui/methods/method-path-in-pattern.stderr @@ -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 {} diff --git a/src/test/ui/qualified/qualified-path-params.stderr b/src/test/ui/qualified/qualified-path-params.stderr index 2be2deeb755..82cc6e19f9d 100644 --- a/src/test/ui/qualified/qualified-path-params.stderr +++ b/src/test/ui/qualified/qualified-path-params.stderr @@ -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 index 00000000000..0a418b5aeef --- /dev/null +++ b/src/test/ui/suggestions/issue-99240-2.rs @@ -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 index 00000000000..2af60f59759 --- /dev/null +++ b/src/test/ui/suggestions/issue-99240-2.stderr @@ -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`. |
