Skip to content

Commit f07802c

Browse files
committed
decouple rustc_hir::print from crate
1 parent 342c5f3 commit f07802c

File tree

6 files changed

+52
-112
lines changed

6 files changed

+52
-112
lines changed

src/librustc_hir/hir.rs

Lines changed: 7 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@ use crate::def::{DefKind, Namespace, Res};
22
use crate::def_id::DefId;
33
crate use crate::hir_id::HirId;
44
use crate::itemlikevisit;
5-
use crate::print;
6-
7-
crate use BlockCheckMode::*;
8-
crate use FnRetTy::*;
9-
crate use UnsafeSource::*;
105

116
use rustc_ast::ast::{self, AsmDialect, CrateSugar, Ident, Name};
127
use rustc_ast::ast::{AttrVec, Attribute, FloatTy, IntTy, Label, LitKind, StrStyle, UintTy};
@@ -169,12 +164,7 @@ impl fmt::Display for Lifetime {
169164

170165
impl fmt::Debug for Lifetime {
171166
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
172-
write!(
173-
f,
174-
"lifetime({}: {})",
175-
self.hir_id,
176-
print::to_string(print::NO_ANN, |s| s.print_lifetime(self))
177-
)
167+
write!(f, "lifetime({}: {})", self.hir_id, self.name.ident())
178168
}
179169
}
180170

@@ -191,7 +181,7 @@ impl Lifetime {
191181
/// A `Path` is essentially Rust's notion of a name; for instance,
192182
/// `std::cmp::PartialEq`. It's represented as a sequence of identifiers,
193183
/// along with a bunch of supporting information.
194-
#[derive(RustcEncodable, RustcDecodable, HashStable_Generic)]
184+
#[derive(RustcEncodable, RustcDecodable, Debug, HashStable_Generic)]
195185
pub struct Path<'hir> {
196186
pub span: Span,
197187
/// The resolution for the path.
@@ -206,18 +196,6 @@ impl Path<'_> {
206196
}
207197
}
208198

209-
impl fmt::Debug for Path<'_> {
210-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
211-
write!(f, "path({})", self)
212-
}
213-
}
214-
215-
impl fmt::Display for Path<'_> {
216-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
217-
write!(f, "{}", print::to_string(print::NO_ANN, |s| s.print_path(self, false)))
218-
}
219-
}
220-
221199
/// A segment of a path: an identifier, an optional lifetime, and a set of
222200
/// types.
223201
#[derive(RustcEncodable, RustcDecodable, Debug, HashStable_Generic)]
@@ -758,25 +736,14 @@ pub struct Block<'hir> {
758736
pub targeted_by_break: bool,
759737
}
760738

761-
#[derive(RustcEncodable, RustcDecodable, HashStable_Generic)]
739+
#[derive(Debug, RustcEncodable, RustcDecodable, HashStable_Generic)]
762740
pub struct Pat<'hir> {
763741
#[stable_hasher(ignore)]
764742
pub hir_id: HirId,
765743
pub kind: PatKind<'hir>,
766744
pub span: Span,
767745
}
768746

769-
impl fmt::Debug for Pat<'_> {
770-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
771-
write!(
772-
f,
773-
"pat({}: {})",
774-
self.hir_id,
775-
print::to_string(print::NO_ANN, |s| s.print_pat(self))
776-
)
777-
}
778-
}
779-
780747
impl Pat<'_> {
781748
// FIXME(#19596) this is a workaround, but there should be a better way
782749
fn walk_short_(&self, it: &mut impl FnMut(&Pat<'_>) -> bool) -> bool {
@@ -1118,26 +1085,15 @@ impl UnOp {
11181085
}
11191086

11201087
/// A statement.
1121-
#[derive(RustcEncodable, RustcDecodable, HashStable_Generic)]
1088+
#[derive(RustcEncodable, RustcDecodable, Debug, HashStable_Generic)]
11221089
pub struct Stmt<'hir> {
11231090
pub hir_id: HirId,
11241091
pub kind: StmtKind<'hir>,
11251092
pub span: Span,
11261093
}
11271094

1128-
impl fmt::Debug for Stmt<'_> {
1129-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1130-
write!(
1131-
f,
1132-
"stmt({}: {})",
1133-
self.hir_id,
1134-
print::to_string(print::NO_ANN, |s| s.print_stmt(self))
1135-
)
1136-
}
1137-
}
1138-
11391095
/// The contents of a statement.
1140-
#[derive(RustcEncodable, RustcDecodable, HashStable_Generic)]
1096+
#[derive(RustcEncodable, RustcDecodable, Debug, HashStable_Generic)]
11411097
pub enum StmtKind<'hir> {
11421098
/// A local (`let`) binding.
11431099
Local(&'hir Local<'hir>),
@@ -1351,7 +1307,7 @@ pub struct AnonConst {
13511307
}
13521308

