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_data_structures",
"rustc_errors", "rustc_errors",
"rustc_hir", "rustc_hir",
"rustc_hir_pretty",
"rustc_index", "rustc_index",
"rustc_infer", "rustc_infer",
"rustc_session", "rustc_session",

View File

@ -18,7 +18,6 @@ rustc_attr = { path = "../librustc_attr" }
rustc_data_structures = { path = "../librustc_data_structures" } rustc_data_structures = { path = "../librustc_data_structures" }
rustc_errors = { path = "../librustc_errors" } rustc_errors = { path = "../librustc_errors" }
rustc_hir = { path = "../librustc_hir" } rustc_hir = { path = "../librustc_hir" }
rustc_hir_pretty = { path = "../librustc_hir_pretty" }
rustc_target = { path = "../librustc_target" } rustc_target = { path = "../librustc_target" }
rustc_session = { path = "../librustc_session" } rustc_session = { path = "../librustc_session" }
smallvec = { version = "1.0", features = ["union", "may_dangle"] } 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 as hir;
use rustc_hir::def::{CtorOf, DefKind, Namespace, Res}; use rustc_hir::def::{CtorOf, DefKind, Namespace, Res};
use rustc_hir::def_id::DefId; 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::{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::lint::builtin::{AMBIGUOUS_ASSOCIATED_ITEMS, LATE_BOUND_LIFETIME_ARGUMENTS};
use rustc_session::parse::feature_err; use rustc_session::parse::feature_err;
use rustc_session::Session; use rustc_session::Session;
@ -1118,6 +1117,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
if !self.tcx().features().unboxed_closures if !self.tcx().features().unboxed_closures
&& trait_segment.generic_args().parenthesized != trait_def.paren_sugar && 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. // For now, require that parenthetical notation be used only with `Fn()` etc.
let (msg, sugg) = if trait_def.paren_sugar { 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(|args| args.args.get(0))
.and_then(|arg| match arg { .and_then(|arg| match arg {
hir::GenericArg::Type(ty) => { hir::GenericArg::Type(ty) => {
Some(to_string(NO_ANN, |s| s.print_type(ty))) sess.source_map().span_to_snippet(ty.span).ok()
} }
_ => None, _ => None,
}) })
@ -1143,7 +1143,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
.iter() .iter()
.filter_map(|b| match (b.ident.as_str() == "Output", &b.kind) { .filter_map(|b| match (b.ident.as_str() == "Output", &b.kind) {
(true, hir::TypeBindingKind::Equality { ty }) => { (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, _ => None,
}) })
@ -1154,7 +1154,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
} else { } else {
("parenthetical notation is only stable when used with `Fn`-family traits", None) ("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); let mut err = feature_err(sess, sym::unboxed_closures, span, msg);
if let Some(sugg) = sugg { if let Some(sugg) = sugg {
let msg = "use parenthetical notation instead"; 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_errors::{Applicability, DiagnosticBuilder};
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::{is_range_literal, Node}; use rustc_hir::{is_range_literal, Node};
use rustc_hir_pretty::{to_string, NO_ANN};
use rustc_span::symbol::sym; use rustc_span::symbol::sym;
use rustc_span::Span; use rustc_span::Span;
@ -199,15 +198,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.peekable(); .peekable();
if compatible_variants.peek().is_some() { if compatible_variants.peek().is_some() {
let expr_text = self if let Ok(expr_text) = self.tcx.sess.source_map().span_to_snippet(expr.span) {
.tcx let suggestions = compatible_variants.map(|v| format!("{}({})", v, expr_text));
.sess let msg = "try using a variant of the expected enum";
.source_map() err.span_suggestions(
.span_to_snippet(expr.span) expr.span,
.unwrap_or_else(|_| to_string(NO_ANN, |s| s.print_expr(expr))); msg,
let suggestions = compatible_variants.map(|v| format!("{}({})", v, expr_text)); suggestions,
let msg = "try using a variant of the expected enum"; Applicability::MaybeIncorrect,
err.span_suggestions(expr.span, msg, suggestions, Applicability::MaybeIncorrect); );
}
} }
} }
} }

View File

@ -475,7 +475,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
tcx.types.err tcx.types.err
} }
Res::Def(DefKind::Ctor(_, CtorKind::Fictive), _) => { 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 tcx.types.err
} }
_ => 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,

View File

@ -2656,14 +2656,14 @@ pub fn check_enum<'tcx>(
check_transparent(tcx, sp, def_id); 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!( 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(),
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(); .emit();
} }

View File

