Skip to content

Commit 92885e3

Browse files
committed
rustc_typeck: remove rustc_hir_pretty usage
1 parent b3866a5 commit 92885e3

13 files changed

+53
-56
lines changed

Cargo.lock

-1
Original file line numberDiff line numberDiff line change
@@ -4229,7 +4229,6 @@ dependencies = [
42294229
"rustc_data_structures",
42304230
"rustc_errors",
42314231
"rustc_hir",
4232-
"rustc_hir_pretty",
42334232
"rustc_index",
42344233
"rustc_infer",
42354234
"rustc_session",

src/librustc_typeck/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ rustc_attr = { path = "../librustc_attr" }
1818
rustc_data_structures = { path = "../librustc_data_structures" }
1919
rustc_errors = { path = "../librustc_errors" }
2020
rustc_hir = { path = "../librustc_hir" }
21-
rustc_hir_pretty = { path = "../librustc_hir_pretty" }
2221
rustc_target = { path = "../librustc_target" }
2322
rustc_session = { path = "../librustc_session" }
2423
smallvec = { version = "1.0", features = ["union", "may_dangle"] }

src/librustc_typeck/astconv.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticId, Fata
2020
use rustc_hir as hir;
2121
use rustc_hir::def::{CtorOf, DefKind, Namespace, Res};
2222
use rustc_hir::def_id::DefId;
23-
use rustc_hir::intravisit::{walk_generics, Visitor};
23+
use rustc_hir::intravisit::{walk_generics, Visitor as _};
2424
use rustc_hir::{Constness, GenericArg, GenericArgs};
25-
use rustc_hir_pretty::{to_string, NO_ANN};
2625
use rustc_session::lint::builtin::{AMBIGUOUS_ASSOCIATED_ITEMS, LATE_BOUND_LIFETIME_ARGUMENTS};
2726
use rustc_session::parse::feature_err;
2827
use rustc_session::Session;
@@ -1118,6 +1117,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
11181117
if !self.tcx().features().unboxed_closures
11191118
&& trait_segment.generic_args().parenthesized != trait_def.paren_sugar
11201119
{
1120+
let sess = &self.tcx().sess.parse_sess;
11211121
// For now, require that parenthetical notation be used only with `Fn()` etc.
11221122
let (msg, sugg) = if trait_def.paren_sugar {
11231123
(
@@ -1132,7 +1132,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
11321132
.and_then(|args| args.args.get(0))
11331133
.and_then(|arg| match arg {
11341134
hir::GenericArg::Type(ty) => {
1135-
Some(to_string(NO_ANN, |s| s.print_type(ty)))
1135+
sess.source_map().span_to_snippet(ty.span).ok()
11361136
}
11371137
_ => None,
11381138
})
@@ -1143,7 +1143,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
11431143
.iter()
11441144
.filter_map(|b| match (b.ident.as_str() == "Output", &b.kind) {
11451145
(true, hir::TypeBindingKind::Equality { ty }) => {
1146-
Some(to_string(NO_ANN, |s| s.print_type(ty)))
1146+
sess.source_map().span_to_snippet(ty.span).ok()
11471147
}
11481148
_ => None,
11491149
})
@@ -1154,7 +1154,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
11541154
} else {
11551155
("parenthetical notation is only stable when used with `Fn`-family traits", None)
11561156
};
1157-
let sess = &self.tcx().sess.parse_sess;
11581157
let mut err = feature_err(sess, sym::unboxed_closures, span, msg);
11591158
if let Some(sugg) = sugg {
11601159
let msg = "use parenthetical notation instead";

src/librustc_typeck/check/demand.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use rustc_ast::util::parser::PREC_POSTFIX;
1010
use rustc_errors::{Applicability, DiagnosticBuilder};
1111
use rustc_hir as hir;
1212
use rustc_hir::{is_range_literal, Node};
13-
use rustc_hir_pretty::{to_string, NO_ANN};
1413
use rustc_span::symbol::sym;
1514
use rustc_span::Span;
1615

@@ -199,15 +198,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
199198
.peekable();
200199

201200
if compatible_variants.peek().is_some() {
202-
let expr_text = self
203-
.tcx
204-
.sess
205-
.source_map()
206-
.span_to_snippet(expr.span)
207-
.unwrap_or_else(|_| to_string(NO_ANN, |s| s.print_expr(expr)));
208-
let suggestions = compatible_variants.map(|v| format!("{}({})", v, expr_text));
209-
let msg = "try using a variant of the expected enum";
210-
err.span_suggestions(expr.span, msg, suggestions, Applicability::MaybeIncorrect);
201+
if let Ok(expr_text) = self.tcx.sess.source_map().span_to_snippet(expr.span) {
202+
let suggestions = compatible_variants.map(|v| format!("{}({})", v, expr_text));
203+
let msg = "try using a variant of the expected enum";
204+
err.span_suggestions(
205+
expr.span,
206+
msg,
207+
suggestions,
208+
Applicability::MaybeIncorrect,
209+
);
210+
}
211211
}
212212
}
213213
}

src/librustc_typeck/check/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
475475
tcx.types.err
476476
}
477477
Res::Def(DefKind::Ctor(_, CtorKind::Fictive), _) => {
478-
report_unexpected_variant_res(tcx, res, expr.span, qpath);
478+
report_unexpected_variant_res(tcx, res, expr.span);
479479
tcx.types.err
480480
}
481481
_ => self.instantiate_value_path(segs, opt_ty, res, expr.span, expr.hir_id).0,

src/librustc_typeck/check/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2656,14 +2656,14 @@ pub fn check_enum<'tcx>(
26562656
check_transparent(tcx, sp, def_id);
26572657
}
26582658