13531309
/// An expression.
1354-
#[derive(RustcEncodable, RustcDecodable)]
1310+
#[derive(Debug, RustcEncodable, RustcDecodable)]
13551311
pub struct Expr<'hir> {
13561312
pub hir_id: HirId,
13571313
pub kind: ExprKind<'hir>,
@@ -1472,17 +1428,6 @@ impl Expr<'_> {
14721428
}
14731429
}
14741430

1475-
impl fmt::Debug for Expr<'_> {
1476-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1477-
write!(
1478-
f,
1479-
"expr({}: {})",
1480-
self.hir_id,
1481-
print::to_string(print::NO_ANN, |s| s.print_expr(self))
1482-
)
1483-
}
1484-
}
1485-
14861431
/// Checks if the specified expression is a built-in range literal.
14871432
/// (See: `LoweringContext::lower_expr()`).
14881433
///
@@ -1965,19 +1910,13 @@ impl TypeBinding<'_> {
19651910
}
19661911
}
19671912

1968-
#[derive(RustcEncodable, RustcDecodable)]
1913+
#[derive(Debug, RustcEncodable, RustcDecodable)]
19691914
pub struct Ty<'hir> {
19701915
pub hir_id: HirId,
19711916
pub kind: TyKind<'hir>,
19721917
pub span: Span,
19731918
}
19741919

1975-
impl fmt::Debug for Ty<'_> {
1976-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1977-
write!(f, "type({})", print::to_string(print::NO_ANN, |s| s.print_type(self)))
1978-
}
1979-
}
1980-
19811920
/// Not represented directly in the AST; referred to by name through a `ty_path`.
19821921
#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
19831922
#[derive(HashStable_Generic)]
@@ -2182,15 +2121,6 @@ pub enum FnRetTy<'hir> {
21822121
Return(&'hir Ty<'hir>),
21832122
}
21842123

