Skip to content

Commit 5784915

Browse files
committed
Auto merge of #17584 - Veykril:landing-page, r=Veykril
Implement symbol interning infra Will fix #15590 My unsafe-fu is not the best but it does satisfy miri. There is still some follow up work to do, notably a lot of places using strings instead of symbols/names, most notably the token tree.
2 parents e9afba5 + cde0f69 commit 5784915

Some content is hidden

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

86 files changed

+1418
-792
lines changed

.typos.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ extend-ignore-re = [
1414
"\\w*\\.{3,4}\\w*",
1515
'"flate2"',
1616
"raison d'être",
17+
"inout",
18+
"optin"
1719
]
1820

1921
[default.extend-words]

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/hir-def/src/attr.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use hir_expand::{
99
attrs::{collect_attrs, Attr, AttrId, RawAttrs},
1010
HirFileId, InFile,
1111
};
12+
use intern::{sym, Symbol};
1213
use la_arena::{ArenaMap, Idx, RawIdx};
1314
use mbe::DelimiterKind;
1415
use syntax::{
@@ -152,7 +153,7 @@ impl Attrs {
152153
}
153154

154155
pub fn lang_item(&self) -> Option<LangItem> {
155-
self.by_key("lang").string_value().and_then(LangItem::from_str)
156+
self.by_key("lang").string_value().and_then(|it| LangItem::from_symbol(&Symbol::intern(it)))
156157
}
157158

158159
pub fn has_doc_hidden(&self) -> bool {
@@ -199,8 +200,12 @@ impl Attrs {
199200
.segments()
200201
.iter()
201202
.rev()
202-
.zip(["core", "prelude", "v1", "test"].iter().rev())
203-
.all(|it| it.0.as_str() == Some(it.1))
203+
.zip(
204+
[sym::core.clone(), sym::prelude.clone(), sym::v1.clone(), sym::test.clone()]
205+
.iter()
206+
.rev(),
207+
)
208+
.all(|it| it.0 == it.1)
204209
})
205210
}
206211

@@ -568,6 +573,10 @@ impl<'attr> AttrQuery<'attr> {
568573
self.attrs().find_map(|attr| attr.string_value())
569574
}
570575

576+
pub fn string_value_with_span(self) -> Option<(&'attr str, span::Span)> {
577+
self.attrs().find_map(|attr| attr.string_value_with_span())
578+
}
579+
571580
pub fn string_value_unescape(self) -> Option<Cow<'attr, str>> {
572581
self.attrs().find_map(|attr| attr.string_value_unescape())
573582
}

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

Lines changed: 67 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ use std::mem;
55

