From 92885e3a5bc855c24e302154635107c848f8b585 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Mon, 23 Mar 2020 22:39:59 +0100 Subject: [PATCH] rustc_typeck: remove rustc_hir_pretty usage --- Cargo.lock | 1 - src/librustc_typeck/Cargo.toml | 1 - src/librustc_typeck/astconv.rs | 9 ++--- src/librustc_typeck/check/demand.rs | 20 +++++----- src/librustc_typeck/check/expr.rs | 2 +- src/librustc_typeck/check/mod.rs | 6 +-- src/librustc_typeck/check/pat.rs | 37 ++++++++++--------- src/librustc_typeck/check_unused.rs | 14 +++---- src/librustc_typeck/lib.rs | 1 + .../ui/methods/method-path-in-pattern.stderr | 6 +-- .../privacy/associated-item-privacy-trait.rs | 6 +-- .../associated-item-privacy-trait.stderr | 4 +- .../ui/qualified/qualified-path-params.stderr | 2 +- 13 files changed, 53 insertions(+), 56 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 02427ecb4b9..549b82e40b2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4229,7 +4229,6 @@ dependencies = [ "rustc_data_structures", "rustc_errors", "rustc_hir", - "rustc_hir_pretty", "rustc_index", "rustc_infer", "rustc_session", diff --git a/src/librustc_typeck/Cargo.toml b/src/librustc_typeck/Cargo.toml index a76f920ce9b..e61a36f844f 100644 --- a/src/librustc_typeck/Cargo.toml +++ b/src/librustc_typeck/Cargo.toml @@ -18,7 +18,6 @@ rustc_attr = { path = "../librustc_attr" } rustc_data_structures = { path = "../librustc_data_structures" } rustc_errors = { path = "../librustc_errors" } rustc_hir = { path = "../librustc_hir" } -rustc_hir_pretty = { path = "../librustc_hir_pretty" } rustc_target = { path = "../librustc_target" } rustc_session = { path = "../librustc_session" } smallvec = { version = "1.0", features = ["union", "may_dangle"] } diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index be515d763cc..432e7545b9e 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -20,9 +20,8 @@ use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticId, Fata use rustc_hir as hir; use rustc_hir::def::{CtorOf, DefKind, Namespace, Res}; use rustc_hir::def_id::DefId; -use rustc_hir::intravisit::{walk_generics, Visitor}; +use rustc_hir::intravisit::{walk_generics, Visitor as _}; use rustc_hir::{Constness, GenericArg, GenericArgs}; -use rustc_hir_pretty::{to_string, NO_ANN}; use rustc_session::lint::builtin::{AMBIGUOUS_ASSOCIATED_ITEMS, LATE_BOUND_LIFETIME_ARGUMENTS}; use rustc_session::parse::feature_err; use rustc_session::Session; @@ -1118,6 +1117,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { if !self.tcx().features().unboxed_closures && trait_segment.generic_args().parenthesized != trait_def.paren_sugar { + let sess = &self.tcx().sess.parse_sess; // For now, require that parenthetical notation be used only with `Fn()` etc. let (msg, sugg) = if trait_def.paren_sugar { ( @@ -1132,7 +1132,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { .and_then(|args| args.args.get(0)) .and_then(|arg| match arg { hir::GenericArg::Type(ty) => { - Some(to_string(NO_ANN, |s| s.print_type(ty))) + sess.source_map().span_to_snippet(ty.span).ok() } _ => None, }) @@ -1143,7 +1143,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { .iter() .filter_map(|b| match (b.ident.as_str() == "Output", &b.kind) { (true, hir::TypeBindingKind::Equality { ty }) => { - Some(to_string(NO_ANN, |s| s.print_type(ty))) + sess.source_map().span_to_snippet(ty.span).ok() } _ => None, }) @@ -1154,7 +1154,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { } else { ("parenthetical notation is only stable when used with `Fn`-family traits", None) }; - let sess = &self.tcx().sess.parse_sess; let mut err = feature_err(sess, sym::unboxed_closures, span, msg); if let Some(sugg) = sugg { let msg = "use parenthetical notation instead"; diff --git a/src/librustc_typeck/check/demand.rs b/src/librustc_typeck/check/demand.rs index 40c056a7641..f7ffb5a2218 100644 --- a/src/librustc_typeck/check/demand.rs +++ b/src/librustc_typeck/check/demand.rs @@ -10,7 +10,6 @@ use rustc_ast::util::parser::PREC_POSTFIX; use rustc_errors::{Applicability, DiagnosticBuilder}; use rustc_hir as hir; use rustc_hir::{is_range_literal, Node}; -use rustc_hir_pretty::{to_string, NO_ANN}; use rustc_span::symbol::sym; use rustc_span::Span; @@ -199,15 +198,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { .peekable(); if compatible_variants.peek().is_some() { - let expr_text = self - .tcx - .sess - .source_map() - .span_to_snippet(expr.span) - .unwrap_or_else(|_| to_string(NO_ANN, |s| s.print_expr(expr))); - let suggestions = compatible_variants.map(|v| format!("{}({})", v, expr_text)); - let msg = "try using a variant of the expected enum"; - err.span_suggestions(expr.span, msg, suggestions, Applicability::MaybeIncorrect); + if let Ok(expr_text) = self.tcx.sess.source_map().span_to_snippet(expr.span) { + let suggestions = compatible_variants.map(|v| format!("{}({})", v, expr_text)); + let msg = "try using a variant of the expected enum"; + err.span_suggestions( + expr.span, + msg, + suggestions, + Applicability::MaybeIncorrect, + ); + } } } } diff --git a/src/librustc_typeck/check/expr.rs b/src/librustc_typeck/check/expr.rs index 4e11246ac72..ae4750f9fd0 100644 --- a/src/librustc_typeck/check/expr.rs +++ b/src/librustc_typeck/check/expr.rs @@ -475,7 +475,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { tcx.types.err } Res::Def(DefKind::Ctor(_, CtorKind::Fictive), _) => { - report_unexpected_variant_res(tcx, res, expr.span, qpath); + report_unexpected_variant_res(tcx, res, expr.span); tcx.types.err } _ => self.instantiate_value_path(segs, opt_ty, res, expr.span, expr.hir_id).0, diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 7855b4b761d..754cd7b160a 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -2656,14 +2656,14 @@ pub fn check_enum<'tcx>( check_transparent(tcx, sp, def_id); } -fn report_unexpected_variant_res(tcx: TyCtxt<'_>, res: Res, span: Span, qpath: &QPath<'_>) { +fn report_unexpected_variant_res(tcx: TyCtxt<'_>, res: Res, 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(), - rustc_hir_pretty::to_string(&tcx.hir(), |s| s.print_qpath(qpath, false)) + tcx.sess.source_map().span_to_snippet(span).map_or(String::new(), |s| format!(" `{}`", s)), ) .emit(); } diff --git a/src/librustc_typeck/check/pat.rs b/src/librustc_typeck/check/pat.rs index d2290d4a583..17a6e0544a1 100644 --- a/src/librustc_typeck/check/pat.rs +++ b/src/librustc_typeck/check/pat.rs @@ -171,9 +171,7 @@ 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(ref qpath) => { - self.check_pat_path(pat, path_res.unwrap(), qpath, expected, ti) - } + PatKind::Path(_) => self.check_pat_path(pat, path_res.unwrap(), expected, ti), PatKind::Struct(ref qpath, fields, etc) => { self.check_pat_struct(pat, qpath, fields, etc, expected, def_bm, ti) } @@ -694,7 +692,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { &self, pat: &Pat<'_>, path_resolution: (Res, Option>, &'b [hir::PathSegment<'b>]), - qpath: &hir::QPath<'_>, expected: Ty<'tcx>, ti: TopInfo<'tcx>, ) -> Ty<'tcx> { @@ -707,17 +704,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { self.set_tainted_by_errors(); return tcx.types.err; } - Res::Def(DefKind::AssocFn, _) - | Res::Def(DefKind::Ctor(_, CtorKind::Fictive), _) - | Res::Def(DefKind::Ctor(_, CtorKind::Fn), _) => { - report_unexpected_variant_res(tcx, res, pat.span, qpath); + Res::Def(DefKind::AssocFn | DefKind::Ctor(_, CtorKind::Fictive | CtorKind::Fn), _) => { + report_unexpected_variant_res(tcx, res, pat.span); return tcx.types.err; } - Res::Def(DefKind::Ctor(_, CtorKind::Const), _) - | Res::SelfCtor(..) - | Res::Def(DefKind::Const, _) - | Res::Def(DefKind::AssocConst, _) - | Res::Def(DefKind::ConstParam, _) => {} // OK + Res::SelfCtor(..) + | Res::Def( + DefKind::Ctor(_, CtorKind::Const) + | DefKind::Const + | DefKind::AssocConst + | DefKind::ConstParam, + _, + ) => {} // OK _ => bug!("unexpected pattern resolution: {:?}", res), } @@ -791,14 +789,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } }; let report_unexpected_res = |res: Res| { + let sm = tcx.sess.source_map(); + let path_str = sm + .span_to_snippet(sm.span_until_char(pat.span, '(')) + .map_or(String::new(), |s| format!(" `{}`", s.trim_end())); let msg = format!( - "expected tuple struct or tuple variant, found {} `{}`", + "expected tuple struct or tuple variant, found {}{}", res.descr(), - rustc_hir_pretty::to_string(&tcx.hir(), |s| s.print_qpath(qpath, false)), + path_str ); + let mut err = struct_span_err!(tcx.sess, pat.span, E0164, "{}", msg); - match (res, &pat.kind) { - (Res::Def(DefKind::Fn, _), _) | (Res::Def(DefKind::AssocFn, _), _) => { + match res { + Res::Def(DefKind::Fn | DefKind::AssocFn, _) => { err.span_label(pat.span, "`fn` calls are not allowed in patterns"); err.help( "for more information, visit \ diff --git a/src/librustc_typeck/check_unused.rs b/src/librustc_typeck/check_unused.rs index fbc8c3327bf..9d8113e7b3f 100644 --- a/src/librustc_typeck/check_unused.rs +++ b/src/librustc_typeck/check_unused.rs @@ -5,7 +5,6 @@ use rustc_errors::Applicability; use rustc_hir as hir; use rustc_hir::def_id::{DefId, DefIdSet, LOCAL_CRATE}; use rustc_hir::itemlikevisit::ItemLikeVisitor; -use rustc_hir_pretty::visibility_qualified; use rustc_session::lint; use rustc_span::Span; @@ -176,16 +175,13 @@ fn unused_crates_lint(tcx: TyCtxt<'_>) { Some(orig_name) => format!("use {} as {};", orig_name, item.ident.name), None => format!("use {};", item.ident.name), }; - - let replacement = visibility_qualified(&item.vis, base_replacement); - let msg = "`extern crate` is not idiomatic in the new edition"; - let help = format!("convert it to a `{}`", visibility_qualified(&item.vis, "use")); - - lint.build(msg) + let vis = tcx.sess.source_map().span_to_snippet(item.vis.span).unwrap_or_default(); + let add_vis = |to| if vis.is_empty() { to } else { format!("{} {}", vis, to) }; + lint.build("`extern crate` is not idiomatic in the new edition") .span_suggestion_short( extern_crate.span, - &help, - replacement, + &format!("convert it to a `{}`", add_vis("use".to_string())), + add_vis(base_replacement), Applicability::MachineApplicable, ) .emit(); diff --git a/src/librustc_typeck/lib.rs b/src/librustc_typeck/lib.rs index e487e0d265c..fd854c75018 100644 --- a/src/librustc_typeck/lib.rs +++ b/src/librustc_typeck/lib.rs @@ -62,6 +62,7 @@ This API is completely unstable and subject to change. #![feature(crate_visibility_modifier)] #![feature(in_band_lifetimes)] #![feature(nll)] +#![feature(or_patterns)] #![feature(try_blocks)] #![feature(never_type)] #![feature(slice_partition_dedup)] diff --git a/src/test/ui/methods/method-path-in-pattern.stderr b/src/test/ui/methods/method-path-in-pattern.stderr index 1d1bdb6b052..ed3c0222c75 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 `::bar` --> $DIR/method-path-in-pattern.rs:19:9 | LL | ::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 `::trait_bar` --> $DIR/method-path-in-pattern.rs:23:9 | LL | ::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 `::bar` --> $DIR/method-path-in-pattern.rs:28:12 | LL | if let ::bar = 0u32 {} diff --git a/src/test/ui/privacy/associated-item-privacy-trait.rs b/src/test/ui/privacy/associated-item-privacy-trait.rs index 03347d5b99a..b1482bc040f 100644 --- a/src/test/ui/privacy/associated-item-privacy-trait.rs +++ b/src/test/ui/privacy/associated-item-privacy-trait.rs @@ -21,9 +21,9 @@ mod priv_trait { Pub.method(); //~^ ERROR type `for<'r> fn(&'r Self) {::method}` is private ::CONST; - //~^ ERROR associated constant `PrivTr::CONST` is private + //~^ ERROR associated constant `::CONST` is private let _: ::AssocTy; - //~^ ERROR associated type `PrivTr::AssocTy` is private + //~^ ERROR associated type `::AssocTy` is private pub type InSignatureTy = ::AssocTy; //~^ ERROR trait `priv_trait::PrivTr` is private pub trait InSignatureTr: PrivTr {} @@ -115,7 +115,7 @@ mod priv_parent_substs { >::CONST; //~^ ERROR type `priv_parent_substs::Priv` is private - let _: ::AssocTy; // FIXME no longer an error?! + let _: ::AssocTy; // FIXME no longer an error?! let _: >::AssocTy; //~^ ERROR type `priv_parent_substs::Priv` is private let _: >::AssocTy; diff --git a/src/test/ui/privacy/associated-item-privacy-trait.stderr b/src/test/ui/privacy/associated-item-privacy-trait.stderr index c30cc947d45..b9f3e35d722 100644 --- a/src/test/ui/privacy/associated-item-privacy-trait.stderr +++ b/src/test/ui/privacy/associated-item-privacy-trait.stderr @@ -31,7 +31,7 @@ LL | priv_trait::mac!(); | = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) -error: associated constant `PrivTr::CONST` is private +error: associated constant `::CONST` is private --> $DIR/associated-item-privacy-trait.rs:23:9 | LL | ::CONST; @@ -42,7 +42,7 @@ LL | priv_trait::mac!(); | = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) -error: associated type `PrivTr::AssocTy` is private +error: associated type `::AssocTy` is private --> $DIR/associated-item-privacy-trait.rs:25:16 | LL | let _: ::AssocTy; diff --git a/src/test/ui/qualified/qualified-path-params.stderr b/src/test/ui/qualified/qualified-path-params.stderr index 7ff43f4404c..4214e2503c3 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 `<::A>::f` +error[E0533]: expected unit struct, unit variant or constant, found associated function `::A::f::` --> $DIR/qualified-path-params.rs:20:9 | LL | ::A::f:: => {}