use rustc_hir_pretty::qpath_to_string to avoid span_to_snippet when rendering path

This commit is contained in:
Michael Goulet 2022-07-16 21:03:30 -07:00
parent 04c590b7ef
commit 7a45a60418
9 changed files with 52 additions and 15 deletions

View 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,

View file

@ -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);

View file

@ -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,

View 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();
}

View file

@ -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(..)

View 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 {}

View 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> => {}

View file

@ -0,0 +1,10 @@
enum Enum {
Unit,
}
type Alias = Enum;
fn main() {
Alias::
Unit();
//~^^ ERROR expected function, found enum variant `Alias::Unit`
}

View file

@ -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`.