2185-
impl fmt::Display for FnRetTy<'_> {
2186-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2187-
match self {
2188-
Self::Return(ref ty) => print::to_string(print::NO_ANN, |s| s.print_type(ty)).fmt(f),
2189-
Self::DefaultReturn(_) => "()".fmt(f),
2190-
}
2191-
}
2192-
}
2193-
21942124
impl FnRetTy<'_> {
21952125
pub fn span(&self) -> Span {
21962126
match *self {

src/librustc_hir/print.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,10 +1006,10 @@ impl<'a> State<'a> {
10061006
close_box: bool,
10071007
) {
10081008
match blk.rules {
1009-
hir::UnsafeBlock(..) => self.word_space("unsafe"),
1010-
hir::PushUnsafeBlock(..) => self.word_space("push_unsafe"),
1011-
hir::PopUnsafeBlock(..) => self.word_space("pop_unsafe"),
1012-
hir::DefaultBlock => (),
1009+
hir::BlockCheckMode::UnsafeBlock(..) => self.word_space("unsafe"),
1010+
hir::BlockCheckMode::PushUnsafeBlock(..) => self.word_space("push_unsafe"),
1011+
hir::BlockCheckMode::PopUnsafeBlock(..) => self.word_space("pop_unsafe"),
1012+
hir::BlockCheckMode::DefaultBlock => (),
10131013
}
10141014
self.maybe_print_comment(blk.span.lo());
10151015
self.ann.pre(self, AnnNode::Block(blk));
@@ -1848,7 +1848,8 @@ impl<'a> State<'a> {
18481848
self.print_block_unclosed(&blk);
18491849

18501850
// If it is a user-provided unsafe block, print a comma after it
1851-
if let hir::UnsafeBlock(hir::UserProvided) = blk.rules {
1851+
if let hir::BlockCheckMode::UnsafeBlock(hir::UnsafeSource::UserProvided) = blk.rules
1852+
{
18521853
self.s.word(",");
18531854
}
18541855
}
@@ -1928,18 +1929,18 @@ impl<'a> State<'a> {
19281929
});
19291930
self.s.word("|");
19301931

1931-
if let hir::DefaultReturn(..) = decl.output {
1932+
if let hir::FnRetTy::DefaultReturn(..) = decl.output {
19321933
return;
19331934
}
19341935

19351936
self.space_if_not_bol();
19361937
self.word_space("->");
19371938
match decl.output {
1938-
hir::Return(ref ty) => {
1939+
hir::FnRetTy::Return(ref ty) => {
19391940
self.print_type(&ty);
19401941
self.maybe_print_comment(ty.span.lo())
19411942
}
1942-
hir::DefaultReturn(..) => unreachable!(),
1943+
hir::FnRetTy::DefaultReturn(..) => unreachable!(),
19431944
}
19441945
}
19451946

@@ -2112,21 +2113,21 @@ impl<'a> State<'a> {
21122113
}
21132114

21142115
pub fn print_fn_output(&mut self, decl: &hir::FnDecl<'_>) {
2115-
if let hir::DefaultReturn(..) = decl.output {
2116+
if let hir::FnRetTy::DefaultReturn(..) = decl.output {
21162117
return;
21172118
}
21182119

21192120
self.space_if_not_bol();
21202121
self.ibox(INDENT_UNIT);
21212122
self.word_space("->");
21222123
match decl.output {
2123-
hir::DefaultReturn(..) => unreachable!(),
2124-
hir::Return(ref ty) => self.print_type(&ty),
2124+
hir::FnRetTy::DefaultReturn(..) => unreachable!(),
2125+
hir::FnRetTy::Return(ref ty) => self.print_type(&ty),
21252126
}
21262127
self.end();
21272128

21282129
match decl.output {
2129-
hir::Return(ref output) => self.maybe_print_comment(output.span.lo()),
2130+
hir::FnRetTy::Return(ref output) => self.maybe_print_comment(output.span.lo()),
21302131
_ => {}
21312132
}
21322133
}

src/librustc_privacy/lib.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,14 +1313,18 @@ impl<'a, 'tcx> Visitor<'tcx> for TypePrivacyVisitor<'a, 'tcx> {
13131313
let is_local_static =
13141314
if let DefKind::Static = kind { def_id.is_local() } else { false };
13151315
if !self.item_is_accessible(def_id) && !is_local_static {
1316-
let name = match *qpath {
1317-
hir::QPath::Resolved(_, ref path) => path.to_string(),
1318-
hir::QPath::TypeRelative(_, ref segment) => segment.ident.to_string(),
1316+
let sess = self.tcx.sess;
1317+
let sm = sess.source_map();
1318+
let name = match qpath {
1319+
hir::QPath::Resolved(_, path) => sm.span_to_snippet(path.span).ok(),
1320+
hir::QPath::TypeRelative(_, segment) => Some(segment.ident.to_string()),
13191321
};
13201322
let kind = kind.descr(def_id);
1321-
self.tcx
1322-
.sess
1323-
.struct_span_err(span, &format!("{} `{}` is private", kind, name))
1323+
let msg = match name {
1324+
Some(name) => format!("{} `{}` is private", kind, name),
1325+
None => format!("{} is private", kind),
1326+
};
1327+
sess.struct_span_err(span, &msg)
13241328
.span_label(span, &format!("private {}", kind))
13251329
.emit();
13261330
return;

src/librustc_typeck/check/_match.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
245245
{
246246
// check that the `if` expr without `else` is the fn body's expr
247247
if expr.span == span {
248-
return self.get_fn_decl(hir_id).map(|(fn_decl, _)| {
249-
(
250-
fn_decl.output.span(),
251-
format!("expected `{}` because of this return type", fn_decl.output),
252-
)
248+
return self.get_fn_decl(hir_id).and_then(|(fn_decl, _)| {
249+
let span = fn_decl.output.span();
250+
let snippet = self.tcx.sess.source_map().span_to_snippet(span).ok()?;
251+
Some((span, format!("expected `{}` because of this return type", snippet)))
253252
});
254253
}
255254
}

src/librustc_typeck/check/callee.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -335,16 +335,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
335335
err.span_label(call_expr.span, "call expression requires function");
336336

337337
if let Some(span) = self.tcx.hir().res_span(def) {
338+
let callee_ty = callee_ty.to_string();
338339
let label = match (unit_variant, inner_callee_path) {
339-
(Some(path), _) => format!("`{}` defined here", path),
340-
(_, Some(hir::QPath::Resolved(_, path))) => format!(
341-
"`{}` defined here returns `{}`",
342-
path,
343-
callee_ty.to_string()
344-
),
345-
_ => format!("`{}` defined here", callee_ty.to_string()),
340+
(Some(path), _) => Some(format!("`{}` defined here", path)),
341+
(_, Some(hir::QPath::Resolved(_, path))) => {
342+
self.tcx.sess.source_map().span_to_snippet(path.span).ok().map(
343+
|p| format!("`{}` defined here returns `{}`", p, callee_ty),
344+
)
345+
}
346+
_ => Some(format!("`{}` defined here", callee_ty)),
346347
};
347-
err.span_label(span, label);
348+
if let Some(label) = label {
349+
err.span_label(span, label);
350+
}
348351
}
349352
err.emit();
350353
} else {

src/librustc_typeck/check/expr.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -696,10 +696,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
696696
self,
697697
&cause,
698698
&mut |db| {
699-
db.span_label(
700-
fn_decl.output.span(),
701-
format!("expected `{}` because of this return type", fn_decl.output,),
702-
);
699+
let span = fn_decl.output.span();
700+
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
701+
db.span_label(
702+
span,
703+
format!("expected `{}` because of this return type", snippet),
704+
);
705+
}
703706
},
704707
true,
705708
);

0 commit comments

Comments
 (0)