rustc_typeck: remove rustc_hir_pretty usage

This commit is contained in:
Mazdak Farrokhzad 2020-03-23 22:39:59 +01:00
parent b3866a5c93
commit 92885e3a5b
13 changed files with 53 additions and 56 deletions

View File

@ -4229,7 +4229,6 @@ dependencies = [
"rustc_data_structures",
"rustc_errors",
"rustc_hir",
"rustc_hir_pretty",
"rustc_index",
"rustc_infer",
"rustc_session",

View File

@ -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"] }

View File

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

View File

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

View File

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

View File

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

View File

@ -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<Ty<'tcx>>, &'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 \

View File

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

View File

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

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

@ -21,9 +21,9 @@ mod priv_trait {
Pub.method();
//~^ ERROR type `for<'r> fn(&'r Self) {<Self as priv_trait::PrivTr>::method}` is private
<Pub as PrivTr>::CONST;
//~^ ERROR associated constant `PrivTr::CONST` is private
//~^ ERROR associated constant `<Pub as PrivTr>::CONST` is private
let _: <Pub as PrivTr>::AssocTy;
//~^ ERROR associated type `PrivTr::AssocTy` is private
//~^ ERROR associated type `<Pub as PrivTr>::AssocTy` is private
pub type InSignatureTy = <Pub as PrivTr>::AssocTy;
//~^ ERROR trait `priv_trait::PrivTr` is private
pub trait InSignatureTr: PrivTr {}
@ -115,7 +115,7 @@ mod priv_parent_substs {
<Priv as PubTr<_>>::CONST;
//~^ ERROR type `priv_parent_substs::Priv` is private
let _: <Pub as PubTr>::AssocTy; // FIXME no longer an error?!
let _: <Pub as PubTr>::AssocTy; // FIXME no longer an error?!
let _: <Pub as PubTr<_>>::AssocTy;
//~^ ERROR type `priv_parent_substs::Priv` is private
let _: <Priv as PubTr<_>>::AssocTy;

View File

@ -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 `<Pub as PrivTr>::CONST` is private
--> $DIR/associated-item-privacy-trait.rs:23:9
|
LL | <Pub as PrivTr>::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 `<Pub as PrivTr>::AssocTy` is private
--> $DIR/associated-item-privacy-trait.rs:25:16
|
LL | let _: <Pub as PrivTr>::AssocTy;

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