@ -171,9 +171,7 @@ 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(ref qpath) => { PatKind::Path(_) => self.check_pat_path(pat, path_res.unwrap(), expected, ti),
self.check_pat_path(pat, path_res.unwrap(), qpath, expected, ti)
}
PatKind::Struct(ref qpath, fields, etc) => { PatKind::Struct(ref qpath, fields, etc) => {
self.check_pat_struct(pat, qpath, fields, etc, expected, def_bm, ti) self.check_pat_struct(pat, qpath, fields, etc, expected, def_bm, ti)
} }
@ -694,7 +692,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
&self, &self,
pat: &Pat<'_>, pat: &Pat<'_>,
path_resolution: (Res, Option<Ty<'tcx>>, &'b [hir::PathSegment<'b>]), path_resolution: (Res, Option<Ty<'tcx>>, &'b [hir::PathSegment<'b>]),
qpath: &hir::QPath<'_>,
expected: Ty<'tcx>, expected: Ty<'tcx>,
ti: TopInfo<'tcx>, ti: TopInfo<'tcx>,
) -> Ty<'tcx> { ) -> Ty<'tcx> {
@ -707,17 +704,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.set_tainted_by_errors(); self.set_tainted_by_errors();
return tcx.types.err; return tcx.types.err;
} }
Res::Def(DefKind::AssocFn, _) Res::Def(DefKind::AssocFn | DefKind::Ctor(_, CtorKind::Fictive | CtorKind::Fn), _) => {
| Res::Def(DefKind::Ctor(_, CtorKind::Fictive), _) report_unexpected_variant_res(tcx, res, pat.span);
| Res::Def(DefKind::Ctor(_, CtorKind::Fn), _) => {
report_unexpected_variant_res(tcx, res, pat.span, qpath);
return tcx.types.err; return tcx.types.err;
} }
Res::Def(DefKind::Ctor(_, CtorKind::Const), _) Res::SelfCtor(..)
| Res::SelfCtor(..) | Res::Def(
| Res::Def(DefKind::Const, _) DefKind::Ctor(_, CtorKind::Const)
| Res::Def(DefKind::AssocConst, _) | DefKind::Const
| Res::Def(DefKind::ConstParam, _) => {} // OK | DefKind::AssocConst
| DefKind::ConstParam,
_,
) => {} // OK
_ => bug!("unexpected pattern resolution: {:?}", res), _ => bug!("unexpected pattern resolution: {:?}", res),
} }
@ -791,14 +789,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
} }
}; };
let report_unexpected_res = |res: Res| { 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!( let msg = format!(
"expected tuple struct or tuple variant, found {} `{}`", "expected tuple struct or tuple variant, found {}{}",
res.descr(), 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); let mut err = struct_span_err!(tcx.sess, pat.span, E0164, "{}", msg);
match (res, &pat.kind) { match res {
(Res::Def(DefKind::Fn, _), _) | (Res::Def(DefKind::AssocFn, _), _) => { Res::Def(DefKind::Fn | DefKind::AssocFn, _) => {
err.span_label(pat.span, "`fn` calls are not allowed in patterns"); err.span_label(pat.span, "`fn` calls are not allowed in patterns");
err.help( err.help(
"for more information, visit \ "for more information, visit \

View File

@ -5,7 +5,6 @@ use rustc_errors::Applicability;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def_id::{DefId, DefIdSet, LOCAL_CRATE}; use rustc_hir::def_id::{DefId, DefIdSet, LOCAL_CRATE};
use rustc_hir::itemlikevisit::ItemLikeVisitor; use rustc_hir::itemlikevisit::ItemLikeVisitor;
use rustc_hir_pretty::visibility_qualified;
use rustc_session::lint; use rustc_session::lint;
use rustc_span::Span; 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), Some(orig_name) => format!("use {} as {};", orig_name, item.ident.name),
None => format!("use {};", item.ident.name), None => format!("use {};", item.ident.name),
}; };
let vis = tcx.sess.source_map().span_to_snippet(item.vis.span).unwrap_or_default();
let replacement = visibility_qualified(&item.vis, base_replacement); let add_vis = |to| if vis.is_empty() { to } else { format!("{} {}", vis, to) };
let msg = "`extern crate` is not idiomatic in the new edition"; lint.build("`extern crate` is not idiomatic in the new edition")
let help = format!("convert it to a `{}`", visibility_qualified(&item.vis, "use"));
lint.build(msg)
.span_suggestion_short( .span_suggestion_short(
extern_crate.span, extern_crate.span,
&help, &format!("convert it to a `{}`", add_vis("use".to_string())),
replacement, add_vis(base_replacement),
Applicability::MachineApplicable, Applicability::MachineApplicable,
) )
.emit(); .emit();

View File

@ -62,6 +62,7 @@ This API is completely unstable and subject to change.
#![feature(crate_visibility_modifier)] #![feature(crate_visibility_modifier)]
#![feature(in_band_lifetimes)] #![feature(in_band_lifetimes)]
#![feature(nll)] #![feature(nll)]
#![feature(or_patterns)]
#![feature(try_blocks)] #![feature(try_blocks)]
#![feature(never_type)] #![feature(never_type)]
#![feature(slice_partition_dedup)] #![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 => {} 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 {}

View File

@ -21,9 +21,9 @@ mod priv_trait {
Pub.method(); Pub.method();
//~^ ERROR type `for<'r> fn(&'r Self) {<Self as priv_trait::PrivTr>::method}` is private //~^ ERROR type `for<'r> fn(&'r Self) {<Self as priv_trait::PrivTr>::method}` is private
<Pub as PrivTr>::CONST; <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; 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; pub type InSignatureTy = <Pub as PrivTr>::AssocTy;
//~^ ERROR trait `priv_trait::PrivTr` is private //~^ ERROR trait `priv_trait::PrivTr` is private
pub trait InSignatureTr: PrivTr {} pub trait InSignatureTr: PrivTr {}
@ -115,7 +115,7 @@ mod priv_parent_substs {
<Priv as PubTr<_>>::CONST; <Priv as PubTr<_>>::CONST;
//~^ ERROR type `priv_parent_substs::Priv` is private //~^ 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; let _: <Pub as PubTr<_>>::AssocTy;
//~^ ERROR type `priv_parent_substs::Priv` is private //~^ ERROR type `priv_parent_substs::Priv` is private
let _: <Priv as PubTr<_>>::AssocTy; 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) = 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 --> $DIR/associated-item-privacy-trait.rs:23:9
| |
LL | <Pub as PrivTr>::CONST; 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) = 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 --> $DIR/associated-item-privacy-trait.rs:25:16
| |
LL | let _: <Pub as PrivTr>::AssocTy; 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 --> $DIR/qualified-path-params.rs:20:9
| |
LL | <S as Tr>::A::f::<u8> => {} LL | <S as Tr>::A::f::<u8> => {}