2659-
fn report_unexpected_variant_res(tcx: TyCtxt<'_>, res: Res, span: Span, qpath: &QPath<'_>) {
2659+
fn report_unexpected_variant_res(tcx: TyCtxt<'_>, res: Res, span: Span) {
26602660
struct_span_err!(
26612661
tcx.sess,
26622662
span,
26632663
E0533,
2664-
"expected unit struct, unit variant or constant, found {} `{}`",
2664+
"expected unit struct, unit variant or constant, found {}{}",
26652665
res.descr(),
2666-
rustc_hir_pretty::to_string(&tcx.hir(), |s| s.print_qpath(qpath, false))
2666+
tcx.sess.source_map().span_to_snippet(span).map_or(String::new(), |s| format!(" `{}`", s)),
26672667
)
26682668
.emit();
26692669
}

src/librustc_typeck/check/pat.rs

+20-17
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
171171
PatKind::TupleStruct(ref qpath, subpats, ddpos) => {
172172
self.check_pat_tuple_struct(pat, qpath, subpats, ddpos, expected, def_bm, ti)
173173
}
174-
PatKind::Path(ref qpath) => {
175-
self.check_pat_path(pat, path_res.unwrap(), qpath, expected, ti)
176-
}
174+
PatKind::Path(_) => self.check_pat_path(pat, path_res.unwrap(), expected, ti),
177175
PatKind::Struct(ref qpath, fields, etc) => {
178176
self.check_pat_struct(pat, qpath, fields, etc, expected, def_bm, ti)
179177
}
@@ -694,7 +692,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
694692
&self,
695693
pat: &Pat<'_>,
696694
path_resolution: (Res, Option<Ty<'tcx>>, &'b [hir::PathSegment<'b>]),
697-
qpath: &hir::QPath<'_>,
698695
expected: Ty<'tcx>,
699696
ti: TopInfo<'tcx>,
700697
) -> Ty<'tcx> {
@@ -707,17 +704,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
707704
self.set_tainted_by_errors();
708705
return tcx.types.err;
709706
}
710-
Res::Def(DefKind::AssocFn, _)
711-
| Res::Def(DefKind::Ctor(_, CtorKind::Fictive), _)
712-
| Res::Def(DefKind::Ctor(_, CtorKind::Fn), _) => {
713-
report_unexpected_variant_res(tcx, res, pat.span, qpath);
707+
Res::Def(DefKind::AssocFn | DefKind::Ctor(_, CtorKind::Fictive | CtorKind::Fn), _) => {
708+
report_unexpected_variant_res(tcx, res, pat.span);
714709
return tcx.types.err;
715710
}
716-
Res::Def(DefKind::Ctor(_, CtorKind::Const), _)
717-
| Res::SelfCtor(..)
718-
| Res::Def(DefKind::Const, _)
719-
| Res::Def(DefKind::AssocConst, _)
720-
| Res::Def(DefKind::ConstParam, _) => {} // OK
711+
Res::SelfCtor(..)
712+
| Res::Def(
713+
DefKind::Ctor(_, CtorKind::Const)
714+
| DefKind::Const
715+
| DefKind::AssocConst
716+
| DefKind::ConstParam,
717+
_,
718+
) => {} // OK
721719
_ => bug!("unexpected pattern resolution: {:?}", res),
722720
}
723721

@@ -791,14 +789,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
791789
}
792790
};
793791
let report_unexpected_res = |res: Res| {
792+
let sm = tcx.sess.source_map();
793+
let path_str = sm
794+
.span_to_snippet(sm.span_until_char(pat.span, '('))
795+
.map_or(String::new(), |s| format!(" `{}`", s.trim_end()));
794796
let msg = format!(
795-
"expected tuple struct or tuple variant, found {} `{}`",
797+
"expected tuple struct or tuple variant, found {}{}",
796798
res.descr(),
797-
rustc_hir_pretty::to_string(&tcx.hir(), |s| s.print_qpath(qpath, false)),
799+
path_str
798800
);
801+
799802
let mut err = struct_span_err!(tcx.sess, pat.span, E0164, "{}", msg);
800-
match (res, &pat.kind) {
801-
(Res::Def(DefKind::Fn, _), _) | (Res::Def(DefKind::AssocFn, _), _) => {
803+
match res {
804+
Res::Def(DefKind::Fn | DefKind::AssocFn, _) => {
802805
err.span_label(pat.span, "`fn` calls are not allowed in patterns");
803806
err.help(
804807
"for more information, visit \

src/librustc_typeck/check_unused.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use rustc_errors::Applicability;
55
use rustc_hir as hir;
66
use rustc_hir::def_id::{DefId, DefIdSet, LOCAL_CRATE};
77
use rustc_hir::itemlikevisit::ItemLikeVisitor;
8-
use rustc_hir_pretty::visibility_qualified;
98
use rustc_session::lint;
109
use rustc_span::Span;
1110

@@ -176,16 +175,13 @@ fn unused_crates_lint(tcx: TyCtxt<'_>) {
176175
Some(orig_name) => format!("use {} as {};", orig_name, item.ident.name),
177176
None => format!("use {};", item.ident.name),
178177
};
179-
180-
let replacement = visibility_qualified(&item.vis, base_replacement);
181-
let msg = "`extern crate` is not idiomatic in the new edition";
182-
let help = format!("convert it to a `{}`", visibility_qualified(&item.vis, "use"));
183-
184-
lint.build(msg)
178+
let vis = tcx.sess.source_map().span_to_snippet(item.vis.span).unwrap_or_default();
179+
let add_vis = |to| if vis.is_empty() { to } else { format!("{} {}", vis, to) };
180+
lint.build("`extern crate` is not idiomatic in the new edition")
185181
.span_suggestion_short(
186182
extern_crate.span,
187-
&help,
188-
replacement,
183+
&format!("convert it to a `{}`", add_vis("use".to_string())),
184+
add_vis(base_replacement),
189185
Applicability::MachineApplicable,
190186
)
191187
.emit();

src/librustc_typeck/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ This API is completely unstable and subject to change.
6262
#![feature(crate_visibility_modifier)]
6363
#![feature(in_band_lifetimes)]
6464
#![feature(nll)]
65+
#![feature(or_patterns)]
6566
#![feature(try_blocks)]
6667
#![feature(never_type)]
6768
#![feature(slice_partition_dedup)]

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

+3-3
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/privacy/associated-item-privacy-trait.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ mod priv_trait {
2121
Pub.method();
2222
//~^ ERROR type `for<'r> fn(&'r Self) {<Self as priv_trait::PrivTr>::method}` is private
2323
<Pub as PrivTr>::CONST;
24-
//~^ ERROR associated constant `PrivTr::CONST` is private
24+
//~^ ERROR associated constant `<Pub as PrivTr>::CONST` is private
2525
let _: <Pub as PrivTr>::AssocTy;
26-
//~^ ERROR associated type `PrivTr::AssocTy` is private
26+
//~^ ERROR associated type `<Pub as PrivTr>::AssocTy` is private
2727
pub type InSignatureTy = <Pub as PrivTr>::AssocTy;
2828
//~^ ERROR trait `priv_trait::PrivTr` is private
2929
pub trait InSignatureTr: PrivTr {}
@@ -115,7 +115,7 @@ mod priv_parent_substs {
115115
<Priv as PubTr<_>>::CONST;
116116
//~^ ERROR type `priv_parent_substs::Priv` is private
117117

118-
let _: <Pub as PubTr>::AssocTy; // FIXME no longer an error?!
118+
let _: <Pub as PubTr>::AssocTy; // FIXME no longer an error?!
119119
let _: <Pub as PubTr<_>>::AssocTy;
120120
//~^ ERROR type `priv_parent_substs::Priv` is private
121121
let _: <Priv as PubTr<_>>::AssocTy;

src/test/ui/privacy/associated-item-privacy-trait.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ LL | priv_trait::mac!();
3131
|
3232
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
3333

34-
error: associated constant `PrivTr::CONST` is private
34+
error: associated constant `<Pub as PrivTr>::CONST` is private
3535
--> $DIR/associated-item-privacy-trait.rs:23:9
3636
|
3737
LL | <Pub as PrivTr>::CONST;
@@ -42,7 +42,7 @@ LL | priv_trait::mac!();
4242
|
4343
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
4444

45-
error: associated type `PrivTr::AssocTy` is private
45+
error: associated type `<Pub as PrivTr>::AssocTy` is private
4646
--> $DIR/associated-item-privacy-trait.rs:25:16
4747
|
4848
LL | let _: <Pub as PrivTr>::AssocTy;

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

+1-1
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> => {}

0 commit comments

Comments
 (0)