use rustc_hir_pretty::qpath_to_string to avoid span_to_snippet when rendering path
This commit is contained in:
parent
04c590b7ef
commit
7a45a60418
9 changed files with 52 additions and 15 deletions
|
@ -211,6 +211,10 @@ pub fn path_to_string(segment: &hir::Path<'_>) -> String {
|
||||||
to_string(NO_ANN, |s| s.print_path(segment, false))
|
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(
|
pub fn fn_to_string(
|
||||||
decl: &hir::FnDecl<'_>,
|
decl: &hir::FnDecl<'_>,
|
||||||
header: hir::FnHeader,
|
header: hir::FnHeader,
|
||||||
|
|
|
@ -397,7 +397,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
= self.typeck_results.borrow().qpath_res(qpath, callee_expr.hir_id)
|
= self.typeck_results.borrow().qpath_res(qpath, callee_expr.hir_id)
|
||||||
// Only suggest removing parens if there are no arguments
|
// Only suggest removing parens if there are no arguments
|
||||||
&& arg_exprs.is_empty()
|
&& arg_exprs.is_empty()
|
||||||
&& let Ok(path) = self.tcx.sess.source_map().span_to_snippet(callee_expr.span)
|
|
||||||
{
|
{
|
||||||
let descr = match kind {
|
let descr = match kind {
|
||||||
def::CtorOf::Struct => "struct",
|
def::CtorOf::Struct => "struct",
|
||||||
|
@ -406,7 +405,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
let removal_span =
|
let removal_span =
|
||||||
callee_expr.span.shrink_to_hi().to(call_expr.span.shrink_to_hi());
|
callee_expr.span.shrink_to_hi().to(call_expr.span.shrink_to_hi());
|
||||||
unit_variant =
|
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);
|
let callee_ty = self.resolve_vars_if_possible(callee_ty);
|
||||||
|
|
|
@ -531,7 +531,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
tcx.ty_error()
|
tcx.ty_error()
|
||||||
}
|
}
|
||||||
Res::Def(DefKind::Ctor(_, CtorKind::Fictive), _) => {
|
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()
|
tcx.ty_error()
|
||||||
}
|
}
|
||||||
_ => self.instantiate_value_path(segs, opt_ty, res, expr.span, expr.hir_id).0,
|
_ => self.instantiate_value_path(segs, opt_ty, res, expr.span, expr.hir_id).0,
|
||||||
|
|
|
@ -863,17 +863,14 @@ fn bad_non_zero_sized_fields<'tcx>(
|
||||||
err.emit();
|
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!(
|
struct_span_err!(
|
||||||
tcx.sess,
|
tcx.sess,
|
||||||
span,
|
span,
|
||||||
E0533,
|
E0533,
|
||||||
"expected unit struct, unit variant or constant, found {}{}",
|
"expected unit struct, unit variant or constant, found {} `{}`",
|
||||||
res.descr(),
|
res.descr(),
|
||||||
tcx.sess
|
rustc_hir_pretty::qpath_to_string(qpath),
|
||||||
.source_map()
|
|
||||||
.span_to_snippet(span)
|
|
||||||
.map_or_else(|_| String::new(), |s| format!(" `{s}`",)),
|
|
||||||
)
|
)
|
||||||
.emit();
|
.emit();
|
||||||
}
|
}
|
||||||
|
|
|
@ -183,7 +183,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
PatKind::TupleStruct(ref qpath, subpats, ddpos) => {
|
PatKind::TupleStruct(ref qpath, subpats, ddpos) => {
|
||||||
self.check_pat_tuple_struct(pat, qpath, subpats, ddpos, expected, def_bm, ti)
|
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) => {
|
PatKind::Struct(ref qpath, fields, has_rest_pat) => {
|
||||||
self.check_pat_struct(pat, qpath, fields, has_rest_pat, expected, def_bm, ti)
|
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>(
|
fn check_pat_path<'b>(
|
||||||
&self,
|
&self,
|
||||||
pat: &Pat<'_>,
|
pat: &Pat<'_>,
|
||||||
|
qpath: &hir::QPath<'_>,
|
||||||
path_resolution: (Res, Option<Ty<'tcx>>, &'b [hir::PathSegment<'b>]),
|
path_resolution: (Res, Option<Ty<'tcx>>, &'b [hir::PathSegment<'b>]),
|
||||||
expected: Ty<'tcx>,
|
expected: Ty<'tcx>,
|
||||||
ti: TopInfo<'tcx>,
|
ti: TopInfo<'tcx>,
|
||||||
|
@ -814,7 +817,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
return tcx.ty_error();
|
return tcx.ty_error();
|
||||||
}
|
}
|
||||||
Res::Def(DefKind::AssocFn | DefKind::Ctor(_, CtorKind::Fictive | CtorKind::Fn), _) => {
|
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();
|
return tcx.ty_error();
|
||||||
}
|
}
|
||||||
Res::SelfCtor(..)
|
Res::SelfCtor(..)
|
||||||
|
|
|
@ -4,13 +4,13 @@ error[E0533]: expected unit struct, unit variant or constant, found associated f
|
||||||
LL | Foo::bar => {}
|
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
|
--> $DIR/method-path-in-pattern.rs:19:9
|
||||||
|
|
|
|
||||||
LL | <Foo>::bar => {}
|
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
|
--> $DIR/method-path-in-pattern.rs:23:9
|
||||||
|
|
|
|
||||||
LL | <Foo>::trait_bar => {}
|
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 {}
|
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
|
--> $DIR/method-path-in-pattern.rs:28:12
|
||||||
|
|
|
|
||||||
LL | if let <Foo>::bar = 0u32 {}
|
LL | if let <Foo>::bar = 0u32 {}
|
||||||
|
|
|
@ -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
|
--> $DIR/qualified-path-params.rs:20:9
|
||||||
|
|
|
|
||||||
LL | <S as Tr>::A::f::<u8> => {}
|
LL | <S as Tr>::A::f::<u8> => {}
|
||||||
|
|
10
src/test/ui/suggestions/issue-99240-2.rs
Normal file
10
src/test/ui/suggestions/issue-99240-2.rs
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
enum Enum {
|
||||||
|
Unit,
|
||||||
|
}
|
||||||
|
type Alias = Enum;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
Alias::
|
||||||
|
Unit();
|
||||||
|
//~^^ ERROR expected function, found enum variant `Alias::Unit`
|
||||||
|
}
|
24
src/test/ui/suggestions/issue-99240-2.stderr
Normal file
24
src/test/ui/suggestions/issue-99240-2.stderr
Normal 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`.
|
Loading…
Add table
Add a link
Reference in a new issue