Skip to content

Commit 7a45a60

Browse files
use rustc_hir_pretty::qpath_to_string to avoid span_to_snippet when rendering path
1 parent 04c590b commit 7a45a60

File tree

9 files changed

+52
-15
lines changed

9 files changed

+52
-15
lines changed

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ pub fn path_to_string(segment: &hir::Path<'_>) -> String {
211211
to_string(NO_ANN, |s| s.print_path(segment, false))
212212
}
213213

214+
pub fn qpath_to_string(segment: &hir::QPath<'_>) -> String {
215+
to_string(NO_ANN, |s| s.print_qpath(segment, false))
216+
}
217+
214218
pub fn fn_to_string(
215219
decl: &hir::FnDecl<'_>,
216220
header: hir::FnHeader,

compiler/rustc_typeck/src/check/callee.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
397397
= self.typeck_results.borrow().qpath_res(qpath, callee_expr.hir_id)
398398
// Only suggest removing parens if there are no arguments
399399
&& arg_exprs.is_empty()
400-
&& let Ok(path) = self.tcx.sess.source_map().span_to_snippet(callee_expr.span)
401400
{
402401
let descr = match kind {
403402
def::CtorOf::Struct => "struct",
@@ -406,7 +405,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
406405
let removal_span =
407406
callee_expr.span.shrink_to_hi().to(call_expr.span.shrink_to_hi());
408407
unit_variant =
409-
Some((removal_span, descr, path));
408+
Some((removal_span, descr, rustc_hir_pretty::qpath_to_string(qpath)));
410409
}
411410

412411
let callee_ty = self.resolve_vars_if_possible(callee_ty);

compiler/rustc_typeck/src/check/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
531531
tcx.ty_error()
532532
}
533533
Res::Def(DefKind::Ctor(_, CtorKind::Fictive), _) => {
534-
report_unexpected_variant_res(tcx, res, expr.span);
534+
report_unexpected_variant_res(tcx, res, qpath, expr.span);
535535
tcx.ty_error()
536536
}
537537
_ => self.instantiate_value_path(segs, opt_ty, res, expr.span, expr.hir_id).0,

compiler/rustc_typeck/src/check/mod.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -863,17 +863,14 @@ fn bad_non_zero_sized_fields<'tcx>(
863863
err.emit();
864864
}
865865

866-
fn report_unexpected_variant_res(tcx: TyCtxt<'_>, res: Res, span: Span) {
866+
fn report_unexpected_variant_res(tcx: TyCtxt<'_>, res: Res, qpath: &hir::QPath<'_>, span: Span) {
867867
struct_span_err!(
868868
tcx.sess,
869869
span,
870870
E0533,
871-
"expected unit struct, unit variant or constant, found {}{}",
871+
"expected unit struct, unit variant or constant, found {} `{}`",
872872
res.descr(),
873-
tcx.sess
874-
.source_map()
875-
.span_to_snippet(span)
876-
.map_or_else(|_| String::new(), |s| format!(" `{s}`",)),
873+
rustc_hir_pretty::qpath_to_string(qpath),
877874
)
878875
.emit();
879876
}

compiler/rustc_typeck/src/check/pat.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
183183
PatKind::TupleStruct(ref qpath, subpats, ddpos) => {
184184
self.check_pat_tuple_struct(pat, qpath, subpats, ddpos, expected, def_bm, ti)
185185
}
186-
PatKind::Path(_) => self.check_pat_path(pat, path_res.unwrap(), expected, ti),
186+
PatKind::Path(ref qpath) => {
187+
self.check_pat_path(pat, qpath, path_res.unwrap(), expected, ti)
188+
}
187189
PatKind::Struct(ref qpath, fields, has_rest_pat) => {
188190
self.check_pat_struct(pat, qpath, fields, has_rest_pat, expected, def_bm, ti)
189191
}
@@ -800,6 +802,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
800802
fn check_pat_path<'b>(
801803
&self,
802804
pat: &Pat<'_>,
805+
qpath: &hir::QPath<'_>,
803806
path_resolution: (Res, Option<Ty<'tcx>>, &'b [hir::PathSegment<'b>]),
804807
expected: Ty<'tcx>,
805808
ti: TopInfo<'tcx>,
@@ -814,7 +817,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
814817
return tcx.ty_error();
815818
}
816819
Res::Def(DefKind::AssocFn | DefKind::Ctor(_, CtorKind::Fictive | CtorKind::Fn), _) => {
817-
report_unexpected_variant_res(tcx, res, pat.span);
820+
report_unexpected_variant_res(tcx, res, qpath, pat.span);
818821
return tcx.ty_error();
819822
}
820823
Res::SelfCtor(..)

src/test/ui/methods/method-path-in-pattern.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ error[E0533]: expected unit struct, unit variant or constant, found associated f
44
LL | Foo::bar => {}
55
| ^^^^^^^^
66

7-
error[E0533]: expected unit struct, unit variant or constant, found associated function `<Foo>::bar`
7+
error[E0533]: expected unit struct, unit variant or constant, found associated function `Foo::bar`
88
--> $DIR/method-path-in-pattern.rs:19:9
99
|
1010
LL | <Foo>::bar => {}
1111
| ^^^^^^^^^^
1212

13-
error[E0533]: expected unit struct, unit variant or constant, found associated function `<Foo>::trait_bar`
13+
error[E0533]: expected unit struct, unit variant or constant, found associated function `Foo::trait_bar`
1414
--> $DIR/method-path-in-pattern.rs:23:9
1515
|
1616
LL | <Foo>::trait_bar => {}
@@ -22,7 +22,7 @@ error[E0533]: expected unit struct, unit variant or constant, found associated f
2222
LL | if let Foo::bar = 0u32 {}
2323
| ^^^^^^^^
2424

25-
error[E0533]: expected unit struct, unit variant or constant, found associated function `<Foo>::bar`
25+
error[E0533]: expected unit struct, unit variant or constant, found associated function `Foo::bar`
2626
--> $DIR/method-path-in-pattern.rs:28:12
2727
|
2828
LL | if let <Foo>::bar = 0u32 {}

src/test/ui/qualified/qualified-path-params.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0533]: expected unit struct, unit variant or constant, found associated function `<S as Tr>::A::f::<u8>`
1+
error[E0533]: expected unit struct, unit variant or constant, found associated function `<<S as Tr>::A>::f<u8>`
22
--> $DIR/qualified-path-params.rs:20:9
33
|
44
LL | <S as Tr>::A::f::<u8> => {}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
enum Enum {
2+
Unit,
3+
}
4+
type Alias = Enum;
5+
6+
fn main() {
7+
Alias::
8+
Unit();
9+
//~^^ ERROR expected function, found enum variant `Alias::Unit`
10+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error[E0618]: expected function, found enum variant `Alias::Unit`
2+
--> $DIR/issue-99240-2.rs:7:5
3+
|
4+
LL | Unit,
5+
| ---- enum variant `Alias::Unit` defined here
6+
...
7+
LL | Alias::
8+
| _____^
9+
| |_____|
10+
| ||
11+
LL | || Unit();
12+
| ||________^_- call expression requires function
13+
| |_________|
14+
|
15+
|
16+
help: `Alias::Unit` is a unit enum variant, and does not take parentheses to be constructed
17+
|
18+
LL - Unit();
19+
LL + Unit;
20+
|
21+
22+
error: aborting due to previous error
23+
24+
For more information about this error, try `rustc --explain E0618`.

0 commit comments

Comments
 (0)