Skip to content

Commit 21e5dc2

Browse files
committed
Auto merge of rust-lang#14881 - Veykril:name-display, r=Veykril
internal: Replace Display impl for Name This allows us to plug in interner for `Name`s that live inside the database.
2 parents a512774 + c7ef6c2 commit 21e5dc2

File tree

108 files changed

+1043
-654
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+1043
-654
lines changed

crates/hir-def/src/body/pretty.rs

+30-20
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use std::fmt::{self, Write};
44

5+
use hir_expand::db::ExpandDatabase;
56
use syntax::ast::HasName;
67

78
use crate::{
@@ -18,16 +19,22 @@ pub(super) fn print_body_hir(db: &dyn DefDatabase, body: &Body, owner: DefWithBo
1819
let header = match owner {
1920
DefWithBodyId::FunctionId(it) => {
2021
let item_tree_id = it.lookup(db).id;
21-
format!("fn {}", item_tree_id.item_tree(db)[item_tree_id.value].name)
22+
format!(
23+
"fn {}",
24+
item_tree_id.item_tree(db)[item_tree_id.value].name.display(db.upcast())
25+
)
2226
}
2327
DefWithBodyId::StaticId(it) => {
2428
let item_tree_id = it.lookup(db).id;
25-
format!("static {} = ", item_tree_id.item_tree(db)[item_tree_id.value].name)
29+
format!(
30+
"static {} = ",
31+
item_tree_id.item_tree(db)[item_tree_id.value].name.display(db.upcast())
32+
)
2633
}
2734
DefWithBodyId::ConstId(it) => {
2835
let item_tree_id = it.lookup(db).id;
2936
let name = match &item_tree_id.item_tree(db)[item_tree_id.value].name {
30-
Some(name) => name.to_string(),
37+
Some(name) => name.display(db.upcast()).to_string(),
3138
None => "_".to_string(),
3239
};
3340
format!("const {name} = ")
@@ -42,7 +49,8 @@ pub(super) fn print_body_hir(db: &dyn DefDatabase, body: &Body, owner: DefWithBo
4249
}
4350
};
4451

45-
let mut p = Printer { body, buf: header, indent_level: 0, needs_indent: false };
52+
let mut p =
53+
Printer { db: db.upcast(), body, buf: header, indent_level: 0, needs_indent: false };
4654
if let DefWithBodyId::FunctionId(it) = owner {
4755
p.buf.push('(');
4856
body.params.iter().zip(&db.function_data(it).params).for_each(|(&param, ty)| {
@@ -61,12 +69,13 @@ pub(super) fn print_body_hir(db: &dyn DefDatabase, body: &Body, owner: DefWithBo
6169
}
6270

6371
pub(super) fn print_expr_hir(
64-
_db: &dyn DefDatabase,
72+
db: &dyn DefDatabase,
6573
body: &Body,
6674
_owner: DefWithBodyId,
6775
expr: ExprId,
6876
) -> String {
69-
let mut p = Printer { body, buf: String::new(), indent_level: 0, needs_indent: false };
77+
let mut p =
78+
Printer { db: db.upcast(), body, buf: String::new(), indent_level: 0, needs_indent: false };
7079
p.print_expr(expr);
7180
p.buf
7281
}
@@ -87,6 +96,7 @@ macro_rules! wln {
8796
}
8897

8998
struct Printer<'a> {
99+
db: &'a dyn ExpandDatabase,
90100
body: &'a Body,
91101
buf: String,
92102
indent_level: usize,
@@ -161,22 +171,22 @@ impl<'a> Printer<'a> {
161171
}
162172
Expr::Loop { body, label } => {
163173
if let Some(lbl) = label {
164-
w!(self, "{}: ", self.body[*lbl].name);
174+
w!(self, "{}: ", self.body[*lbl].name.display(self.db));
165175
}
166176
w!(self, "loop ");
167177
self.print_expr(*body);
168178
}
169179
Expr::While { condition, body, label } => {
170180
if let Some(lbl) = label {
171-
w!(self, "{}: ", self.body[*lbl].name);
181+
w!(self, "{}: ", self.body[*lbl].name.display(self.db));
172182
}
173183
w!(self, "while ");
174184
self.print_expr(*condition);
175185
self.print_expr(*body);
176186
}
177187
Expr::For { iterable, pat, body, label } => {
178188
if let Some(lbl) = label {
179-
w!(self, "{}: ", self.body[*lbl].name);
189+
w!(self, "{}: ", self.body[*lbl].name.display(self.db));
180190
}
181191
w!(self, "for ");
182192
self.print_pat(*pat);
@@ -199,10 +209,10 @@ impl<'a> Printer<'a> {
199209
}
200210
Expr::MethodCall { receiver, method_name, args, generic_args } => {
201211
self.print_expr(*receiver);
202-
w!(self, ".{}", method_name);
212+
w!(self, ".{}", method_name.display(self.db));
203213
if let Some(args) = generic_args {
204214
w!(self, "::<");
205-
print_generic_args(args, self).unwrap();
215+
print_generic_args(self.db, args, self).unwrap();
206216
w!(self, ">");
207217
}
208218
w!(self, "(");
@@ -237,13 +247,13 @@ impl<'a> Printer<'a> {
237247
Expr::Continue { label } => {
238248
w!(self, "continue");
239249
if let Some(lbl) = label {
240-
w!(self, " {}", self.body[*lbl].name);
250+
w!(self, " {}", self.body[*lbl].name.display(self.db));
241251
}
242252
}
243253
Expr::Break { expr, label } => {
244254
w!(self, "break");
245255
if let Some(lbl) = label {
246-
w!(self, " {}", self.body[*lbl].name);
256+
w!(self, " {}", self.body[*lbl].name.display(self.db));
247257
}
248258
if let Some(expr) = expr {
249259
self.whitespace();
@@ -282,7 +292,7 @@ impl<'a> Printer<'a> {
282292
w!(self, "{{");
283293
self.indented(|p| {
284294
for field in &**fields {
285-
w!(p, "{}: ", field.name);
295+
w!(p, "{}: ", field.name.display(self.db));
286296
p.print_expr(field.expr);
287297
wln!(p, ",");
288298
}
@@ -299,7 +309,7 @@ impl<'a> Printer<'a> {
299309
}
300310
Expr::Field { expr, name } => {
301311
self.print_expr(*expr);
302-
w!(self, ".{}", name);
312+
w!(self, ".{}", name.display(self.db));
303313
}
304314
Expr::Await { expr } => {
305315
self.print_expr(*expr);
@@ -437,7 +447,7 @@ impl<'a> Printer<'a> {
437447
}
438448
Expr::Literal(lit) => self.print_literal(lit),
439449
Expr::Block { id: _, statements, tail, label } => {
440-
let label = label.map(|lbl| format!("{}: ", self.body[lbl].name));
450+
let label = label.map(|lbl| format!("{}: ", self.body[lbl].name.display(self.db)));
441451
self.print_block(label.as_deref(), statements, tail);
442452
}
443453
Expr::Unsafe { id: _, statements, tail } => {
@@ -513,7 +523,7 @@ impl<'a> Printer<'a> {
513523
w!(self, " {{");
514524
self.indented(|p| {
515525
for arg in args.iter() {
516-
w!(p, "{}: ", arg.name);
526+
w!(p, "{}: ", arg.name.display(self.db));
517527
p.print_pat(arg.pat);
518528
wln!(p, ",");
519529
}
@@ -646,11 +656,11 @@ impl<'a> Printer<'a> {
646656
}
647657

648658
fn print_type_ref(&mut self, ty: &TypeRef) {
649-
print_type_ref(ty, self).unwrap();
659+
print_type_ref(self.db, ty, self).unwrap();
650660
}
651661

652662
fn print_path(&mut self, path: &Path) {
653-
print_path(path, self).unwrap();
663+
print_path(self.db, path, self).unwrap();
654664
}
655665

656666
fn print_binding(&mut self, id: BindingId) {
@@ -661,6 +671,6 @@ impl<'a> Printer<'a> {
661671
BindingAnnotation::Ref => "ref ",
662672
BindingAnnotation::RefMut => "ref mut ",
663673
};
664-
w!(self, "{}{}", mode, name);
674+
w!(self, "{}{}", mode, name.display(self.db));
665675
}
666676
}

crates/hir-def/src/builtin_type.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,14 @@ impl AsName for BuiltinType {
106106

107107
impl fmt::Display for BuiltinType {
108108
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
109-
let type_name = self.as_name();
110-
type_name.fmt(f)
109+
match self {
110+
BuiltinType::Char => f.write_str("char"),
111+
BuiltinType::Bool => f.write_str("bool"),
112+
BuiltinType::Str => f.write_str("str"),
113+
BuiltinType::Int(it) => it.fmt(f),
114+
BuiltinType::Uint(it) => it.fmt(f),
115+
BuiltinType::Float(it) => it.fmt(f),
116+
}
111117
}
112118
}
113119

crates/hir-def/src/hir/type_ref.rs

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
//! HIR for references to types. Paths in these are not yet resolved. They can
22
//! be directly created from an ast::TypeRef, without further queries.
33
4+
use core::fmt;
45
use std::fmt::Write;
56

67
use hir_expand::{
8+
db::ExpandDatabase,
79
name::{AsName, Name},
810
AstId,
911
};
@@ -383,15 +385,6 @@ pub enum ConstRefOrPath {
383385
Path(Name),
384386
}
385387

386-
impl std::fmt::Display for ConstRefOrPath {
387-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
388-
match self {
389-
ConstRefOrPath::Scalar(s) => s.fmt(f),
390-
ConstRefOrPath::Path(n) => n.fmt(f),
391-
}
392-
}
393-
}
394-
395388
impl ConstRefOrPath {
396389
pub(crate) fn from_expr_opt(expr: Option<ast::Expr>) -> Self {
397390
match expr {
@@ -400,6 +393,19 @@ impl ConstRefOrPath {
400393
}
401394
}
402395

396+
pub fn display<'a>(&'a self, db: &'a dyn ExpandDatabase) -> impl fmt::Display + 'a {
397+
struct Display<'a>(&'a dyn ExpandDatabase, &'a ConstRefOrPath);
398+
impl fmt::Display for Display<'_> {
399+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
400+
match self.1 {
401+
ConstRefOrPath::Scalar(s) => s.fmt(f),
402+
ConstRefOrPath::Path(n) => n.display(self.0).fmt(f),
403+
}
404+
}
405+
}
406+
Display(db, self)
407+
}
408+
403409
// FIXME: as per the comments on `TypeRef::Array`, this evaluation should not happen at this
404410
// parse stage.
405411
fn from_expr(expr: ast::Expr) -> Self {

0 commit comments

Comments
 (0)