Skip to content

Commit 742ec4b

Browse files
committed
ast: remove implicit pprust dependency via Display.
Instead just use `pprust::path_to_string(..)` where needed. This has two benefits: a) The AST definition is now independent of printing it. (Therefore we get closer to extracting a data-crate.) b) Debugging should be easier as program flow is clearer.
1 parent d131abe commit 742ec4b

File tree

15 files changed

+88
-47
lines changed

15 files changed

+88
-47
lines changed

src/librustc/lint/levels.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use rustc_data_structures::stable_hasher::{HashStable, ToStableHashKey, StableHa
1212
use syntax::ast;
1313
use syntax::attr;
1414
use syntax::feature_gate;
15+
use syntax::print::pprust;
1516
use syntax::source_map::MultiSpan;
1617
use syntax::symbol::{Symbol, sym};
1718

@@ -285,7 +286,7 @@ impl<'a> LintLevelsBuilder<'a> {
285286
tool_ident.span,
286287
E0710,
287288
"an unknown tool name found in scoped lint: `{}`",
288-
meta_item.path
289+
pprust::path_to_string(&meta_item.path),
289290
);
290291
continue;
291292
}

src/librustc_lint/builtin.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use syntax::feature_gate::{Stability, deprecated_attributes};
4545
use syntax_pos::{BytePos, Span};
4646
use syntax::symbol::{Symbol, kw, sym};
4747
use syntax::errors::{Applicability, DiagnosticBuilder};
48-
use syntax::print::pprust::expr_to_string;
48+
use syntax::print::pprust::{self, expr_to_string};
4949
use syntax::visit::FnKind;
5050

5151
use rustc::hir::{self, GenericParamKind, PatKind};
@@ -701,7 +701,8 @@ impl EarlyLintPass for DeprecatedAttr {
701701
}
702702
}
703703
if attr.check_name(sym::no_start) || attr.check_name(sym::crate_id) {
704-
let msg = format!("use of deprecated attribute `{}`: no longer used.", attr.path);
704+
let path_str = pprust::path_to_string(&attr.path);
705+
let msg = format!("use of deprecated attribute `{}`: no longer used.", path_str);
705706
lint_deprecated_attr(cx, attr, &msg, None);
706707
}
707708
}

src/librustc_mir/dataflow/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use syntax::ast::{self, MetaItem};
2+
use syntax::print::pprust;
23
use syntax::symbol::{Symbol, sym};
34

45
use rustc_index::bit_set::{BitSet, HybridBitSet};
@@ -159,9 +160,8 @@ where
159160
if let Some(s) = item.value_str() {
160161
return Some(s.to_string())
161162
} else {
162-
sess.span_err(
163-
item.span,
164-
&format!("{} attribute requires a path", item.path));
163+
let path = pprust::path_to_string(&item.path);
164+
sess.span_err(item.span, &format!("{} attribute requires a path", path));
165165
return None;
166166
}
167167
}

src/librustc_passes/ast_validation.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,8 @@ impl<'a> AstValidator<'a> {
263263
let mut err = self.err_handler().struct_span_err(poly.span,
264264
&format!("`?Trait` is not permitted in {}", where_));
265265
if is_trait {
266-
err.note(&format!("traits are `?{}` by default", poly.trait_ref.path));
266+
let path_str = pprust::path_to_string(&poly.trait_ref.path);
267+
err.note(&format!("traits are `?{}` by default", path_str));
267268
}
268269
err.emit();
269270
}

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use syntax::ext::expand::AstFragment;
3737
use syntax::ext::hygiene::ExpnId;
3838
use syntax::feature_gate::is_builtin_attr;
3939
use syntax::parse::token::{self, Token};
40+
use syntax::print::pprust;
4041
use syntax::{span_err, struct_span_err};
4142
use syntax::source_map::{respan, Spanned};
4243
use syntax::symbol::{kw, sym};
@@ -228,7 +229,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
228229
.span_suggestion(
229230
path.span,
230231
"try",
231-
format!("crate::{}", path),
232+
format!("crate::{}", pprust::path_to_string(&path)),
232233
Applicability::MaybeIncorrect,
233234
)
234235
.emit();