66
use base_db::CrateId;
77
use hir_expand::{
8-
name::{name, AsName, Name},
8+
name::{AsName, Name},
99
ExpandError, InFile,
1010
};
11-
use intern::Interned;
11+
use intern::{sym, Interned};
1212
use rustc_hash::FxHashMap;
1313
use smallvec::SmallVec;
1414
use span::AstIdMap;
@@ -187,8 +187,10 @@ impl ExprCollector<'_> {
187187
{
188188
let is_mutable =
189189
self_param.mut_token().is_some() && self_param.amp_token().is_none();
190-
let binding_id: la_arena::Idx<Binding> =
191-
self.alloc_binding(name![self], BindingAnnotation::new(is_mutable, false));
190+
let binding_id: la_arena::Idx<Binding> = self.alloc_binding(
191+
Name::new_symbol_root(sym::self_.clone()),
192+
BindingAnnotation::new(is_mutable, false),
193+
);
192194
self.body.self_param = Some(binding_id);
193195
self.source_map.self_param = Some(self.expander.in_file(AstPtr::new(&self_param)));
194196
}
@@ -1588,18 +1590,22 @@ impl ExprCollector<'_> {
15881590
});
15891591
let mut mappings = vec![];
15901592
let fmt = match template.and_then(|it| self.expand_macros_to_string(it)) {
1591-
Some((s, is_direct_literal)) => format_args::parse(
1592-
&s,
1593-
fmt_snippet,
1594-
args,
1595-
is_direct_literal,
1596-
|name| self.alloc_expr_desugared(Expr::Path(Path::from(name))),
1597-
|name, span| {
1598-
if let Some(span) = span {
1599-
mappings.push((span, name))
1600-
}
1601-
},
1602-
),
1593+
Some((s, is_direct_literal)) => {
1594+
let call_ctx = self.expander.syntax_context();
1595+
format_args::parse(
1596+
&s,
1597+
fmt_snippet,
1598+
args,
1599+
is_direct_literal,
1600+
|name| self.alloc_expr_desugared(Expr::Path(Path::from(name))),
1601+
|name, span| {
1602+
if let Some(span) = span {
1603+
mappings.push((span, name))
1604+
}
1605+
},
1606+
call_ctx,
1607+
)
1608+
}
16031609
None => FormatArgs {
16041610
template: Default::default(),
16051611
arguments: args.finish(),
@@ -1723,14 +1729,18 @@ impl ExprCollector<'_> {
17231729
// unsafe { ::core::fmt::UnsafeArg::new() }
17241730
// )
17251731

1726-
let Some(new_v1_formatted) =
1727-
LangItem::FormatArguments.ty_rel_path(self.db, self.krate, name![new_v1_formatted])
1728-
else {
1732+
let Some(new_v1_formatted) = LangItem::FormatArguments.ty_rel_path(
1733+
self.db,
1734+
self.krate,
1735+
Name::new_symbol_root(sym::new_v1_formatted.clone()),
1736+
) else {
17291737
return self.missing_expr();
17301738
};
1731-
let Some(unsafe_arg_new) =
1732-
LangItem::FormatUnsafeArg.ty_rel_path(self.db, self.krate, name![new])
1733-
else {
1739+
let Some(unsafe_arg_new) = LangItem::FormatUnsafeArg.ty_rel_path(
1740+
self.db,
1741+
self.krate,
1742+
Name::new_symbol_root(sym::new.clone()),
1743+
) else {
17341744
return self.missing_expr();
17351745
};
17361746
let new_v1_formatted = self.alloc_expr_desugared(Expr::Path(new_v1_formatted));
@@ -1812,10 +1822,10 @@ impl ExprCollector<'_> {
18121822
self.db,
18131823
self.krate,
18141824
match alignment {
1815-
Some(FormatAlignment::Left) => name![Left],
1816-
Some(FormatAlignment::Right) => name![Right],
1817-
Some(FormatAlignment::Center) => name![Center],
1818-
None => name![Unknown],
1825+
Some(FormatAlignment::Left) => Name::new_symbol_root(sym::Left.clone()),
1826+
Some(FormatAlignment::Right) => Name::new_symbol_root(sym::Right.clone()),
1827+
Some(FormatAlignment::Center) => Name::new_symbol_root(sym::Center.clone()),
1828+
None => Name::new_symbol_root(sym::Unknown.clone()),
18191829
},
18201830
);
18211831
match align {
@@ -1838,8 +1848,11 @@ impl ExprCollector<'_> {
18381848
let width = self.make_count(width, argmap);
18391849

18401850
let format_placeholder_new = {
1841-
let format_placeholder_new =
1842-
LangItem::FormatPlaceholder.ty_rel_path(self.db, self.krate, name![new]);
1851+
let format_placeholder_new = LangItem::FormatPlaceholder.ty_rel_path(
1852+
self.db,
1853+
self.krate,
1854+
Name::new_symbol_root(sym::new.clone()),
1855+
);
18431856
match format_placeholder_new {
18441857
Some(path) => self.alloc_expr_desugared(Expr::Path(path)),
18451858
None => self.missing_expr(),
@@ -1883,11 +1896,14 @@ impl ExprCollector<'_> {
18831896
*n as u128,
18841897
Some(BuiltinUint::Usize),
18851898
)));
1886-
let count_is =
1887-
match LangItem::FormatCount.ty_rel_path(self.db, self.krate, name![Is]) {
1888-
Some(count_is) => self.alloc_expr_desugared(Expr::Path(count_is)),
1889-
None => self.missing_expr(),
1890-
};
1899+
let count_is = match LangItem::FormatCount.ty_rel_path(
1900+
self.db,
1901+
self.krate,
1902+
Name::new_symbol_root(sym::Is.clone()),
1903+
) {
1904+
Some(count_is) => self.alloc_expr_desugared(Expr::Path(count_is)),
1905+
None => self.missing_expr(),
1906+
};
18911907
self.alloc_expr_desugared(Expr::Call {
18921908
callee: count_is,
18931909
args: Box::new([args]),
@@ -1905,7 +1921,7 @@ impl ExprCollector<'_> {
19051921
let count_param = match LangItem::FormatCount.ty_rel_path(
19061922
self.db,
19071923
self.krate,
1908-
name![Param],
1924+
Name::new_symbol_root(sym::Param.clone()),
19091925
) {
19101926
Some(count_param) => self.alloc_expr_desugared(Expr::Path(count_param)),
19111927
None => self.missing_expr(),
@@ -1921,7 +1937,11 @@ impl ExprCollector<'_> {
19211937
self.missing_expr()
19221938
}
19231939
}
1924-
None => match LangItem::FormatCount.ty_rel_path(self.db, self.krate, name![Implied]) {
1940+
None => match LangItem::FormatCount.ty_rel_path(
1941+
self.db,
1942+
self.krate,
1943+
Name::new_symbol_root(sym::Implied.clone()),
1944+
) {
19251945
Some(count_param) => self.alloc_expr_desugared(Expr::Path(count_param)),
19261946
None => self.missing_expr(),
19271947
},
@@ -1942,18 +1962,18 @@ impl ExprCollector<'_> {
19421962
let new_fn = match LangItem::FormatArgument.ty_rel_path(
19431963
self.db,
19441964
self.krate,
1945-
match ty {
1946-
Format(Display) => name![new_display],
1947-
Format(Debug) => name![new_debug],
1948-
Format(LowerExp) => name![new_lower_exp],
1949-
Format(UpperExp) => name![new_upper_exp],
1950-
Format(Octal) => name![new_octal],
1951-
Format(Pointer) => name![new_pointer],
1952-
Format(Binary) => name![new_binary],
1953-
Format(LowerHex) => name![new_lower_hex],
1954-
Format(UpperHex) => name![new_upper_hex],
1955-
Usize => name![from_usize],
1956-
},
1965+
Name::new_symbol_root(match ty {
1966+
Format(Display) => sym::new_display.clone(),
1967+
Format(Debug) => sym::new_debug.clone(),
1968+
Format(LowerExp) => sym::new_lower_exp.clone(),
1969+
Format(UpperExp) => sym::new_upper_exp.clone(),
1970+
Format(Octal) => sym::new_octal.clone(),
1971+
Format(Pointer) => sym::new_pointer.clone(),
1972+
Format(Binary) => sym::new_binary.clone(),
1973+
Format(LowerHex) => sym::new_lower_hex.clone(),
1974+
Format(UpperHex) => sym::new_upper_hex.clone(),
1975+
Usize => sym::from_usize.clone(),
1976+
}),
19571977
) {
19581978
Some(new_fn) => self.alloc_expr_desugared(Expr::Path(new_fn)),
19591979
None => self.missing_expr(),

crates/hir-def/src/builtin_type.rs

Lines changed: 50 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
66
use std::fmt;
77

8-
use hir_expand::name::{name, AsName, Name};
8+
use hir_expand::name::{AsName, Name};
9+
use intern::sym;
910
/// Different signed int types.
1011
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
1112
pub enum BuiltinInt {
@@ -48,63 +49,67 @@ pub enum BuiltinType {
4849

4950
impl BuiltinType {
5051
#[rustfmt::skip]
51-
pub const ALL: &'static [(Name, BuiltinType)] = &[
52-
(name![char], BuiltinType::Char),
53-
(name![bool], BuiltinType::Bool),
54-
(name![str], BuiltinType::Str),
55-
56-
(name![isize], BuiltinType::Int(BuiltinInt::Isize)),
57-
(name![i8], BuiltinType::Int(BuiltinInt::I8)),
58-
(name![i16], BuiltinType::Int(BuiltinInt::I16)),
59-
(name![i32], BuiltinType::Int(BuiltinInt::I32)),
60-
(name![i64], BuiltinType::Int(BuiltinInt::I64)),
61-
(name![i128], BuiltinType::Int(BuiltinInt::I128)),
62-
63-
(name![usize], BuiltinType::Uint(BuiltinUint::Usize)),
64-
(name![u8], BuiltinType::Uint(BuiltinUint::U8)),
65-
(name![u16], BuiltinType::Uint(BuiltinUint::U16)),
66-
(name![u32], BuiltinType::Uint(BuiltinUint::U32)),
67-
(name![u64], BuiltinType::Uint(BuiltinUint::U64)),
68-
(name![u128], BuiltinType::Uint(BuiltinUint::U128)),
69-
70-
(name![f16], BuiltinType::Float(BuiltinFloat::F16)),
71-
(name![f32], BuiltinType::Float(BuiltinFloat::F32)),
72-
(name![f64], BuiltinType::Float(BuiltinFloat::F64)),
73-
(name![f128], BuiltinType::Float(BuiltinFloat::F128)),
74-
];
52+
pub fn all_builtin_types() -> [(Name, BuiltinType); 19] {
53+
[
54+
(Name::new_symbol_root(sym::char.clone()), BuiltinType::Char),
55+
(Name::new_symbol_root(sym::bool.clone()), BuiltinType::Bool),
56+
(Name::new_symbol_root(sym::str.clone()), BuiltinType::Str),
57+
58+
(Name::new_symbol_root(sym::isize.clone()), BuiltinType::Int(BuiltinInt::Isize)),
59+
(Name::new_symbol_root(sym::i8.clone()), BuiltinType::Int(BuiltinInt::I8)),
60+
(Name::new_symbol_root(sym::i16.clone()), BuiltinType::Int(BuiltinInt::I16)),
61+
(Name::new_symbol_root(sym::i32.clone()), BuiltinType::Int(BuiltinInt::I32)),
62+
(Name::new_symbol_root(sym::i64.clone()), BuiltinType::Int(BuiltinInt::I64)),
63+
(Name::new_symbol_root(sym::i128.clone()), BuiltinType::Int(BuiltinInt::I128)),
64+
65+
(Name::new_symbol_root(sym::usize.clone()), BuiltinType::Uint(BuiltinUint::Usize)),
66+
(Name::new_symbol_root(sym::u8.clone()), BuiltinType::Uint(BuiltinUint::U8)),
67+
(Name::new_symbol_root(sym::u16.clone()), BuiltinType::Uint(BuiltinUint::U16)),
68+
(Name::new_symbol_root(sym::u32.clone()), BuiltinType::Uint(BuiltinUint::U32)),
69+
(Name::new_symbol_root(sym::u64.clone()), BuiltinType::Uint(BuiltinUint::U64)),
70+
(Name::new_symbol_root(sym::u128.clone()), BuiltinType::Uint(BuiltinUint::U128)),
71+
72+
(Name::new_symbol_root(sym::f16.clone()), BuiltinType::Float(BuiltinFloat::F16)),
73+
(Name::new_symbol_root(sym::f32.clone()), BuiltinType::Float(BuiltinFloat::F32)),
74+
(Name::new_symbol_root(sym::f64.clone()), BuiltinType::Float(BuiltinFloat::F64)),
75+
(Name::new_symbol_root(sym::f128.clone()), BuiltinType::Float(BuiltinFloat::F128)),
76+
]
77+
}
7578

7679
pub fn by_name(name: &Name) -> Option<Self> {
77-
Self::ALL.iter().find_map(|(n, ty)| if n == name { Some(*ty) } else { None })
80+
Self::all_builtin_types()
81+
.iter()
82+
.find_map(|(n, ty)| if n == name { Some(*ty) } else { None })
7883
}
7984
}
8085

8186
impl AsName for BuiltinType {
8287
fn as_name(&self) -> Name {
8388
match self {
84-
BuiltinType::Char => name![char],
85-
BuiltinType::Bool => name![bool],
86-
BuiltinType::Str => name![str],
89+
BuiltinType::Char => Name::new_symbol_root(sym::char.clone()),
90+
BuiltinType::Bool => Name::new_symbol_root(sym::bool.clone()),
91+
BuiltinType::Str => Name::new_symbol_root(sym::str.clone()),
8792
BuiltinType::Int(it) => match it {
88-
BuiltinInt::Isize => name![isize],
89-
BuiltinInt::I8 => name![i8],
90-
BuiltinInt::I16 => name![i16],
91-
BuiltinInt::I32 => name![i32],
92-
BuiltinInt::I64 => name![i64],
93-
BuiltinInt::I128 => name![i128],
93+
BuiltinInt::Isize => Name::new_symbol_root(sym::isize.clone()),
94+
BuiltinInt::I8 => Name::new_symbol_root(sym::i8.clone()),
95+
BuiltinInt::I16 => Name::new_symbol_root(sym::i16.clone()),
96+
BuiltinInt::I32 => Name::new_symbol_root(sym::i32.clone()),
97+
BuiltinInt::I64 => Name::new_symbol_root(sym::i64.clone()),
98+
BuiltinInt::I128 => Name::new_symbol_root(sym::i128.clone()),
9499
},
95100
BuiltinType::Uint(it) => match it {
96-
BuiltinUint::Usize => name![usize],
97-
BuiltinUint::U8 => name![u8],
98-
BuiltinUint::U16 => name![u16],
99-
BuiltinUint::U32 => name![u32],
100-
BuiltinUint::U64 => name![u64],
101-
BuiltinUint::U128 => name![u128],
101+
BuiltinUint::Usize => Name::new_symbol_root(sym::usize.clone()),
102+
BuiltinUint::U8 => Name::new_symbol_root(sym::u8.clone()),
103+
BuiltinUint::U16 => Name::new_symbol_root(sym::u16.clone()),
104+
BuiltinUint::U32 => Name::new_symbol_root(sym::u32.clone()),
105+
BuiltinUint::U64 => Name::new_symbol_root(sym::u64.clone()),
106+
BuiltinUint::U128 => Name::new_symbol_root(sym::u128.clone()),
102107
},
103108
BuiltinType::Float(it) => match it {
104-
BuiltinFloat::F16 => name![f16],
105-
BuiltinFloat::F32 => name![f32],
106-
BuiltinFloat::F64 => name![f64],
107-
BuiltinFloat::F128 => name![f128],
109+
BuiltinFloat::F16 => Name::new_symbol_root(sym::f16.clone()),
110+
BuiltinFloat::F32 => Name::new_symbol_root(sym::f32.clone()),
111+
BuiltinFloat::F64 => Name::new_symbol_root(sym::f64.clone()),
112+
BuiltinFloat::F128 => Name::new_symbol_root(sym::f128.clone()),
108113
},
109114
}
110115
}

0 commit comments

Comments
 (0)