src/librustc_resolve/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use rustc_metadata::cstore::CStore;
3838
use syntax::ext::hygiene::{ExpnId, Transparency, SyntaxContext};
3939
use syntax::ast::{self, Name, NodeId, Ident, FloatTy, IntTy, UintTy};
4040
use syntax::ext::base::{SyntaxExtension, MacroKind, SpecialDerives};
41+
use syntax::print::pprust;
4142
use syntax::symbol::{kw, sym};
4243

4344
use syntax::visit::{self, Visitor};
@@ -2011,13 +2012,13 @@ impl<'a> Resolver<'a> {
20112012
let mut candidates =
20122013
self.lookup_import_candidates(ident, TypeNS, is_mod);
20132014
candidates.sort_by_cached_key(|c| {
2014-
(c.path.segments.len(), c.path.to_string())
2015+
(c.path.segments.len(), pprust::path_to_string(&c.path))
20152016
});
20162017
if let Some(candidate) = candidates.get(0) {
20172018
(
20182019
String::from("unresolved import"),
20192020
Some((
2020-
vec![(ident.span, candidate.path.to_string())],
2021+
vec![(ident.span, pprust::path_to_string(&candidate.path))],
20212022
String::from("a similar path exists"),
20222023
Applicability::MaybeIncorrect,
20232024
)),

src/librustc_resolve/macros.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use syntax::ext::hygiene::{self, ExpnId, ExpnData, ExpnKind};
2121
use syntax::ext::compile_declarative_macro;
2222
use syntax::feature_gate::{emit_feature_err, is_builtin_attr_name};
2323
use syntax::feature_gate::GateIssue;
24+
use syntax::print::pprust;
2425
use syntax::symbol::{Symbol, kw, sym};
2526
use syntax_pos::{Span, DUMMY_SP};
2627

@@ -324,7 +325,8 @@ impl<'a> Resolver<'a> {
324325

325326
Ok(if ext.macro_kind() != kind {
326327
let expected = kind.descr_expected();
327-
let msg = format!("expected {}, found {} `{}`", expected, res.descr(), path);
328+
let path_str = pprust::path_to_string(path);
329+
let msg = format!("expected {}, found {} `{}`", expected, res.descr(), path_str);
328330
self.session.struct_span_err(path.span, &msg)
329331
.span_label(path.span, format!("not {} {}", kind.article(), expected))
330332
.emit();
@@ -805,14 +807,16 @@ impl<'a> Resolver<'a> {
805807
}
806808
}
807809
if let Some(depr) = &stability.rustc_depr {
808-
let (message, lint) = stability::rustc_deprecation_message(depr, &path.to_string());
810+
let path = pprust::path_to_string(path);
811+
let (message, lint) = stability::rustc_deprecation_message(depr, &path);
809812
stability::early_report_deprecation(
810813
self.session, &message, depr.suggestion, lint, span
811814
);
812815
}
813816
}
814817
if let Some(depr) = &ext.deprecation {
815-
let (message, lint) = stability::deprecation_message(depr, &path.to_string());
818+
let path = pprust::path_to_string(&path);
819+
let (message, lint) = stability::deprecation_message(depr, &path);
816820
stability::early_report_deprecation(self.session, &message, None, lint, span);
817821
}
818822
}

src/librustdoc/html/render.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ use serialize::json::{ToJson, Json, as_json};
4646
use syntax::ast;
4747
use syntax::edition::Edition;
4848
use syntax::ext::base::MacroKind;
49+
use syntax::print::pprust;
4950
use syntax::source_map::FileName;
5051
use syntax::feature_gate::UnstableFeatures;
5152
use syntax::symbol::{Symbol, sym};
@@ -2957,7 +2958,7 @@ fn item_enum(w: &mut Buffer, cx: &Context, it: &clean::Item, e: &clean::Enum) {
29572958
}
29582959

29592960
fn render_attribute(attr: &ast::MetaItem) -> Option<String> {
2960-
let path = attr.path.to_string();
2961+
let path = pprust::path_to_string(&attr.path);
29612962

29622963
if attr.is_word() {
29632964
Some(path)

src/libsyntax/ast.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ pub use crate::util::parser::ExprPrecedence;
77

88
use crate::ext::hygiene::ExpnId;
99
use crate::parse::token::{self, DelimToken};
10-
use crate::print::pprust;
1110
use crate::ptr::P;
1211
use crate::source_map::{dummy_spanned, respan, Spanned};
1312
use crate::symbol::{kw, sym, Symbol};
@@ -86,12 +85,6 @@ impl PartialEq<Symbol> for Path {
8685
}
8786
}
8887

89-
impl fmt::Display for Path {
90-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
91-
write!(f, "{}", pprust::path_to_string(self))
92-
}
93-
}
94-
9588
impl Path {
9689
// Convert a span and an identifier to the corresponding
9790
// one-segment path.

src/libsyntax/attr/builtin.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::early_buffered_lints::BufferedEarlyLintId;
55
use crate::ext::base::ExtCtxt;
66
use crate::feature_gate::{Features, GatedCfg};
77
use crate::parse::ParseSess;
8+
use crate::print::pprust;
89

910
use errors::{Applicability, Handler};
1011
use syntax_pos::hygiene::Transparency;
@@ -243,7 +244,11 @@ fn find_stability_generic<'a, I>(sess: &ParseSess,
243244
let meta = meta.as_ref().unwrap();
244245
let get = |meta: &MetaItem, item: &mut Option<Symbol>| {
245246
if item.is_some() {
246-
handle_errors(sess, meta.span, AttrError::MultipleItem(meta.path.to_string()));
247+
handle_errors(
248+
sess,
249+
meta.span,
250+
AttrError::MultipleItem(pprust::path_to_string(&meta.path)),
251+
);
247252
return false
248253
}
249254
if let Some(v) = meta.value_str() {
@@ -271,7 +276,10 @@ fn find_stability_generic<'a, I>(sess: &ParseSess,
271276
handle_errors(
272277
sess,
273278
mi.span,
274-
AttrError::UnknownMetaItem(mi.path.to_string(), expected),
279+
AttrError::UnknownMetaItem(
280+
pprust::path_to_string(&mi.path),
281+
expected,
282+
),
275283
);
276284
continue 'outer
277285
}
@@ -362,7 +370,7 @@ fn find_stability_generic<'a, I>(sess: &ParseSess,
362370
sess,
363371
meta.span(),
364372
AttrError::UnknownMetaItem(
365-
mi.path.to_string(),
373+
pprust::path_to_string(&mi.path),
366374
&["feature", "reason", "issue", "soft"]
367375
),
368376
);
@@ -434,7 +442,8 @@ fn find_stability_generic<'a, I>(sess: &ParseSess,
434442
sess,
435443
meta.span(),
436444
AttrError::UnknownMetaItem(
437-
mi.path.to_string(), &["since", "note"],
445+
pprust::path_to_string(&mi.path),
446+
&["since", "note"],
438447
),
439448
);
440449
continue 'outer
@@ -597,8 +606,11 @@ pub fn eval_condition<F>(cfg: &ast::MetaItem, sess: &ParseSess, eval: &mut F)
597606
!eval_condition(mis[0].meta_item().unwrap(), sess, eval)
598607
},
599608
_ => {
600-
span_err!(sess.span_diagnostic, cfg.span, E0537,
601-
"invalid predicate `{}`", cfg.path);
609+
span_err!(
610+
sess.span_diagnostic, cfg.span, E0537,
611+
"invalid predicate `{}`",
612+
pprust::path_to_string(&cfg.path)
613+
);
602614
false
603615
}
604616
}
@@ -653,7 +665,9 @@ fn find_deprecation_generic<'a, I>(sess: &ParseSess,
653665
let get = |meta: &MetaItem, item: &mut Option<Symbol>| {
654666
if item.is_some() {
655667
handle_errors(
656-
sess, meta.span, AttrError::MultipleItem(meta.path.to_string())
668+
sess,
669+
meta.span,
670+
AttrError::MultipleItem(pprust::path_to_string(&meta.path)),
657671
);
658672
return false
659673
}
@@ -691,8 +705,10 @@ fn find_deprecation_generic<'a, I>(sess: &ParseSess,
691705
handle_errors(
692706
sess,
693707
meta.span(),
694-
AttrError::UnknownMetaItem(mi.path.to_string(),
695-
&["since", "note"]),
708+
AttrError::UnknownMetaItem(
709+
pprust::path_to_string(&mi.path),
710+
&["since", "note"],
711+
),
696712
);
697713
continue 'outer
698714
}

src/libsyntax/ext/expand.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::mut_visit::*;
1313
use crate::parse::{DirectoryOwnership, PResult, ParseSess};
1414
use crate::parse::token;
1515
use crate::parse::parser::Parser;
16+
use crate::print::pprust;
1617
use crate::ptr::P;
1718
use crate::symbol::{sym, Symbol};
1819
use crate::tokenstream::{TokenStream, TokenTree};
@@ -388,7 +389,8 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
388389
"`derive` may only be applied to structs, enums and unions");
389390
if let ast::AttrStyle::Inner = attr.style {
390391
let trait_list = derives.iter()
391-
.map(|t| t.to_string()).collect::<Vec<_>>();
392+
.map(|t| pprust::path_to_string(t))
393+
.collect::<Vec<_>>();
392394
let suggestion = format!("#[derive({})]", trait_list.join(", "));
393395
err.span_suggestion(
394396
span, "try an outer attribute", suggestion,
@@ -587,8 +589,11 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
587589
let result = if let Some(result) = fragment_kind.make_from(tok_result) {
588590
result
589591
} else {
590-
let msg = format!("non-{kind} macro in {kind} position: {path}",
591-
kind = fragment_kind.name(), path = mac.path);
592+
let msg = format!(
593+
"non-{kind} macro in {kind} position: {path}",
594+
kind = fragment_kind.name(),
595+
path = pprust::path_to_string(&mac.path),
596+
);
592597
self.cx.span_err(span, &msg);
593598
self.cx.trace_macros_diag();
594599
fragment_kind.dummy(span)
@@ -878,7 +883,7 @@ impl<'a> Parser<'a> {
878883
err.span_label(span, "caused by the macro expansion here");
879884
let msg = format!(
880885
"the usage of `{}!` is likely invalid in {} context",
881-
macro_path,
886+
pprust::path_to_string(&macro_path),
882887
kind_name,
883888
);
884889
err.note(&msg);

src/libsyntax/parse/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,7 @@ impl<'a> Parser<'a> {
854854
// This is a best-effort recovery.
855855
path.span,
856856
"try",
857-
format!("<{}>::{}", ty_str, path),
857+
format!("<{}>::{}", ty_str, pprust::path_to_string(&path)),
858858
Applicability::MaybeIncorrect,
859859
)
860860
.emit();

src/libsyntax/parse/parser.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,12 +1454,14 @@ impl<'a> Parser<'a> {
14541454
`pub(super)`: visible only in the current module's parent
14551455
`pub(in path::to::module)`: visible only on the specified path"##;
14561456

1457+
let path_str = pprust::path_to_string(&path);
1458+
14571459
struct_span_err!(self.sess.span_diagnostic, path.span, E0704, "{}", msg)
14581460
.help(suggestion)
14591461
.span_suggestion(
14601462
path.span,
1461-
&format!("make this visible only to module `{}` with `in`", path),
1462-
format!("in {}", path),
1463+
&format!("make this visible only to module `{}` with `in`", path_str),
1464+
format!("in {}", path_str),
14631465
Applicability::MachineApplicable,
14641466
)
14651467
.emit();

src/libsyntax/parse/parser/expr.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,8 +552,11 @@ impl<'a> Parser<'a> {
552552

553553
// Report non-fatal diagnostics, keep `x as usize` as an expression
554554
// in AST and continue parsing.
555-
let msg = format!("`<` is interpreted as a start of generic \
556-
arguments for `{}`, not a {}", path, op_noun);
555+
let msg = format!(
556+
"`<` is interpreted as a start of generic arguments for `{}`, not a {}",
557+
pprust::path_to_string(&path),
558+
op_noun,
559+
);
557560
let span_after_type = parser_snapshot_after_type.token.span;
558561
let expr = mk_expr(self, P(Ty {
559562
span: path.span,

0 commit comments

Comments
 (0)